From 79d705c1d0075c0a26cd2a5c2de835bff01eb201 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Mon, 29 Jan 2024 09:18:36 -0500 Subject: [PATCH] The view model can no longer be initialized without an app --- fitnesstrax/app/src/components/day.rs | 12 +- fitnesstrax/app/src/view_models/day_detail.rs | 113 +++++++++--------- fitnesstrax/app/src/views/day_detail_view.rs | 36 +++--- 3 files changed, 85 insertions(+), 76 deletions(-) diff --git a/fitnesstrax/app/src/components/day.rs b/fitnesstrax/app/src/components/day.rs index 5d1d713..8c0930c 100644 --- a/fitnesstrax/app/src/components/day.rs +++ b/fitnesstrax/app/src/components/day.rs @@ -199,7 +199,7 @@ impl DayDetail { pub struct DayEditPrivate { on_finished: RefCell>, workout_rows: RefCell, - view_model: RefCell, + view_model: RefCell>, } impl Default for DayEditPrivate { @@ -212,7 +212,7 @@ impl Default for DayEditPrivate { .hexpand(true) .build(), ), - view_model: RefCell::new(DayDetailViewModel::default()), + view_model: RefCell::new(None), } } } @@ -241,7 +241,7 @@ impl DayEdit { s.set_orientation(gtk::Orientation::Vertical); s.set_hexpand(true); *s.imp().on_finished.borrow_mut() = Box::new(on_finished); - *s.imp().view_model.borrow_mut() = view_model.clone(); + *s.imp().view_model.borrow_mut() = Some(view_model.clone()); let workout_buttons = workout_buttons(view_model.clone(), { let s = s.clone(); @@ -322,7 +322,11 @@ impl DayEdit { _ => panic!("Record type {:?} is not a Time/Distance record", type_), }; let record = Record { id, data }; - self.imp().view_model.borrow().update_record(record); + let view_model = self.imp().view_model.borrow(); + let view_model = view_model + .as_ref() + .expect("DayEdit has not been initialized with a view model"); + view_model.update_record(record); } } diff --git a/fitnesstrax/app/src/view_models/day_detail.rs b/fitnesstrax/app/src/view_models/day_detail.rs index 81a22ed..5268774 100644 --- a/fitnesstrax/app/src/view_models/day_detail.rs +++ b/fitnesstrax/app/src/view_models/day_detail.rs @@ -92,12 +92,9 @@ impl Deref for RecordState { } } -#[derive(Default)] -struct DayDetailViewModelInner {} - -#[derive(Clone, Default)] +#[derive(Clone)] pub struct DayDetailViewModel { - app: Option, + app: App, pub date: chrono::NaiveDate, weight: Arc>>>, steps: Arc>>>, @@ -109,7 +106,7 @@ pub struct DayDetailViewModel { impl DayDetailViewModel { pub fn new(date: chrono::NaiveDate, records: Vec>, app: App) -> Self { let s = Self { - app: Some(app), + app, date, weight: Arc::new(RwLock::new(None)), @@ -231,63 +228,63 @@ impl DayDetailViewModel { glib::spawn_future({ let s = self.clone(); async move { - if let Some(app) = s.app { - let weight_record = s.weight.read().unwrap().clone(); - match weight_record { - Some(RecordState::New(Record { data, .. })) => { - let _ = app.put_record(TraxRecord::Weight(data)).await; - } - Some(RecordState::Original(_)) => {} - Some(RecordState::Updated(weight)) => { - let _ = app - .update_record(Record { - id: weight.id, - data: TraxRecord::Weight(weight.data), - }) - .await; - } - Some(RecordState::Deleted(_)) => {} - None => {} + let weight_record = s.weight.read().unwrap().clone(); + match weight_record { + Some(RecordState::New(Record { data, .. })) => { + let _ = s.app.put_record(TraxRecord::Weight(data)).await; } - - let steps_record = s.steps.read().unwrap().clone(); - match steps_record { - Some(RecordState::New(Record { data, .. })) => { - let _ = app.put_record(TraxRecord::Steps(data)).await; - } - Some(RecordState::Original(_)) => {} - Some(RecordState::Updated(steps)) => { - let _ = app - .update_record(Record { - id: steps.id, - data: TraxRecord::Steps(steps.data), - }) - .await; - } - Some(RecordState::Deleted(_)) => {} - None => {} + Some(RecordState::Original(_)) => {} + Some(RecordState::Updated(weight)) => { + let _ = s + .app + .update_record(Record { + id: weight.id, + data: TraxRecord::Weight(weight.data), + }) + .await; } + Some(RecordState::Deleted(_)) => {} + None => {} + } - let records = s - .records - .write() - .unwrap() - .drain() - .map(|(_, record)| record) - .collect::>>(); + let steps_record = s.steps.read().unwrap().clone(); + match steps_record { + Some(RecordState::New(Record { data, .. })) => { + let _ = s.app.put_record(TraxRecord::Steps(data)).await; + } + Some(RecordState::Original(_)) => {} + Some(RecordState::Updated(steps)) => { + let _ = s + .app + .update_record(Record { + id: steps.id, + data: TraxRecord::Steps(steps.data), + }) + .await; + } + Some(RecordState::Deleted(_)) => {} + None => {} + } - for record in records { - println!("saving record: {:?}", record); - match record { - RecordState::New(Record { data, .. }) => { - let _ = app.put_record(data).await; - } - RecordState::Original(_) => {} - RecordState::Updated(r) => { - let _ = app.update_record(r.clone()).await; - } - RecordState::Deleted(_) => unimplemented!(), + let records = s + .records + .write() + .unwrap() + .drain() + .map(|(_, record)| record) + .collect::>>(); + + for record in records { + println!("saving record: {:?}", record); + match record { + RecordState::New(Record { data, .. }) => { + let _ = s.app.put_record(data).await; } + RecordState::Original(_) => {} + RecordState::Updated(r) => { + let _ = s.app.update_record(r.clone()).await; + } + RecordState::Deleted(_) => unimplemented!(), } } } diff --git a/fitnesstrax/app/src/views/day_detail_view.rs b/fitnesstrax/app/src/views/day_detail_view.rs index ae213fe..0f9b19c 100644 --- a/fitnesstrax/app/src/views/day_detail_view.rs +++ b/fitnesstrax/app/src/views/day_detail_view.rs @@ -25,7 +25,7 @@ use std::cell::RefCell; #[derive(Default)] pub struct DayDetailViewPrivate { container: Singleton, - view_model: RefCell, + view_model: RefCell>, } #[glib::object_subclass] @@ -47,7 +47,7 @@ glib::wrapper! { impl DayDetailView { pub fn new(view_model: DayDetailViewModel) -> Self { let s: Self = Object::builder().build(); - *s.imp().view_model.borrow_mut() = view_model; + *s.imp().view_model.borrow_mut() = Some(view_model); s.append(&s.imp().container); @@ -57,20 +57,28 @@ impl DayDetailView { } fn view(&self) { - self.imp() - .container - .swap(&DayDetail::new(self.imp().view_model.borrow().clone(), { - let s = self.clone(); - move || s.edit() - })); + let view_model = self.imp().view_model.borrow(); + let view_model = view_model + .as_ref() + .expect("DayDetailView has not been initialized with a view_model") + .clone(); + + self.imp().container.swap(&DayDetail::new(view_model, { + let s = self.clone(); + move || s.edit() + })); } fn edit(&self) { - self.imp() - .container - .swap(&DayEdit::new(self.imp().view_model.borrow().clone(), { - let s = self.clone(); - move || s.view() - })); + let view_model = self.imp().view_model.borrow(); + let view_model = view_model + .as_ref() + .expect("DayDetailView has not been initialized with a view_model") + .clone(); + + self.imp().container.swap(&DayEdit::new(view_model, { + let s = self.clone(); + move || s.view() + })); } }