diff options
Diffstat (limited to 'src/gui/update.rs')
-rw-r--r-- | src/gui/update.rs | 107 |
1 files changed, 93 insertions, 14 deletions
diff --git a/src/gui/update.rs b/src/gui/update.rs index c8dfa6d..419a6e4 100644 --- a/src/gui/update.rs +++ b/src/gui/update.rs @@ -2,6 +2,7 @@ use async_channel::{Receiver, Sender}; use chrono::{Duration, NaiveDate}; use gtk4::prelude::GridExt; use std::collections::HashSet; +use std::iter::FromIterator; use crate::{ gui::{calendar, form, utils, App}, @@ -15,11 +16,41 @@ pub fn send(tx: Sender<Msg>, msg: Msg) { } pub enum Msg { - ShowAddForm { date: NaiveDate }, - ShowUpdateForm { event: Event }, - AddEvent { new: Event }, - UpdateEvent { old: Event, new: Event }, - DeleteEvent { event: Event }, + ShowAddForm { + date: NaiveDate, + }, + ShowRepetitionDialog { + date: NaiveDate, + event: Event, + }, + ShowUpdateForm { + event: Event, + }, + ShowUpdateRepetitionForm { + date: NaiveDate, + event: Event, + }, + AddEvent { + new: Event, + }, + UpdateEvent { + old: Event, + new: Event, + }, + UpdateEventOccurence { + event: Event, + occurence: usize, + date: NaiveDate, + new: Event, + }, + DeleteEvent { + event: Event, + }, + DeleteOccurence { + event: Event, + date: NaiveDate, + occurence: usize, + }, SelectPreviousWeek, SelectNextWeek, } @@ -27,8 +58,14 @@ pub enum Msg { 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(&app, event::init(date), true).await, - Msg::ShowUpdateForm { event } => form::show(&app, event, false).await, + Msg::ShowAddForm { date } => form::show(&app, form::Target::New { date }).await, + Msg::ShowRepetitionDialog { date, event } => { + form::repetition_dialog(&app, date, event).await + } + Msg::ShowUpdateForm { event } => form::show(&app, form::Target::Update { event }).await, + Msg::ShowUpdateRepetitionForm { date, event } => { + form::show(&app, form::Target::UpdateRepetition { event, date }).await + } Msg::AddEvent { new } => { let refresh_dates = add(&mut app, &new); refresh(&app, &refresh_dates) @@ -38,10 +75,29 @@ pub async fn event_handler(rx: Receiver<Msg>, mut app: App) { refresh_dates.extend(add(&mut app, &new)); refresh(&app, &refresh_dates); } + Msg::UpdateEventOccurence { + event, + occurence, + date, + new, + } => { + remove_occurence(&mut app, &event, occurence); + let mut refresh_dates = add(&mut app, &new); + refresh_dates.insert(date); + refresh(&app, &refresh_dates) + } Msg::DeleteEvent { event } => { let refresh_dates = remove(&mut app, &event); refresh(&app, &refresh_dates) } + Msg::DeleteOccurence { + event, + date, + occurence, + } => { + remove_occurence(&mut app, &event, occurence); + refresh(&app, &HashSet::from([date])) + } Msg::SelectPreviousWeek => { app.grid.remove_row(4); app.grid.insert_row(1); @@ -66,12 +122,10 @@ pub async fn event_handler(rx: Receiver<Msg>, mut app: App) { /// Remove event and return dates that should be refreshed. fn remove(app: &mut App, event: &Event) -> HashSet<NaiveDate> { if event.repetition.is_some() { - match app.repeated_events.iter().position(|e| e.id == event.id) { + match app.recurring_events.iter().position(|e| e.id == event.id) { Some(index) => { - app.repeated_events.remove(index); - let mut dates = repetition_dates(app, event); - dates.insert(event.date); - dates + app.recurring_events.remove(index); + repetition_dates(app, event) } None => { eprintln!("Event not found when trying to delete {:?}", event); @@ -92,10 +146,35 @@ fn remove(app: &mut App, event: &Event) -> HashSet<NaiveDate> { } } +/// Remove event repetition +fn remove_occurence(app: &mut App, event: &Event, occurence: usize) { + match app.recurring_events.iter().position(|e| e.id == event.id) { + Some(index) => { + let event = app.recurring_events.get_mut(index).unwrap(); + match event.repetition.clone() { + Some(mut repetition) => { + repetition.removed_occurences.insert(occurence); + event.repetition = Some(repetition.clone()) + } + None => eprintln!( + "Repetition not found when trying to delete repetition of {:?}", + event + ), + } + } + None => { + eprintln!( + "Event not found when trying to delete repetition of {:?}", + event + ) + } + } +} + /// Add event and return dates that should be refreshed. fn add(app: &mut App, event: &Event) -> HashSet<NaiveDate> { if event.repetition.is_some() { - app.repeated_events.push(event.clone()); + app.recurring_events.push(event.clone()); let mut dates = repetition_dates(app, event); dates.insert(event.date); dates @@ -114,7 +193,7 @@ fn repetition_dates(app: &App, event: &Event) -> HashSet<NaiveDate> { /// Refresh app for the given dates. fn refresh(app: &App, dates: &HashSet<NaiveDate>) { let repetitions = - event::repetitions_between(&app.repeated_events, app.start_date, app.end_date); + event::repetitions_between(&app.recurring_events, app.start_date, app.end_date); for date in dates { calendar::refresh_date(app, *date, &repetitions) |