use gtk4 as gtk; use anyhow::Result; use async_channel::Sender; use chrono::{Datelike, Duration, NaiveDate, Weekday}; use rusqlite::Connection; use std::rc::Rc; use crate::gui::calendar; use crate::gui::update::Msg; use crate::{db, model::category::Category, model::event::Event}; pub struct App { pub conn: Rc, pub window: Rc, pub tx: Sender, // Calendar pub calendar: 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, // Categories // pub categories: gtk::Box, pub categories: Vec, pub default_color: String, } impl App { pub fn new(conn: Rc, app: >k::Application, tx: Sender) -> Result { let today = chrono::offset::Local::now().naive_local().date(); // TODO: error handling let start_date = NaiveDate::from_isoywd_opt(today.year(), today.iso_week().week(), Weekday::Mon) .unwrap(); let end_date = start_date + Duration::days(7 * 4 - 1); let events = db::events::list_non_recurring_between(&conn, start_date, end_date)?; let recurring_events = db::events::list_recurring(&conn)?; let categories = db::categories::list(&conn)?; let default_color = db::event_color::get_default_color(&conn)?; let calendar = calendar::create( tx.clone(), today, start_date, end_date, &events, &recurring_events, &categories, &default_color, ); // let categories = gtk::Box::builder() // .orientation(gtk::Orientation::Vertical) // .build(); // let notebook = gtk::Notebook::builder().build(); // notebook.append_page(&calendar, Some(>k::Label::new(Some("Calendrier")))); // notebook.append_page(&categories, Some(>k::Label::new(Some("Catégories")))); // window.set_child(Some(¬ebook)); let window = Rc::new( gtk::ApplicationWindow::builder() .application(app) .title("Calendrier") .default_width(1056) .default_height(594) .visible(true) .child(&calendar) .build(), ); Ok(Self { conn, window, tx, calendar, events, recurring_events, today, start_date, end_date, categories, default_color, }) } }