From 251077b0c1986065736fc60d7645b49198830f07 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Tue, 23 Jan 2024 08:46:35 -0500 Subject: [PATCH] Implement the Edit Cancel button --- fitnesstrax/app/src/components/day.rs | 98 ++++++++++--------- fitnesstrax/app/src/view_models/day_detail.rs | 73 ++++++++------ 2 files changed, 94 insertions(+), 77 deletions(-) diff --git a/fitnesstrax/app/src/components/day.rs b/fitnesstrax/app/src/components/day.rs index 60ef5f7..ce6ed28 100644 --- a/fitnesstrax/app/src/components/day.rs +++ b/fitnesstrax/app/src/components/day.rs @@ -233,52 +233,8 @@ impl DayEdit { *s.imp().on_finished.borrow_mut() = Box::new(on_finished); - s.append( - &ActionGroup::builder() - .primary_action("Save", { - let s = s.clone(); - let view_model = view_model.clone(); - move || { - view_model.save(); - s.finish(); - } - }) - .secondary_action("Cancel", { - let s = s.clone(); - let view_model = view_model.clone(); - move || { - view_model.revert(); - s.finish(); - } - }) - .build(), - ); - - let top_row = gtk::Box::builder() - .orientation(gtk::Orientation::Horizontal) - .build(); - top_row.append( - &weight_field(view_model.weight(), { - let view_model = view_model.clone(); - move |w| match w { - Some(w) => view_model.set_weight(w), - None => eprintln!("have not implemented record delete"), - } - }) - .widget(), - ); - - top_row.append( - &steps_editor(view_model.steps(), { - let view_model = view_model.clone(); - move |s| match s { - Some(s) => view_model.set_steps(s), - None => eprintln!("have not implemented record delete"), - } - }) - .widget(), - ); - s.append(&top_row); + s.append(&control_buttons(&s, &view_model)); + s.append(&weight_and_steps_row(&view_model)); s } @@ -287,3 +243,53 @@ impl DayEdit { (self.imp().on_finished.borrow())() } } + +fn control_buttons(s: &DayEdit, view_model: &DayDetailViewModel) -> ActionGroup { + ActionGroup::builder() + .primary_action("Save", { + let s = s.clone(); + let view_model = view_model.clone(); + move || { + view_model.save(); + s.finish(); + } + }) + .secondary_action("Cancel", { + let s = s.clone(); + let view_model = view_model.clone(); + move || { + view_model.revert(); + s.finish(); + } + }) + .build() +} + +fn weight_and_steps_row(view_model: &DayDetailViewModel) -> gtk::Box { + let row = gtk::Box::builder() + .orientation(gtk::Orientation::Horizontal) + .build(); + row.append( + &weight_field(view_model.weight(), { + let view_model = view_model.clone(); + move |w| match w { + Some(w) => view_model.set_weight(w), + None => eprintln!("have not implemented record delete"), + } + }) + .widget(), + ); + + row.append( + &steps_editor(view_model.steps(), { + let view_model = view_model.clone(); + move |s| match s { + Some(s) => view_model.set_steps(s), + None => eprintln!("have not implemented record delete"), + } + }) + .widget(), + ); + + row +} diff --git a/fitnesstrax/app/src/view_models/day_detail.rs b/fitnesstrax/app/src/view_models/day_detail.rs index f0d359d..3007481 100644 --- a/fitnesstrax/app/src/view_models/day_detail.rs +++ b/fitnesstrax/app/src/view_models/day_detail.rs @@ -95,43 +95,24 @@ pub struct DayDetailViewModel { weight: Arc>>>, steps: Arc>>>, records: Arc>>>, + + original_records: Vec>, } impl DayDetailViewModel { pub fn new(date: chrono::NaiveDate, records: Vec>, app: App) -> Self { - let (weight_records, records): (Vec>, Vec>) = - records.into_iter().partition(|r| r.data.is_weight()); - let (step_records, records): (Vec>, Vec>) = - records.into_iter().partition(|r| r.data.is_steps()); - Self { + let s = Self { app: Some(app), date, - weight: Arc::new(RwLock::new( - weight_records - .first() - .and_then(|r| match r.data { - TraxRecord::Weight(ref w) => Some((r.id.clone(), w.clone())), - _ => None, - }) - .map(|(id, w)| RecordState::Original(Record { id, data: w })), - )), - steps: Arc::new(RwLock::new( - step_records - .first() - .and_then(|r| match r.data { - TraxRecord::Steps(ref w) => Some((r.id.clone(), w.clone())), - _ => None, - }) - .map(|(id, w)| RecordState::Original(Record { id, data: w })), - )), - records: Arc::new(RwLock::new( - records - .into_iter() - .map(|r| (r.id.clone(), RecordState::Original(r))) - .collect::>>(), - )), - } + weight: Arc::new(RwLock::new(None)), + steps: Arc::new(RwLock::new(None)), + records: Arc::new(RwLock::new(HashMap::new())), + + original_records: records, + }; + s.populate_records(); + s } pub fn weight(&self) -> Option { @@ -275,7 +256,37 @@ impl DayDetailViewModel { } pub fn revert(&self) { - unimplemented!(); + self.populate_records(); + } + + fn populate_records(&self) { + let records = self.original_records.clone(); + + let (weight_records, records): (Vec>, Vec>) = + records.into_iter().partition(|r| r.data.is_weight()); + let (step_records, records): (Vec>, Vec>) = + records.into_iter().partition(|r| r.data.is_steps()); + + *self.weight.write().unwrap() = weight_records + .first() + .and_then(|r| match r.data { + TraxRecord::Weight(ref w) => Some((r.id.clone(), w.clone())), + _ => None, + }) + .map(|(id, w)| RecordState::Original(Record { id, data: w })); + + *self.steps.write().unwrap() = step_records + .first() + .and_then(|r| match r.data { + TraxRecord::Steps(ref w) => Some((r.id.clone(), w.clone())), + _ => None, + }) + .map(|(id, w)| RecordState::Original(Record { id, data: w })); + + *self.records.write().unwrap() = records + .into_iter() + .map(|r| (r.id.clone(), RecordState::Original(r))) + .collect::>>(); } }