Save new information to the day detail view and to the historical view

This commit is contained in:
Savanni D'Gerinel 2023-12-29 09:24:37 -05:00
parent b5dcee3737
commit 7bd4885b09
2 changed files with 35 additions and 8 deletions

View File

@ -113,6 +113,14 @@ impl AppWindow {
s.load_records(); s.load_records();
s.navigation.connect_popped({
let s = s.clone();
move |_, _| match *s.current_view.borrow() {
View::Historical(_) => s.load_records(),
_ => {}
}
});
s s
} }

View File

@ -16,6 +16,7 @@ You should have received a copy of the GNU General Public License along with Fit
// use chrono::NaiveDate; // use chrono::NaiveDate;
// use ft_core::TraxRecord; // use ft_core::TraxRecord;
use chrono::{Local, NaiveDate};
use dimensioned::si; use dimensioned::si;
use emseries::Record; use emseries::Record;
use ft_core::{RecordType, TimeDistance, TraxRecord, Weight}; use ft_core::{RecordType, TimeDistance, TraxRecord, Weight};
@ -133,14 +134,14 @@ glib::wrapper! {
} }
impl DayDetail { impl DayDetail {
pub fn new<SaveRecordFn, UpdateRecordFn>( pub fn new<PutRecordFn, UpdateRecordFn>(
date: chrono::NaiveDate, date: chrono::NaiveDate,
records: Vec<Record<TraxRecord>>, records: Vec<Record<TraxRecord>>,
on_save_record: SaveRecordFn, on_put_record: PutRecordFn,
on_update_record: UpdateRecordFn, on_update_record: UpdateRecordFn,
) -> Self ) -> Self
where where
SaveRecordFn: Fn(TraxRecord) + 'static, PutRecordFn: Fn(TraxRecord) + 'static,
UpdateRecordFn: Fn(Record<TraxRecord>) + 'static, UpdateRecordFn: Fn(Record<TraxRecord>) + 'static,
{ {
let s: Self = Object::builder().build(); let s: Self = Object::builder().build();
@ -168,14 +169,14 @@ impl DayDetail {
}); });
let weight_view = match weight_record { let weight_view = match weight_record {
Some((id, data)) => WeightView::new(Some(data.clone()), move |weight| { Some((id, data)) => WeightView::new(date.clone(), Some(data.clone()), move |weight| {
on_update_record(Record { on_update_record(Record {
id: id.clone(), id: id.clone(),
data: TraxRecord::Weight(Weight { date, weight }), data: TraxRecord::Weight(Weight { date, weight }),
}) })
}), }),
None => WeightView::new(None, move |weight| { None => WeightView::new(date.clone(), None, move |weight| {
on_save_record(TraxRecord::Weight(Weight { date, weight })); on_put_record(TraxRecord::Weight(Weight { date, weight }));
}), }),
}; };
s.append(&weight_view); s.append(&weight_view);
@ -220,6 +221,7 @@ impl DayDetail {
} }
pub struct WeightViewPrivate { pub struct WeightViewPrivate {
date: RefCell<NaiveDate>,
record: RefCell<Option<Weight>>, record: RefCell<Option<Weight>>,
view: RefCell<gtk::Label>, view: RefCell<gtk::Label>,
edit: RefCell<gtk::Entry>, edit: RefCell<gtk::Entry>,
@ -239,6 +241,7 @@ impl Default for WeightViewPrivate {
let current = view.clone(); let current = view.clone();
Self { Self {
date: RefCell::new(Local::now().date_naive()),
record: RefCell::new(None), record: RefCell::new(None),
view: RefCell::new(view), view: RefCell::new(view),
edit: RefCell::new(edit), edit: RefCell::new(edit),
@ -264,13 +267,18 @@ glib::wrapper! {
} }
impl WeightView { impl WeightView {
pub fn new<OnEditFinished>(weight: Option<Weight>, on_edit_finished: OnEditFinished) -> Self pub fn new<OnEditFinished>(
date: NaiveDate,
weight: Option<Weight>,
on_edit_finished: OnEditFinished,
) -> Self
where where
OnEditFinished: Fn(si::Kilogram<f64>) + 'static, OnEditFinished: Fn(si::Kilogram<f64>) + 'static,
{ {
let s: Self = Object::builder().build(); let s: Self = Object::builder().build();
*s.imp().on_edit_finished.borrow_mut() = Box::new(on_edit_finished); *s.imp().on_edit_finished.borrow_mut() = Box::new(on_edit_finished);
*s.imp().date.borrow_mut() = date;
*s.imp().record.borrow_mut() = weight; *s.imp().record.borrow_mut() = weight;
s.view(); s.view();
@ -324,9 +332,20 @@ impl WeightView {
if *self.imp().current.borrow() == *edit { if *self.imp().current.borrow() == *edit {
let w = edit.buffer().text().parse::<f64>().unwrap(); let w = edit.buffer().text().parse::<f64>().unwrap();
self.imp().on_edit_finished.borrow()(w * si::KG); self.imp().on_edit_finished.borrow()(w * si::KG);
self.view();
let mut record = self.imp().record.borrow_mut();
match *record {
Some(ref mut record) => record.weight = w * si::KG,
None => {
*record = Some(Weight {
date: self.imp().date.borrow().clone(),
weight: w * si::KG,
})
} }
} }
}
self.view();
}
} }
#[derive(Default)] #[derive(Default)]