From 7bd4885b09bfe6d2cc5b0b147b80fdc970498374 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Fri, 29 Dec 2023 09:24:37 -0500 Subject: [PATCH] Save new information to the day detail view and to the historical view --- fitnesstrax/app/src/app_window.rs | 8 ++++++ fitnesstrax/app/src/components/day.rs | 35 +++++++++++++++++++++------ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/fitnesstrax/app/src/app_window.rs b/fitnesstrax/app/src/app_window.rs index b5ab019..69d6ab1 100644 --- a/fitnesstrax/app/src/app_window.rs +++ b/fitnesstrax/app/src/app_window.rs @@ -113,6 +113,14 @@ impl AppWindow { s.load_records(); + s.navigation.connect_popped({ + let s = s.clone(); + move |_, _| match *s.current_view.borrow() { + View::Historical(_) => s.load_records(), + _ => {} + } + }); + s } diff --git a/fitnesstrax/app/src/components/day.rs b/fitnesstrax/app/src/components/day.rs index 8529c04..19ebda5 100644 --- a/fitnesstrax/app/src/components/day.rs +++ b/fitnesstrax/app/src/components/day.rs @@ -16,6 +16,7 @@ You should have received a copy of the GNU General Public License along with Fit // use chrono::NaiveDate; // use ft_core::TraxRecord; +use chrono::{Local, NaiveDate}; use dimensioned::si; use emseries::Record; use ft_core::{RecordType, TimeDistance, TraxRecord, Weight}; @@ -133,14 +134,14 @@ glib::wrapper! { } impl DayDetail { - pub fn new( + pub fn new( date: chrono::NaiveDate, records: Vec>, - on_save_record: SaveRecordFn, + on_put_record: PutRecordFn, on_update_record: UpdateRecordFn, ) -> Self where - SaveRecordFn: Fn(TraxRecord) + 'static, + PutRecordFn: Fn(TraxRecord) + 'static, UpdateRecordFn: Fn(Record) + 'static, { let s: Self = Object::builder().build(); @@ -168,14 +169,14 @@ impl DayDetail { }); let weight_view = match weight_record { - Some((id, data)) => WeightView::new(Some(data.clone()), move |weight| { + Some((id, data)) => WeightView::new(date.clone(), Some(data.clone()), move |weight| { on_update_record(Record { id: id.clone(), data: TraxRecord::Weight(Weight { date, weight }), }) }), - None => WeightView::new(None, move |weight| { - on_save_record(TraxRecord::Weight(Weight { date, weight })); + None => WeightView::new(date.clone(), None, move |weight| { + on_put_record(TraxRecord::Weight(Weight { date, weight })); }), }; s.append(&weight_view); @@ -220,6 +221,7 @@ impl DayDetail { } pub struct WeightViewPrivate { + date: RefCell, record: RefCell>, view: RefCell, edit: RefCell, @@ -239,6 +241,7 @@ impl Default for WeightViewPrivate { let current = view.clone(); Self { + date: RefCell::new(Local::now().date_naive()), record: RefCell::new(None), view: RefCell::new(view), edit: RefCell::new(edit), @@ -264,13 +267,18 @@ glib::wrapper! { } impl WeightView { - pub fn new(weight: Option, on_edit_finished: OnEditFinished) -> Self + pub fn new( + date: NaiveDate, + weight: Option, + on_edit_finished: OnEditFinished, + ) -> Self where OnEditFinished: Fn(si::Kilogram) + 'static, { let s: Self = Object::builder().build(); *s.imp().on_edit_finished.borrow_mut() = Box::new(on_edit_finished); + *s.imp().date.borrow_mut() = date; *s.imp().record.borrow_mut() = weight; s.view(); @@ -324,8 +332,19 @@ impl WeightView { if *self.imp().current.borrow() == *edit { let w = edit.buffer().text().parse::().unwrap(); self.imp().on_edit_finished.borrow()(w * si::KG); - self.view(); + + let mut record = self.imp().record.borrow_mut(); + match *record { + Some(ref mut record) => record.weight = w * si::KG, + None => { + *record = Some(Weight { + date: self.imp().date.borrow().clone(), + weight: w * si::KG, + }) + } + } } + self.view(); } }