DayDetailViewModel now ignores records and directly retrieves data from App
This is preparatory work. Having the view model directly retrieve data both adds a degree of symmetry (it both gets data from and sends data to the app) and makes it possible for the view model to refresh itself when needing to revert data or after saving data.
This commit is contained in:
parent
304008c674
commit
c1e797f3ae
|
@ -136,20 +136,19 @@ impl AppWindow {
|
||||||
fn show_historical_view(&self, records: Vec<Record<TraxRecord>>) {
|
fn show_historical_view(&self, records: Vec<Record<TraxRecord>>) {
|
||||||
let view = View::Historical(HistoricalView::new(self.app.clone(), records, {
|
let view = View::Historical(HistoricalView::new(self.app.clone(), records, {
|
||||||
let s = self.clone();
|
let s = self.clone();
|
||||||
Rc::new(move |date, records| {
|
Rc::new(move |date, _records| {
|
||||||
let layout = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
let s = s.clone();
|
||||||
layout.append(&adw::HeaderBar::new());
|
glib::spawn_future_local(async move {
|
||||||
// layout.append(&DayDetailView::new(date, records, s.app.clone()));
|
let layout = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
||||||
layout.append(&DayDetailView::new(DayDetailViewModel::new(
|
layout.append(&adw::HeaderBar::new());
|
||||||
date,
|
let view_model = DayDetailViewModel::new(date, s.app.clone()).await;
|
||||||
records,
|
layout.append(&DayDetailView::new(view_model));
|
||||||
s.app.clone(),
|
let page = &adw::NavigationPage::builder()
|
||||||
)));
|
.title(date.format("%Y-%m-%d").to_string())
|
||||||
let page = &adw::NavigationPage::builder()
|
.child(&layout)
|
||||||
.title(date.format("%Y-%m-%d").to_string())
|
.build();
|
||||||
.child(&layout)
|
s.navigation.push(page);
|
||||||
.build();
|
});
|
||||||
s.navigation.push(page);
|
|
||||||
})
|
})
|
||||||
}));
|
}));
|
||||||
self.swap_main(view);
|
self.swap_main(view);
|
||||||
|
|
|
@ -85,7 +85,9 @@ pub struct DayDetailViewModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DayDetailViewModel {
|
impl DayDetailViewModel {
|
||||||
pub fn new(date: chrono::NaiveDate, records: Vec<Record<TraxRecord>>, app: App) -> Self {
|
pub async fn new(date: chrono::NaiveDate, app: App) -> Self {
|
||||||
|
let records = app.records(date, date).await.unwrap();
|
||||||
|
|
||||||
let (weight_records, records): (Vec<Record<TraxRecord>>, Vec<Record<TraxRecord>>) =
|
let (weight_records, records): (Vec<Record<TraxRecord>>, Vec<Record<TraxRecord>>) =
|
||||||
records.into_iter().partition(|r| r.data.is_weight());
|
records.into_iter().partition(|r| r.data.is_weight());
|
||||||
let (step_records, records): (Vec<Record<TraxRecord>>, Vec<Record<TraxRecord>>) =
|
let (step_records, records): (Vec<Record<TraxRecord>>, Vec<Record<TraxRecord>>) =
|
||||||
|
|
|
@ -59,27 +59,28 @@ impl ObjectSubclass for HistoricalViewPrivate {
|
||||||
factory.connect_bind({
|
factory.connect_bind({
|
||||||
let app = s.app.clone();
|
let app = s.app.clone();
|
||||||
move |_, list_item| {
|
move |_, list_item| {
|
||||||
let records = list_item
|
let app = app.clone();
|
||||||
.downcast_ref::<gtk::ListItem>()
|
let list_item = list_item.clone();
|
||||||
.expect("should be a ListItem")
|
glib::spawn_future_local(async move {
|
||||||
.item()
|
let records = list_item
|
||||||
.and_downcast::<DayRecords>()
|
.downcast_ref::<gtk::ListItem>()
|
||||||
.expect("should be a DaySummary");
|
.expect("should be a ListItem")
|
||||||
|
.item()
|
||||||
|
.and_downcast::<DayRecords>()
|
||||||
|
.expect("should be a DaySummary");
|
||||||
|
|
||||||
let summary = list_item
|
let summary = list_item
|
||||||
.downcast_ref::<gtk::ListItem>()
|
.downcast_ref::<gtk::ListItem>()
|
||||||
.expect("should be a ListItem")
|
.expect("should be a ListItem")
|
||||||
.child()
|
.child()
|
||||||
.and_downcast::<DaySummary>()
|
.and_downcast::<DaySummary>()
|
||||||
.expect("should be a DaySummary");
|
.expect("should be a DaySummary");
|
||||||
|
|
||||||
if let Some(app) = app.borrow().clone() {
|
if let Some(app) = app.borrow().clone() {
|
||||||
summary.set_data(DayDetailViewModel::new(
|
let view_model = DayDetailViewModel::new(records.date(), app.clone()).await;
|
||||||
records.date(),
|
summary.set_data(view_model);
|
||||||
records.records(),
|
}
|
||||||
app.clone(),
|
});
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue