use gtk4 as gtk; use anyhow::Result; use async_channel::Sender; use chrono::{Datelike, Duration, NaiveDate, Weekday}; use gtk::glib::signal::Inhibit; use gtk::prelude::*; use rusqlite::Connection; use std::rc::Rc; use crate::gui::calendar; use crate::gui::update::Msg; use crate::{db, model::event::Event}; pub struct App { pub conn: Rc, pub window: Rc, pub grid: gtk::Grid, pub events: Vec, // TODO: use Hashmap to have fast access to events by id ? pub recurring_events: Vec, // TODO: use Hashmap to have fast access to events by id ? pub today: NaiveDate, pub start_date: NaiveDate, pub end_date: NaiveDate, pub tx: Sender, } impl App { pub fn new(conn: Rc, app: >k::Application, tx: Sender) -> Result { let window = Rc::new( gtk::ApplicationWindow::builder() .application(app) .title("Calendar") .default_width(800) .default_height(600) .visible(true) .build(), ); let today = chrono::offset::Local::today().naive_utc(); let start_date = NaiveDate::from_isoywd(today.year(), today.iso_week().week(), Weekday::Mon); let end_date = start_date + Duration::days(7 * 4 - 1); let events = db::list_non_recurring_between(&conn, start_date, end_date)?; let recurring_events = db::list_recurring(&conn)?; let grid = calendar::create( tx.clone(), today, start_date, end_date, &events, &recurring_events, ); window.set_child(Some(&grid)); window.connect_close_request(move |window| { if let Some(application) = window.application() { application.remove_window(window); } Inhibit(false) }); Ok(Self { conn, window, grid, events, recurring_events, today, start_date, end_date, tx, }) } }