diff options
Diffstat (limited to 'src/gui/form/mod.rs')
-rw-r--r-- | src/gui/form/mod.rs | 295 |
1 files changed, 179 insertions, 116 deletions
diff --git a/src/gui/form/mod.rs b/src/gui/form/mod.rs index dbaf879..e05bb78 100644 --- a/src/gui/form/mod.rs +++ b/src/gui/form/mod.rs @@ -39,16 +39,22 @@ pub async fn repetition_dialog(app: &App, date: NaiveDate, event: &Event) { .build(); lines.append(&button); let tx = app.tx.clone(); - button.connect_clicked( - glib::clone!( - #[weak] dialog, - #[strong] event, - move |_| { - update::send(tx.clone(), Msg::ShowUpdateRepetitionForm { date, event_id: event.id }); - dialog.close() - } - ) - ); + button.connect_clicked(glib::clone!( + #[weak] + dialog, + #[strong] + event, + move |_| { + update::send( + tx.clone(), + Msg::ShowUpdateRepetitionForm { + date, + event_id: event.id, + }, + ); + dialog.close() + } + )); let button = gtk::Button::builder() .label("Toutes les occurences") @@ -56,32 +62,38 @@ pub async fn repetition_dialog(app: &App, date: NaiveDate, event: &Event) { .build(); lines.append(&button); let tx = app.tx.clone(); - button.connect_clicked( - glib::clone!( - #[weak] dialog, - #[strong] event, - move |_| { - update::send(tx.clone(), Msg::ShowUpdateForm { event_id: event.id }); - dialog.close() - } - ) - ); + button.connect_clicked(glib::clone!( + #[weak] + dialog, + #[strong] + event, + move |_| { + update::send(tx.clone(), Msg::ShowUpdateForm { event_id: event.id }); + dialog.close() + } + )); let button = gtk::Button::builder() .label("À partir de cette occurence") .build(); lines.append(&button); let tx = app.tx.clone(); - button.connect_clicked( - glib::clone!( - #[weak] dialog, - #[strong] event, - move |_| { - update::send(tx.clone(), Msg::ShowUpdateFromOccurence { date, event_id: event.id }); - dialog.close() - } - ) - ); + button.connect_clicked(glib::clone!( + #[weak] + dialog, + #[strong] + event, + move |_| { + update::send( + tx.clone(), + Msg::ShowUpdateFromOccurence { + date, + event_id: event.id, + }, + ); + dialog.close() + } + )); dialog.run_future().await; } @@ -211,97 +223,128 @@ pub async fn show(app: &App, target: Target) { let conn = app.conn.clone(); let tx = app.tx.clone(); let categories = app.categories.clone(); - button.connect_clicked( - glib::clone!( - #[weak] dialog, - #[strong] target, - #[strong] categories, - move |_| { - let removed_occurences = match &target { - Target::Update { event, .. } => { - event.repetition.as_ref().map(|r| r.removed_occurences.clone()).unwrap_or_default() - }, - _ => HashSet::new(), - }; - match repetition::validate(&repetition_model, removed_occurences) { - Ok(repetition) => { - let id = match &target { - Target::Update {event} => event.id, - _ => Uuid::new_v4(), - }; - - // Find category id from selected id - let category = categories.iter().find(|c| c.name == dropdown_categories[category_dropdown.selected() as usize]).map(|c| c.id); - - match event::validate(id, date.buffer().text().to_string(), name.buffer().text().to_string(), start.buffer().text().to_string(), end.buffer().text().to_string(), repetition, category) { - Some(new) => { - match &target { - Target::New {..} => { - match db::events::insert(&conn, &new) { - Ok(_) => { - update::send(tx.clone(), Msg::AddEvent { new }); - dialog.close() - }, - Err(err) => eprintln!("Error when inserting event: {}", err) - } + button.connect_clicked(glib::clone!( + #[weak] + dialog, + #[strong] + target, + #[strong] + categories, + move |_| { + let removed_occurences = match &target { + Target::Update { event, .. } => event + .repetition + .as_ref() + .map(|r| r.removed_occurences.clone()) + .unwrap_or_default(), + _ => HashSet::new(), + }; + match repetition::validate(&repetition_model, removed_occurences) { + Ok(repetition) => { + let id = match &target { + Target::Update { event } => event.id, + _ => Uuid::new_v4(), + }; + + // Find category id from selected id + let category = categories + .iter() + .find(|c| { + c.name == dropdown_categories[category_dropdown.selected() as usize] + }) + .map(|c| c.id); + + match event::validate( + id, + date.buffer().text().to_string(), + name.buffer().text().to_string(), + start.buffer().text().to_string(), + end.buffer().text().to_string(), + repetition, + category, + ) { + Some(new) => { + match &target { + Target::New { .. } => match db::events::insert(&conn, &new) { + Ok(_) => { + update::send(tx.clone(), Msg::AddEvent { new }); + dialog.close() } - Target::Update {event} => { - match db::events::update(&conn, &new) { - Ok(_) => { - update::send(tx.clone(), Msg::UpdateEvent { old: event.clone(), new }); - dialog.close() + Err(err) => eprintln!("Error when inserting event: {}", err), + }, + Target::Update { event } => match db::events::update(&conn, &new) { + Ok(_) => { + update::send( + tx.clone(), + Msg::UpdateEvent { + old: event.clone(), + new, }, - Err(err) => eprintln!("Error when updating event: {}", err) - } + ); + dialog.close() } - Target::UpdateRepetition { event, date } => { - // TODO: improve intermediate error state - match delete_repetition_occurence(&conn, event, *date) { - Ok(occurence) => { - match db::events::insert(&conn, &new) { - Ok(_) => { - update::send(tx.clone(), Msg::UpdateEventOccurence { - event: event.clone(), - occurence, - date: *date, - new - }) - } - Err(err) => eprintln!("Error when updating repetition: {}", err) - }; - dialog.close() - }, - Err(err) => eprintln!("Error when updating repetition: {}", err) + Err(err) => eprintln!("Error when updating event: {}", err), + }, + Target::UpdateRepetition { event, date } => { + // TODO: improve intermediate error state + match delete_repetition_occurence(&conn, event, *date) { + Ok(occurence) => { + match db::events::insert(&conn, &new) { + Ok(_) => update::send( + tx.clone(), + Msg::UpdateEventOccurence { + event: event.clone(), + occurence, + date: *date, + new, + }, + ), + Err(err) => eprintln!( + "Error when updating repetition: {}", + err + ), + }; + dialog.close() + } + Err(err) => { + eprintln!("Error when updating repetition: {}", err) } } - Target::UpdateFromOccurence { date, event } => { - match update_repetition_until(&conn, *date - Duration::days(1), event) { - Ok(updated) => { - match db::events::insert(&conn, &new) { - Ok(_) => { - update::send(tx.clone(), Msg::UpdateRepeatedFrom { - old: event.clone(), - updated, - new - }); - dialog.close() + } + Target::UpdateFromOccurence { date, event } => { + match update_repetition_until( + &conn, + *date - Duration::days(1), + event, + ) { + Ok(updated) => match db::events::insert(&conn, &new) { + Ok(_) => { + update::send( + tx.clone(), + Msg::UpdateRepeatedFrom { + old: event.clone(), + updated, + new, }, - Err(err) => eprintln!("Error when inserting event: {}", err) - } - }, - Err(err) => eprintln!("Error when updating event: {}", err) - } + ); + dialog.close() + } + Err(err) => { + eprintln!("Error when inserting event: {}", err) + } + }, + Err(err) => eprintln!("Error when updating event: {}", err), } } } - None => eprintln!("Event is not valid.") } - }, - Err(message) => eprintln!("{}", message) + None => eprintln!("Event is not valid."), + } } + Err(message) => eprintln!("{}", message), } - ) - ); + } + )); if let Some(event) = event { let label = match target { @@ -313,13 +356,22 @@ pub async fn show(app: &App, target: Target) { lines.append(&button); let conn = app.conn.clone(); let tx = app.tx.clone(); - button.connect_clicked( - glib::clone!(#[weak] dialog, move |_| { + button.connect_clicked(glib::clone!( + #[weak] + dialog, + move |_| { match target { Target::UpdateRepetition { date, .. } => { match delete_repetition_occurence(&conn, &event, date) { Ok(occurence) => { - update::send(tx.clone(), Msg::DeleteOccurence { event: event.clone(), date, occurence }); + update::send( + tx.clone(), + Msg::DeleteOccurence { + event: event.clone(), + date, + occurence, + }, + ); dialog.close() } Err(err) => { @@ -330,16 +382,27 @@ pub async fn show(app: &App, target: Target) { Target::UpdateFromOccurence { date, .. } => { match update_repetition_until(&conn, date - Duration::days(1), &event) { Ok(updated) => { - update::send(tx.clone(), Msg::UpdateEvent { old: event.clone(), new: updated }); + update::send( + tx.clone(), + Msg::UpdateEvent { + old: event.clone(), + new: updated, + }, + ); dialog.close() - }, - Err(err) => eprintln!("Error when updating event: {}", err) + } + Err(err) => eprintln!("Error when updating event: {}", err), } } _ => { let operation = db::events::delete(&conn, &event.id); if operation.is_ok() { - update::send(tx.clone(), Msg::DeleteEvent { event: event.clone() }); + update::send( + tx.clone(), + Msg::DeleteEvent { + event: event.clone(), + }, + ); dialog.close() } } |