diff options
author | Joris | 2022-03-12 13:27:29 +0100 |
---|---|---|
committer | Joris | 2022-03-12 13:36:09 +0100 |
commit | d584df359640176ec4bc06f59d1e8d42ab17a413 (patch) | |
tree | 6cfaf676fc2ecf4e61067aa376fb2bed0d984d79 /src/gui/form/repetition.rs | |
parent | aad7b9601dfa05255d5c24f4a6377d9a25646d45 (diff) |
Update and delete recurring events
Diffstat (limited to 'src/gui/form/repetition.rs')
-rw-r--r-- | src/gui/form/repetition.rs | 47 |
1 files changed, 26 insertions, 21 deletions
diff --git a/src/gui/form/repetition.rs b/src/gui/form/repetition.rs index accb091..1d36765 100644 --- a/src/gui/form/repetition.rs +++ b/src/gui/form/repetition.rs @@ -2,13 +2,11 @@ use gtk4 as gtk; use chrono::{Weekday, Weekday::*}; use gtk::prelude::*; +use std::collections::HashSet; -use crate::{ - model::event::Event, - model::{ - repetition, - repetition::{DayOfMonth, Repetition}, - }, +use crate::model::{ + repetition, + repetition::{DayOfMonth, Frequency, Repetition}, }; static WEEKDAYS_STR: [&str; 7] = [ @@ -29,7 +27,7 @@ pub struct Model { yearly_radio: gtk::CheckButton, } -pub fn view(event: &Event) -> Model { +pub fn view(repetition: Option<&Repetition>) -> Model { let view = gtk::Box::builder() .orientation(gtk::Orientation::Vertical) .build(); @@ -39,12 +37,14 @@ pub fn view(event: &Event) -> Model { let no_radio = gtk::CheckButton::builder() .label("Non") - .active(event.repetition.is_none()) + .active(repetition.is_none()) .build(); view.append(&no_radio); - let default = match event.repetition { - Some(Repetition::Daily { period }) => period.to_string(), + let frequency = repetition.as_ref().map(|r| r.frequency.clone()); + + let default = match frequency { + Some(Frequency::Daily { period }) => period.to_string(), _ => "".to_string(), }; let day_interval_entry = gtk::Entry::builder().text(&default).build(); @@ -56,8 +56,8 @@ pub fn view(event: &Event) -> Model { ); view.append(&day_interval_box); - let default = match event.repetition { - Some(Repetition::Monthly { + let default = match frequency { + Some(Frequency::Monthly { day: DayOfMonth::Day { day }, }) => day.to_string(), _ => "".to_string(), @@ -67,8 +67,8 @@ pub fn view(event: &Event) -> Model { radio_input(&no_radio, !default.is_empty(), &monthly_entry, "Mensuel"); view.append(&monthly_box); - let (active, default) = match event.repetition { - Some(Repetition::Monthly { + let (active, default) = match frequency { + Some(Frequency::Monthly { day: DayOfMonth::Weekday { weekday }, }) => (true, weekday), _ => (false, Mon), @@ -83,7 +83,7 @@ pub fn view(event: &Event) -> Model { let yearly_radio = gtk::CheckButton::builder() .group(&no_radio) .label("Annuel") - .active(event.repetition == Some(Repetition::Yearly)) + .active(frequency == Some(Frequency::Yearly)) .build(); view.append(&yearly_radio); @@ -127,24 +127,29 @@ fn label(text: &str) -> gtk::Label { } pub fn validate(model: &Model) -> Result<Option<Repetition>, String> { - if model.no_radio.is_active() { + let frequency = if model.no_radio.is_active() { Ok(None) } else if model.day_interval_radio.is_active() { let period = repetition::validate_period(&model.day_interval_entry.buffer().text())?; - Ok(Some(Repetition::Daily { period })) + Ok(Some(Frequency::Daily { period })) } else if model.monthly_radio.is_active() { let day = repetition::validate_day(&model.monthly_entry.buffer().text())?; - Ok(Some(Repetition::Monthly { + Ok(Some(Frequency::Monthly { day: DayOfMonth::Day { day }, })) } else if model.first_day_radio.is_active() { let weekday = WEEKDAYS[model.first_day_dropdown.selected() as usize]; - Ok(Some(Repetition::Monthly { + Ok(Some(Frequency::Monthly { day: DayOfMonth::Weekday { weekday }, })) } else if model.yearly_radio.is_active() { - Ok(Some(Repetition::Yearly)) + Ok(Some(Frequency::Yearly)) } else { Err("Aucune option n’a été sélectionnée".to_string()) - } + }?; + + Ok(frequency.map(|frequency| Repetition { + frequency, + removed_occurences: HashSet::new(), + })) } |