use gtk4 as gtk; use async_channel::{Receiver, Sender}; use chrono::NaiveDate; use gtk::prelude::*; use rusqlite::Connection; use std::rc::Rc; use crate::app::calendar; use crate::app::form; use crate::app::utils; use crate::app::App; use crate::model::event::Event; pub fn send(tx: Sender, msg: Msg) { utils::spawn(async move { let _ = tx.send(msg).await; }) } pub enum Msg { ShowAddForm { date: NaiveDate }, AddEvent { event: Event }, } pub async fn event_handler(conn: Rc, rx: Receiver, tx: Sender, mut app: App) { while let Ok(msg) = rx.recv().await { match msg { Msg::ShowAddForm { date } => { form::dialog(Rc::clone(&conn), tx.clone(), Rc::clone(&app.window), date).await; } Msg::AddEvent { event } => { let date = event.date.clone(); let d = date.signed_duration_since(app.start_date).num_days(); app.events.push(event); let col = (d % 7) as i32; let row = 1 + (d / 7) as i32; app.grid.attach( &calendar::day_entry(tx.clone(), &date, &app.today, &app.events), col, row, 1, 1, ); } } } }