From 487071a89b9a91b416cfa3a47ed8cfce11b1a520 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Thu, 28 Dec 2023 09:51:41 -0500 Subject: [PATCH] Record data to the database This isn't recording real data. It's basically discarding all information from the weight edit field. But it is creating a record. --- fitnesstrax/app/src/app_window.rs | 19 ++++++++++++- fitnesstrax/app/src/components/day.rs | 28 ++++++++++++-------- fitnesstrax/app/src/views/historical_view.rs | 27 +++++-------------- 3 files changed, 41 insertions(+), 33 deletions(-) diff --git a/fitnesstrax/app/src/app_window.rs b/fitnesstrax/app/src/app_window.rs index d97017c..b59afa7 100644 --- a/fitnesstrax/app/src/app_window.rs +++ b/fitnesstrax/app/src/app_window.rs @@ -217,7 +217,24 @@ impl AppWindow { Rc::new(move |date, records| { let layout = gtk::Box::new(gtk::Orientation::Vertical, 0); layout.append(&adw::HeaderBar::new()); - layout.append(&DayDetail::new(date, records)); + layout.append(&DayDetail::new( + date, + records, + { + let app_tx = s.app_tx.clone(); + move |record| { + let _ = + app_tx.send_blocking(AppInvocation::PutRecord(record)); + } + }, + { + let app_tx = s.app_tx.clone(); + move |record| { + let _ = app_tx + .send_blocking(AppInvocation::UpdateRecord(record)); + } + }, + )); let page = &adw::NavigationPage::builder() .title(date.format("%Y-%m-%d").to_string()) .child(&layout) diff --git a/fitnesstrax/app/src/components/day.rs b/fitnesstrax/app/src/components/day.rs index 66364cb..6af77a1 100644 --- a/fitnesstrax/app/src/components/day.rs +++ b/fitnesstrax/app/src/components/day.rs @@ -133,7 +133,16 @@ glib::wrapper! { } impl DayDetail { - pub fn new(date: chrono::NaiveDate, records: Vec>) -> Self { + pub fn new( + date: chrono::NaiveDate, + records: Vec>, + on_save_record: SaveRecordFn, + on_update_record: UpdateRecordFn, + ) -> Self + where + SaveRecordFn: Fn(TraxRecord) + 'static, + UpdateRecordFn: Fn(Record) + 'static, + { let s: Self = Object::builder().build(); s.set_orientation(gtk::Orientation::Vertical); @@ -159,17 +168,14 @@ impl DayDetail { }); let weight_view = match weight_record { - Some((id, record)) => WeightView::new(Some(record.clone()), move |weight| { - println!( - "on_blur on the weight view. Need to record {:?}, {:?}", - id, weight - ); + Some((id, data)) => WeightView::new(Some(data.clone()), move |weight| { + on_update_record(Record { + id: id.clone(), + data: TraxRecord::Weight(Weight { date, weight }), + }) }), - None => WeightView::new(None, |weight| { - println!( - "on_blur on the weight view. Need to create a new record for {:?}", - weight - ); + None => WeightView::new(None, move |weight| { + on_save_record(TraxRecord::Weight(Weight { date, weight })); }), }; s.append(&weight_view); diff --git a/fitnesstrax/app/src/views/historical_view.rs b/fitnesstrax/app/src/views/historical_view.rs index 4111127..938223d 100644 --- a/fitnesstrax/app/src/views/historical_view.rs +++ b/fitnesstrax/app/src/views/historical_view.rs @@ -155,6 +155,9 @@ impl DayRecords { } } +// This isn't feeling quite right. DayRecords is a glib object, but I'm not sure that I want to +// really be passing that around. It seems not generic enough. I feel like this whole grouped +// records thing can be made more generic. struct GroupedRecords { interval: DayInterval, data: HashMap, @@ -184,12 +187,6 @@ impl GroupedRecords { } fn items<'a>(&'a self) -> impl Iterator + 'a { - /* - GroupedRecordIterator { - interval: self.interval.clone(), - data: &self.data, - } - */ self.interval.days().map(|date| { self.data .get(&date) @@ -199,21 +196,9 @@ impl GroupedRecords { } } -/* -struct GroupedRecordIterator<'a> { - interval: DayInterval, - data: &'a HashMap, -} - -impl <'a> Iterator for GroupedRecordIterator<'a> { - type Item = &'a DayRecords; - - fn next(&mut self) -> Option<&'a DayRecords> { - - } -} -*/ - +// This interval doesn't feel right, either. The idea that I have a specific interval type for just +// NaiveDate is odd. This should be genericized, as should the iterator. Also, it shouldn't live +// here, but in utilities. #[derive(Clone, Debug, PartialEq, Eq)] struct DayInterval { start: NaiveDate,