diff --git a/fitnesstrax/app/src/components/day.rs b/fitnesstrax/app/src/components/day.rs index 31f01f0..2a3d8b7 100644 --- a/fitnesstrax/app/src/components/day.rs +++ b/fitnesstrax/app/src/components/day.rs @@ -275,26 +275,40 @@ impl DayEdit { s.set_orientation(gtk::Orientation::Vertical); s.set_hexpand(true); - /*, move |weight| { - on_update_record(Record { - id: id.clone(), - data: ft_core::TraxRecord::Weight(ft_core::Weight { date, weight }), - }) - }*/ - - /*, move |weight| { - on_put_record(ft_core::TraxRecord::Weight(ft_core::Weight { - date, - weight, - })); - }*/ - s.append( &ActionGroup::builder() .primary_action("Save", { let s = s.clone(); + let records = records.clone(); move || { - println!("weight value: {:?}", s.imp().weight.value()); + println!("saving to database"); + let weight_record = records.iter().find_map(|record| match record { + Record { + id, + data: ft_core::TraxRecord::Weight(w), + } => Some((id, w)), + _ => None, + }); + + let weight = s.imp().weight.value(); + + if let Some(weight) = weight { + match weight_record { + Some((id, _)) => on_update_record(Record { + id: id.clone(), + data: ft_core::TraxRecord::Weight(ft_core::Weight { + date, + weight, + }), + }), + None => { + on_put_record(ft_core::TraxRecord::Weight(ft_core::Weight { + date, + weight, + })) + } + } + }; } }) .secondary_action("Cancel", on_cancel) diff --git a/fitnesstrax/app/src/views/day_detail_view.rs b/fitnesstrax/app/src/views/day_detail_view.rs index cbd47b7..f9b5128 100644 --- a/fitnesstrax/app/src/views/day_detail_view.rs +++ b/fitnesstrax/app/src/views/day_detail_view.rs @@ -54,6 +54,7 @@ impl DayDetailView { *s.imp().date.borrow_mut() = date; *s.imp().records.borrow_mut() = records; + *s.imp().app.borrow_mut() = Some(app); s.append(&s.imp().container); @@ -105,6 +106,7 @@ impl DayDetailView { } fn on_put_record(&self) -> Box { + let s = self.clone(); let app = self.imp().app.clone(); Box::new(move |record| { let app = app.clone(); @@ -118,23 +120,29 @@ impl DayDetailView { } } }); + s.view(); }) } fn on_update_record(&self) -> Box)> { + let s = self.clone(); let app = self.imp().app.clone(); Box::new(move |record| { let app = app.clone(); glib::spawn_future_local({ async move { + println!("record: {:?}", record); match &*app.borrow() { Some(app) => { let _ = app.update_record(record).await; } - None => {} + None => { + println!("no app!"); + } } } }); + s.view(); }) } }