The view model can no longer be initialized without an app
This commit is contained in:
parent
7d5d639ed9
commit
9874b6081b
|
@ -197,7 +197,7 @@ impl DayDetail {
|
||||||
pub struct DayEditPrivate {
|
pub struct DayEditPrivate {
|
||||||
on_finished: RefCell<Box<dyn Fn()>>,
|
on_finished: RefCell<Box<dyn Fn()>>,
|
||||||
workout_rows: RefCell<gtk::Box>,
|
workout_rows: RefCell<gtk::Box>,
|
||||||
view_model: RefCell<DayDetailViewModel>,
|
view_model: RefCell<Option<DayDetailViewModel>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for DayEditPrivate {
|
impl Default for DayEditPrivate {
|
||||||
|
@ -210,7 +210,7 @@ impl Default for DayEditPrivate {
|
||||||
.hexpand(true)
|
.hexpand(true)
|
||||||
.build(),
|
.build(),
|
||||||
),
|
),
|
||||||
view_model: RefCell::new(DayDetailViewModel::default()),
|
view_model: RefCell::new(None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -239,7 +239,7 @@ impl DayEdit {
|
||||||
s.set_orientation(gtk::Orientation::Vertical);
|
s.set_orientation(gtk::Orientation::Vertical);
|
||||||
s.set_hexpand(true);
|
s.set_hexpand(true);
|
||||||
*s.imp().on_finished.borrow_mut() = Box::new(on_finished);
|
*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 workout_buttons = workout_buttons(view_model.clone(), {
|
||||||
let s = s.clone();
|
let s = s.clone();
|
||||||
|
@ -320,7 +320,11 @@ impl DayEdit {
|
||||||
_ => panic!("Record type {:?} is not a Time/Distance record", type_),
|
_ => panic!("Record type {:?} is not a Time/Distance record", type_),
|
||||||
};
|
};
|
||||||
let record = Record { id, data };
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -93,12 +93,9 @@ impl<T: Clone + emseries::Recordable> Deref for RecordState<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Clone)]
|
||||||
struct DayDetailViewModelInner {}
|
|
||||||
|
|
||||||
#[derive(Clone, Default)]
|
|
||||||
pub struct DayDetailViewModel {
|
pub struct DayDetailViewModel {
|
||||||
app: Option<App>,
|
app: App,
|
||||||
pub date: chrono::NaiveDate,
|
pub date: chrono::NaiveDate,
|
||||||
weight: Arc<RwLock<Option<RecordState<ft_core::Weight>>>>,
|
weight: Arc<RwLock<Option<RecordState<ft_core::Weight>>>>,
|
||||||
steps: Arc<RwLock<Option<RecordState<ft_core::Steps>>>>,
|
steps: Arc<RwLock<Option<RecordState<ft_core::Steps>>>>,
|
||||||
|
@ -110,7 +107,7 @@ pub struct DayDetailViewModel {
|
||||||
impl DayDetailViewModel {
|
impl DayDetailViewModel {
|
||||||
pub fn new(date: chrono::NaiveDate, records: Vec<Record<TraxRecord>>, app: App) -> Self {
|
pub fn new(date: chrono::NaiveDate, records: Vec<Record<TraxRecord>>, app: App) -> Self {
|
||||||
let s = Self {
|
let s = Self {
|
||||||
app: Some(app),
|
app,
|
||||||
date,
|
date,
|
||||||
|
|
||||||
weight: Arc::new(RwLock::new(None)),
|
weight: Arc::new(RwLock::new(None)),
|
||||||
|
@ -230,15 +227,15 @@ impl DayDetailViewModel {
|
||||||
glib::spawn_future({
|
glib::spawn_future({
|
||||||
let s = self.clone();
|
let s = self.clone();
|
||||||
async move {
|
async move {
|
||||||
if let Some(app) = s.app {
|
|
||||||
let weight_record = s.weight.read().unwrap().clone();
|
let weight_record = s.weight.read().unwrap().clone();
|
||||||
match weight_record {
|
match weight_record {
|
||||||
Some(RecordState::New(Record { data, .. })) => {
|
Some(RecordState::New(Record { data, .. })) => {
|
||||||
let _ = app.put_record(TraxRecord::Weight(data)).await;
|
let _ = s.app.put_record(TraxRecord::Weight(data)).await;
|
||||||
}
|
}
|
||||||
Some(RecordState::Original(_)) => {}
|
Some(RecordState::Original(_)) => {}
|
||||||
Some(RecordState::Updated(weight)) => {
|
Some(RecordState::Updated(weight)) => {
|
||||||
let _ = app
|
let _ = s
|
||||||
|
.app
|
||||||
.update_record(Record {
|
.update_record(Record {
|
||||||
id: weight.id,
|
id: weight.id,
|
||||||
data: TraxRecord::Weight(weight.data),
|
data: TraxRecord::Weight(weight.data),
|
||||||
|
@ -252,11 +249,12 @@ impl DayDetailViewModel {
|
||||||
let steps_record = s.steps.read().unwrap().clone();
|
let steps_record = s.steps.read().unwrap().clone();
|
||||||
match steps_record {
|
match steps_record {
|
||||||
Some(RecordState::New(Record { data, .. })) => {
|
Some(RecordState::New(Record { data, .. })) => {
|
||||||
let _ = app.put_record(TraxRecord::Steps(data)).await;
|
let _ = s.app.put_record(TraxRecord::Steps(data)).await;
|
||||||
}
|
}
|
||||||
Some(RecordState::Original(_)) => {}
|
Some(RecordState::Original(_)) => {}
|
||||||
Some(RecordState::Updated(steps)) => {
|
Some(RecordState::Updated(steps)) => {
|
||||||
let _ = app
|
let _ = s
|
||||||
|
.app
|
||||||
.update_record(Record {
|
.update_record(Record {
|
||||||
id: steps.id,
|
id: steps.id,
|
||||||
data: TraxRecord::Steps(steps.data),
|
data: TraxRecord::Steps(steps.data),
|
||||||
|
@ -279,17 +277,16 @@ impl DayDetailViewModel {
|
||||||
println!("saving record: {:?}", record);
|
println!("saving record: {:?}", record);
|
||||||
match record {
|
match record {
|
||||||
RecordState::New(Record { data, .. }) => {
|
RecordState::New(Record { data, .. }) => {
|
||||||
let _ = app.put_record(data).await;
|
let _ = s.app.put_record(data).await;
|
||||||
}
|
}
|
||||||
RecordState::Original(_) => {}
|
RecordState::Original(_) => {}
|
||||||
RecordState::Updated(r) => {
|
RecordState::Updated(r) => {
|
||||||
let _ = app.update_record(r.clone()).await;
|
let _ = s.app.update_record(r.clone()).await;
|
||||||
}
|
}
|
||||||
RecordState::Deleted(_) => unimplemented!(),
|
RecordState::Deleted(_) => unimplemented!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ use std::cell::RefCell;
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct DayDetailViewPrivate {
|
pub struct DayDetailViewPrivate {
|
||||||
container: Singleton,
|
container: Singleton,
|
||||||
view_model: RefCell<DayDetailViewModel>,
|
view_model: RefCell<Option<DayDetailViewModel>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[glib::object_subclass]
|
#[glib::object_subclass]
|
||||||
|
@ -47,7 +47,7 @@ glib::wrapper! {
|
||||||
impl DayDetailView {
|
impl DayDetailView {
|
||||||
pub fn new(view_model: DayDetailViewModel) -> Self {
|
pub fn new(view_model: DayDetailViewModel) -> Self {
|
||||||
let s: Self = Object::builder().build();
|
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);
|
s.append(&s.imp().container);
|
||||||
|
|
||||||
|
@ -57,18 +57,26 @@ impl DayDetailView {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self) {
|
fn view(&self) {
|
||||||
self.imp()
|
let view_model = self.imp().view_model.borrow();
|
||||||
.container
|
let view_model = view_model
|
||||||
.swap(&DayDetail::new(self.imp().view_model.borrow().clone(), {
|
.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();
|
let s = self.clone();
|
||||||
move || s.edit()
|
move || s.edit()
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn edit(&self) {
|
fn edit(&self) {
|
||||||
self.imp()
|
let view_model = self.imp().view_model.borrow();
|
||||||
.container
|
let view_model = view_model
|
||||||
.swap(&DayEdit::new(self.imp().view_model.borrow().clone(), {
|
.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();
|
let s = self.clone();
|
||||||
move || s.view()
|
move || s.view()
|
||||||
}));
|
}));
|
||||||
|
|
Loading…
Reference in New Issue