Show and edit the details of a day #133

Merged
savanni merged 9 commits from fitnesstrax/day-detail into main 2023-12-29 16:01:36 +00:00
2 changed files with 35 additions and 8 deletions
Showing only changes of commit 7bd4885b09 - Show all commits

View File

@ -113,6 +113,14 @@ impl AppWindow {
s.load_records();
s.navigation.connect_popped({
let s = s.clone();
move |_, _| match *s.current_view.borrow() {
View::Historical(_) => s.load_records(),
_ => {}
}
});
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 ft_core::TraxRecord;
use chrono::{Local, NaiveDate};
use dimensioned::si;
use emseries::Record;
use ft_core::{RecordType, TimeDistance, TraxRecord, Weight};
@ -133,14 +134,14 @@ glib::wrapper! {
}
impl DayDetail {
pub fn new<SaveRecordFn, UpdateRecordFn>(
pub fn new<PutRecordFn, UpdateRecordFn>(
date: chrono::NaiveDate,
records: Vec<Record<TraxRecord>>,
on_save_record: SaveRecordFn,
on_put_record: PutRecordFn,
on_update_record: UpdateRecordFn,
) -> Self
where
SaveRecordFn: Fn(TraxRecord) + 'static,
PutRecordFn: Fn(TraxRecord) + 'static,
UpdateRecordFn: Fn(Record<TraxRecord>) + 'static,
{
let s: Self = Object::builder().build();
@ -168,14 +169,14 @@ impl DayDetail {
});
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 {
id: id.clone(),
data: TraxRecord::Weight(Weight { date, weight }),
})
}),
None => WeightView::new(None, move |weight| {
on_save_record(TraxRecord::Weight(Weight { date, weight }));
None => WeightView::new(date.clone(), None, move |weight| {
on_put_record(TraxRecord::Weight(Weight { date, weight }));
}),
};
s.append(&weight_view);
@ -220,6 +221,7 @@ impl DayDetail {
}
pub struct WeightViewPrivate {
date: RefCell<NaiveDate>,
record: RefCell<Option<Weight>>,
view: RefCell<gtk::Label>,
edit: RefCell<gtk::Entry>,
@ -239,6 +241,7 @@ impl Default for WeightViewPrivate {
let current = view.clone();
Self {
date: RefCell::new(Local::now().date_naive()),
record: RefCell::new(None),
view: RefCell::new(view),
edit: RefCell::new(edit),
@ -264,13 +267,18 @@ glib::wrapper! {
}
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
OnEditFinished: Fn(si::Kilogram<f64>) + 'static,
{
let s: Self = Object::builder().build();
*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.view();
@ -324,10 +332,21 @@ impl WeightView {
if *self.imp().current.borrow() == *edit {
let w = edit.buffer().text().parse::<f64>().unwrap();
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)]
pub struct TimeDistanceViewPrivate {