diff options
author | Joris | 2022-01-09 14:37:35 +0100 |
---|---|---|
committer | Joris | 2022-01-09 14:37:35 +0100 |
commit | d0a9063631e71928484a698f6c585ebb3915e8a2 (patch) | |
tree | 9a5fc430684aea73877711dd3392b1845d4b9904 /src/app/update.rs | |
parent | 5166efe517291f5c9fc6326f30651ef799d6db65 (diff) |
Allow to update events
Diffstat (limited to 'src/app/update.rs')
-rw-r--r-- | src/app/update.rs | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/src/app/update.rs b/src/app/update.rs index e7bf7af..f1576b5 100644 --- a/src/app/update.rs +++ b/src/app/update.rs @@ -1,11 +1,9 @@ use async_channel::{Receiver, Sender}; use chrono::NaiveDate; -use rusqlite::Connection; -use std::rc::Rc; use crate::{ app::{calendar, form, utils, App}, - model::event::Event, + model::{event, event::Event}, }; pub fn send(tx: Sender<Msg>, msg: Msg) { @@ -16,20 +14,39 @@ pub fn send(tx: Sender<Msg>, msg: Msg) { pub enum Msg { ShowAddForm { date: NaiveDate }, - AddEvent { event: Event }, + ShowUpdateForm { event: Event }, + AddEvent { new: Event }, + UpdateEvent { old: Event, new: Event }, } -pub async fn event_handler(conn: Rc<Connection>, rx: Receiver<Msg>, tx: Sender<Msg>, mut app: App) { +pub async fn event_handler(rx: Receiver<Msg>, mut app: App) { while let Ok(msg) = rx.recv().await { match msg { Msg::ShowAddForm { date } => { - form::show(Rc::clone(&conn), tx.clone(), Rc::clone(&app.window), date).await; + form::show(&app, event::init(date), true).await; } - Msg::AddEvent { event } => { - let date = event.date.clone(); - app.events.push(event); + Msg::ShowUpdateForm { event } => { + form::show(&app, event, false).await; + } + Msg::AddEvent { new } => { + let date = new.date.clone(); + app.events.push(new); calendar::refresh_date(&app, date); } + Msg::UpdateEvent { old, new } => { + let new_date = new.date.clone(); + match app.events.iter().position(|e| e.id == new.id) { + Some(index) => { + app.events.remove(index); + app.events.push(new); + calendar::refresh_date(&app, new_date); + if old.date != new_date { + calendar::refresh_date(&app, old.date.clone()) + } + } + None => println!("Event not found when updating from {:?} to {:?}", old, new), + } + } } } } |