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
4 changed files with 54 additions and 34 deletions
Showing only changes of commit f422e233a1 - Show all commits

View File

@ -97,7 +97,7 @@ impl App {
.map_err(|_| AppError::Unhandled)
}
pub async fn save_record(&self, record: Record<TraxRecord>) -> Result<(), AppError> {
pub async fn update_record(&self, record: Record<TraxRecord>) -> Result<(), AppError> {
let db = self.database.clone();
self.runtime
.spawn_blocking(move || {

View File

@ -130,7 +130,18 @@ impl AppWindow {
Rc::new(move |date, records| {
let layout = gtk::Box::new(gtk::Orientation::Vertical, 0);
layout.append(&adw::HeaderBar::new());
layout.append(&DayDetail::new(date, records));
layout.append(&DayDetail::new(
date,
records,
{
let s = s.clone();
move |record| s.on_put_record(record)
},
{
let s = s.clone();
move |record| s.on_update_record(record)
},
));
let page = &adw::NavigationPage::builder()
.title(date.format("%Y-%m-%d").to_string())
.child(&layout)
@ -178,4 +189,22 @@ impl AppWindow {
}
});
}
fn on_put_record(&self, record: TraxRecord) {
glib::spawn_future_local({
let s = self.clone();
async move {
s.app.put_record(record).await;
}
});
}
fn on_update_record(&self, record: Record<TraxRecord>) {
glib::spawn_future_local({
let s = self.clone();
async move {
s.app.update_record(record).await;
}
});
}
}

View File

@ -133,7 +133,16 @@ glib::wrapper! {
}
impl DayDetail {
pub fn new(date: chrono::NaiveDate, records: Vec<Record<TraxRecord>>) -> Self {
pub fn new<SaveRecordFn, UpdateRecordFn>(
date: chrono::NaiveDate,
records: Vec<Record<TraxRecord>>,
on_save_record: SaveRecordFn,
on_update_record: UpdateRecordFn,
) -> Self
where
SaveRecordFn: Fn(TraxRecord) + 'static,
UpdateRecordFn: Fn(Record<TraxRecord>) + 'static,
{
let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Vertical);
@ -159,17 +168,14 @@ impl DayDetail {
});
let weight_view = match weight_record {
Some((id, record)) => WeightView::new(Some(record.clone()), move |weight| {
println!(
"on_blur on the weight view. Need to record {:?}, {:?}",
id, weight
);
Some((id, data)) => WeightView::new(Some(data.clone()), move |weight| {
on_update_record(Record {
id: id.clone(),
data: TraxRecord::Weight(Weight { date, weight }),
})
}),
None => WeightView::new(None, |weight| {
println!(
"on_blur on the weight view. Need to create a new record for {:?}",
weight
);
None => WeightView::new(None, move |weight| {
on_save_record(TraxRecord::Weight(Weight { date, weight }));
}),
};
s.append(&weight_view);

View File

@ -155,6 +155,9 @@ impl DayRecords {
}
}
// This isn't feeling quite right. DayRecords is a glib object, but I'm not sure that I want to
// really be passing that around. It seems not generic enough. I feel like this whole grouped
// records thing can be made more generic.
struct GroupedRecords {
interval: DayInterval,
data: HashMap<NaiveDate, DayRecords>,
@ -184,12 +187,6 @@ impl GroupedRecords {
}
fn items<'a>(&'a self) -> impl Iterator<Item = DayRecords> + 'a {
/*
GroupedRecordIterator {
interval: self.interval.clone(),
data: &self.data,
}
*/
self.interval.days().map(|date| {
self.data
.get(&date)
@ -199,21 +196,9 @@ impl GroupedRecords {
}
}
/*
struct GroupedRecordIterator<'a> {
interval: DayInterval,
data: &'a HashMap<NaiveDate, DayRecords>,
}
impl <'a> Iterator for GroupedRecordIterator<'a> {
type Item = &'a DayRecords;
fn next(&mut self) -> Option<&'a DayRecords> {
}
}
*/
// This interval doesn't feel right, either. The idea that I have a specific interval type for just
// NaiveDate is odd. This should be genericized, as should the iterator. Also, it shouldn't live
// here, but in utilities.
#[derive(Clone, Debug, PartialEq, Eq)]
struct DayInterval {
start: NaiveDate,