diff options
author | Joris | 2022-03-19 22:03:58 +0100 |
---|---|---|
committer | Joris | 2022-03-19 22:03:58 +0100 |
commit | 35cc74578e969bae4812afd2ff041eba3746142d (patch) | |
tree | c0ef18d3c81554f0e70c91fcb1006d587470365c /src/gui/form/repetition.rs | |
parent | 199624dc03ead28ddc7454147457512d9568c593 (diff) |
Allow to repeat an event until a specific date
Also provide a shortcut to modify a repetead event from a specific
occurence. This set the end repetition date under the hood and create a
new repeated event.
Diffstat (limited to 'src/gui/form/repetition.rs')
-rw-r--r-- | src/gui/form/repetition.rs | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/src/gui/form/repetition.rs b/src/gui/form/repetition.rs index 1d36765..4da65ac 100644 --- a/src/gui/form/repetition.rs +++ b/src/gui/form/repetition.rs @@ -1,13 +1,15 @@ use gtk4 as gtk; -use chrono::{Weekday, Weekday::*}; +use chrono::{NaiveDate, Weekday, Weekday::*}; use gtk::prelude::*; use std::collections::HashSet; +use crate::gui::form::utils; use crate::model::{ - repetition, + event, repetition, repetition::{DayOfMonth, Frequency, Repetition}, }; +use crate::validation; static WEEKDAYS_STR: [&str; 7] = [ "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche", @@ -25,6 +27,7 @@ pub struct Model { first_day_radio: gtk::CheckButton, first_day_dropdown: gtk::DropDown, yearly_radio: gtk::CheckButton, + until: gtk::Entry, } pub fn view(repetition: Option<&Repetition>) -> Model { @@ -52,7 +55,7 @@ pub fn view(repetition: Option<&Repetition>) -> Model { &no_radio, !default.is_empty(), &day_interval_entry, - "Interval de jours", + "Intervalle de jours", ); view.append(&day_interval_box); @@ -84,9 +87,18 @@ pub fn view(repetition: Option<&Repetition>) -> Model { .group(&no_radio) .label("Annuel") .active(frequency == Some(Frequency::Yearly)) + .margin_bottom(10) .build(); view.append(&yearly_radio); + let until = repetition + .as_ref() + .and_then(|r| r.until) + .map(|u| utils::entry(&u.format(event::DATE_FORMAT).to_string())) + .unwrap_or_default(); + view.append(&utils::label("Répéter jusqu’au")); + view.append(&until); + Model { view, no_radio, @@ -97,6 +109,7 @@ pub fn view(repetition: Option<&Repetition>) -> Model { first_day_radio, first_day_dropdown, yearly_radio, + until, } } @@ -126,7 +139,10 @@ fn label(text: &str) -> gtk::Label { .build() } -pub fn validate(model: &Model) -> Result<Option<Repetition>, String> { +pub fn validate( + model: &Model, + removed_occurences: HashSet<usize>, +) -> Result<Option<Repetition>, String> { let frequency = if model.no_radio.is_active() { Ok(None) } else if model.day_interval_radio.is_active() { @@ -148,8 +164,22 @@ pub fn validate(model: &Model) -> Result<Option<Repetition>, String> { Err("Aucune option n’a été sélectionnée".to_string()) }?; + // Check until + let until = (if frequency.is_some() { + match validation::non_empty(model.until.buffer().text()) { + Some(until) => match NaiveDate::parse_from_str(&until, event::DATE_FORMAT) { + Ok(until) => Ok(Some(until)), + Err(_) => Err(format!("Can’t parse date from {}", until)), + }, + None => Ok(None), + } + } else { + Ok(None) + })?; + Ok(frequency.map(|frequency| Repetition { frequency, - removed_occurences: HashSet::new(), + removed_occurences, + until, })) } |