Thoroughly fill out the day detail view model along with proper automated testing #179
|
@ -222,12 +222,21 @@ impl DayDetail {
|
|||
|
||||
pub struct DayEditPrivate {
|
||||
on_finished: RefCell<Box<dyn Fn()>>,
|
||||
workout_rows: RefCell<gtk::Box>,
|
||||
view_model: RefCell<Option<DayDetailViewModel>>,
|
||||
}
|
||||
|
||||
impl Default for DayEditPrivate {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
on_finished: RefCell::new(Box::new(|| {})),
|
||||
workout_rows: RefCell::new(
|
||||
gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.hexpand(true)
|
||||
.build(),
|
||||
),
|
||||
view_model: RefCell::new(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -257,6 +266,7 @@ impl DayEdit {
|
|||
s.set_hexpand(true);
|
||||
|
||||
*s.imp().on_finished.borrow_mut() = Box::new(on_finished);
|
||||
*s.imp().view_model.borrow_mut() = Some(view_model.clone());
|
||||
|
||||
s.append(
|
||||
&ActionGroup::builder()
|
||||
|
|
|
@ -75,12 +75,9 @@ impl<T: Clone + emseries::Recordable> Deref for RecordState<T> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct DayDetailViewModelInner {}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
#[derive(Clone)]
|
||||
pub struct DayDetailViewModel {
|
||||
app: Option<App>,
|
||||
app: App,
|
||||
pub date: chrono::NaiveDate,
|
||||
weight: Arc<RwLock<Option<RecordState<ft_core::Weight>>>>,
|
||||
steps: Arc<RwLock<Option<RecordState<ft_core::Steps>>>>,
|
||||
|
@ -94,7 +91,7 @@ impl DayDetailViewModel {
|
|||
let (step_records, records): (Vec<Record<TraxRecord>>, Vec<Record<TraxRecord>>) =
|
||||
records.into_iter().partition(|r| r.data.is_steps());
|
||||
Self {
|
||||
app: Some(app),
|
||||
app,
|
||||
date,
|
||||
weight: Arc::new(RwLock::new(
|
||||
weight_records
|
||||
|
@ -168,15 +165,15 @@ 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(weight)) => {
|
||||
let _ = app.put_record(TraxRecord::Weight(weight)).await;
|
||||
Some(RecordState::New(data)) => {
|
||||
let _ = s.app.put_record(TraxRecord::Weight(data)).await;
|
||||
}
|
||||
Some(RecordState::Original(_)) => {}
|
||||
Some(RecordState::Updated(weight)) => {
|
||||
let _ = app
|
||||
let _ = s
|
||||
.app
|
||||
.update_record(Record {
|
||||
id: weight.id,
|
||||
data: TraxRecord::Weight(weight.data),
|
||||
|
@ -189,12 +186,13 @@ impl DayDetailViewModel {
|
|||
|
||||
let steps_record = s.steps.read().unwrap().clone();
|
||||
match steps_record {
|
||||
Some(RecordState::New(steps)) => {
|
||||
let _ = app.put_record(TraxRecord::Steps(steps)).await;
|
||||
Some(RecordState::New(data)) => {
|
||||
let _ = s.app.put_record(TraxRecord::Steps(data)).await;
|
||||
}
|
||||
Some(RecordState::Original(_)) => {}
|
||||
Some(RecordState::Updated(steps)) => {
|
||||
let _ = app
|
||||
let _ = s
|
||||
.app
|
||||
.update_record(Record {
|
||||
id: steps.id,
|
||||
data: TraxRecord::Steps(steps.data),
|
||||
|
@ -216,17 +214,16 @@ impl DayDetailViewModel {
|
|||
for record in records {
|
||||
match record {
|
||||
RecordState::New(data) => {
|
||||
let _ = app.put_record(data).await;
|
||||
let _ = s.app.put_record(data).await;
|
||||
}
|
||||
RecordState::Original(_) => {}
|
||||
RecordState::Updated(r) => {
|
||||
let _ = app.update_record(r.clone()).await;
|
||||
let _ = s.app.update_record(r.clone()).await;
|
||||
}
|
||||
RecordState::Deleted(_) => unimplemented!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ use std::cell::RefCell;
|
|||
#[derive(Default)]
|
||||
pub struct DayDetailViewPrivate {
|
||||
container: Singleton,
|
||||
view_model: RefCell<DayDetailViewModel>,
|
||||
view_model: RefCell<Option<DayDetailViewModel>>,
|
||||
}
|
||||
|
||||
#[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,18 +57,26 @@ impl DayDetailView {
|
|||
}
|
||||
|
||||
fn view(&self) {
|
||||
self.imp()
|
||||
.container
|
||||
.swap(&DayDetail::new(self.imp().view_model.borrow().clone(), {
|
||||
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 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()
|
||||
}));
|
||||
|
|
Loading…
Reference in New Issue