Create a swappable UI component #160

Merged
savanni merged 7 commits from fitnesstrax/swappable into main 2024-01-18 12:56:56 +00:00
2 changed files with 38 additions and 16 deletions
Showing only changes of commit c075b7ed6e - Show all commits

View File

@ -275,26 +275,40 @@ impl DayEdit {
s.set_orientation(gtk::Orientation::Vertical);
s.set_hexpand(true);
/*, move |weight| {
on_update_record(Record {
id: id.clone(),
data: ft_core::TraxRecord::Weight(ft_core::Weight { date, weight }),
})
}*/
/*, move |weight| {
on_put_record(ft_core::TraxRecord::Weight(ft_core::Weight {
date,
weight,
}));
}*/
s.append(
&ActionGroup::builder()
.primary_action("Save", {
let s = s.clone();
let records = records.clone();
move || {
println!("weight value: {:?}", s.imp().weight.value());
println!("saving to database");
let weight_record = records.iter().find_map(|record| match record {
Record {
id,
data: ft_core::TraxRecord::Weight(w),
} => Some((id, w)),
_ => None,
});
let weight = s.imp().weight.value();
if let Some(weight) = weight {
match weight_record {
Some((id, _)) => on_update_record(Record {
id: id.clone(),
data: ft_core::TraxRecord::Weight(ft_core::Weight {
date,
weight,
}),
}),
None => {
on_put_record(ft_core::TraxRecord::Weight(ft_core::Weight {
date,
weight,
}))
}
}
};
}
})
.secondary_action("Cancel", on_cancel)

View File

@ -54,6 +54,7 @@ impl DayDetailView {
*s.imp().date.borrow_mut() = date;
*s.imp().records.borrow_mut() = records;
*s.imp().app.borrow_mut() = Some(app);
s.append(&s.imp().container);
@ -105,6 +106,7 @@ impl DayDetailView {
}
fn on_put_record(&self) -> Box<dyn Fn(ft_core::TraxRecord)> {
let s = self.clone();
let app = self.imp().app.clone();
Box::new(move |record| {
let app = app.clone();
@ -118,23 +120,29 @@ impl DayDetailView {
}
}
});
s.view();
})
}
fn on_update_record(&self) -> Box<dyn Fn(Record<ft_core::TraxRecord>)> {
let s = self.clone();
let app = self.imp().app.clone();
Box::new(move |record| {
let app = app.clone();
glib::spawn_future_local({
async move {
println!("record: {:?}", record);
match &*app.borrow() {
Some(app) => {
let _ = app.update_record(record).await;
}
None => {}
None => {
println!("no app!");
}
}
}
});
s.view();
})
}
}