2023-12-22 19:54:38 +00:00
|
|
|
/*
|
|
|
|
Copyright 2023, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
|
|
|
|
|
|
|
This file is part of FitnessTrax.
|
|
|
|
|
|
|
|
FitnessTrax is free software: you can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
General Public License as published by the Free Software Foundation, either version 3 of the
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
FitnessTrax is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
|
|
|
|
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2024-01-20 22:04:20 +00:00
|
|
|
use crate::{
|
|
|
|
app::App, components::DaySummary, types::DayInterval, view_models::DayDetailViewModel,
|
|
|
|
};
|
2024-01-20 19:35:10 +00:00
|
|
|
use chrono::NaiveDate;
|
2023-12-28 02:49:44 +00:00
|
|
|
use emseries::Record;
|
2023-12-25 00:13:49 +00:00
|
|
|
use ft_core::TraxRecord;
|
2023-12-22 19:54:38 +00:00
|
|
|
use glib::Object;
|
|
|
|
use gtk::{prelude::*, subclass::prelude::*};
|
2023-12-25 05:36:13 +00:00
|
|
|
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
2023-12-22 19:54:38 +00:00
|
|
|
|
|
|
|
/// The historical view will show a window into the main database. It will show some version of
|
|
|
|
/// daily summaries, daily details, and will provide all functions the user may need for editing
|
|
|
|
/// records.
|
2023-12-28 02:49:44 +00:00
|
|
|
pub struct HistoricalViewPrivate {
|
2024-01-20 22:04:20 +00:00
|
|
|
app: Rc<RefCell<Option<App>>>,
|
|
|
|
time_window: Rc<RefCell<DayInterval>>,
|
2023-12-28 19:34:32 +00:00
|
|
|
list_view: gtk::ListView,
|
2023-12-28 02:49:44 +00:00
|
|
|
}
|
2023-12-22 19:54:38 +00:00
|
|
|
|
|
|
|
#[glib::object_subclass]
|
|
|
|
impl ObjectSubclass for HistoricalViewPrivate {
|
|
|
|
const NAME: &'static str = "HistoricalView";
|
|
|
|
type Type = HistoricalView;
|
|
|
|
type ParentType = gtk::Box;
|
|
|
|
|
|
|
|
fn new() -> Self {
|
2023-12-28 19:34:32 +00:00
|
|
|
let factory = gtk::SignalListItemFactory::new();
|
|
|
|
factory.connect_setup(move |_, list_item| {
|
|
|
|
list_item
|
|
|
|
.downcast_ref::<gtk::ListItem>()
|
|
|
|
.expect("should be a ListItem")
|
|
|
|
.set_child(Some(&DaySummary::new()));
|
|
|
|
});
|
|
|
|
|
2024-01-20 22:04:20 +00:00
|
|
|
let s = Self {
|
|
|
|
app: Rc::new(RefCell::new(None)),
|
|
|
|
time_window: Rc::new(RefCell::new(DayInterval::default())),
|
2023-12-28 19:34:32 +00:00
|
|
|
list_view: gtk::ListView::builder()
|
|
|
|
.factory(&factory)
|
|
|
|
.single_click_activate(true)
|
|
|
|
.build(),
|
2024-01-20 22:04:20 +00:00
|
|
|
};
|
2024-01-26 14:53:42 +00:00
|
|
|
|
2024-01-20 22:04:20 +00:00
|
|
|
factory.connect_bind({
|
|
|
|
let app = s.app.clone();
|
|
|
|
move |_, list_item| {
|
2024-01-29 15:22:21 +00:00
|
|
|
let date = list_item
|
2024-01-20 22:04:20 +00:00
|
|
|
.downcast_ref::<gtk::ListItem>()
|
|
|
|
.expect("should be a ListItem")
|
|
|
|
.item()
|
2024-01-29 15:22:21 +00:00
|
|
|
.and_downcast::<Date>()
|
|
|
|
.expect("should be a Date");
|
2024-01-20 22:04:20 +00:00
|
|
|
|
|
|
|
let summary = list_item
|
|
|
|
.downcast_ref::<gtk::ListItem>()
|
|
|
|
.expect("should be a ListItem")
|
|
|
|
.child()
|
|
|
|
.and_downcast::<DaySummary>()
|
|
|
|
.expect("should be a DaySummary");
|
|
|
|
|
|
|
|
if let Some(app) = app.borrow().clone() {
|
2024-01-29 15:22:21 +00:00
|
|
|
let _ = glib::spawn_future_local(async move {
|
|
|
|
println!(
|
|
|
|
"setting up a DayDetailViewModel for {}",
|
|
|
|
date.date().format("%Y-%m-%d")
|
|
|
|
);
|
|
|
|
let view_model = DayDetailViewModel::new(date.date(), app.clone()).await;
|
|
|
|
summary.set_data(view_model);
|
|
|
|
});
|
2024-01-20 22:04:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
s
|
2023-12-22 19:54:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ObjectImpl for HistoricalViewPrivate {}
|
|
|
|
impl WidgetImpl for HistoricalViewPrivate {}
|
|
|
|
impl BoxImpl for HistoricalViewPrivate {}
|
|
|
|
|
|
|
|
glib::wrapper! {
|
2023-12-22 22:32:45 +00:00
|
|
|
pub struct HistoricalView(ObjectSubclass<HistoricalViewPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
|
2023-12-22 19:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HistoricalView {
|
2024-01-29 15:22:21 +00:00
|
|
|
pub fn new<SelectFn>(app: App, interval: DayInterval, on_select_day: Rc<SelectFn>) -> Self
|
2023-12-25 05:36:13 +00:00
|
|
|
where
|
2023-12-26 15:45:50 +00:00
|
|
|
SelectFn: Fn(chrono::NaiveDate, Vec<Record<TraxRecord>>) + 'static,
|
2023-12-25 05:36:13 +00:00
|
|
|
{
|
2023-12-22 19:54:38 +00:00
|
|
|
let s: Self = Object::builder().build();
|
2023-12-22 22:32:45 +00:00
|
|
|
s.set_orientation(gtk::Orientation::Vertical);
|
2023-12-25 05:36:13 +00:00
|
|
|
s.set_css_classes(&["historical"]);
|
2023-12-22 19:54:38 +00:00
|
|
|
|
2024-01-20 22:04:20 +00:00
|
|
|
*s.imp().app.borrow_mut() = Some(app);
|
|
|
|
|
2024-01-29 15:22:21 +00:00
|
|
|
let mut model = gio::ListStore::new::<Date>();
|
|
|
|
model.extend(interval.days().map(|d| Date::new(d)));
|
2023-12-28 19:34:32 +00:00
|
|
|
s.imp()
|
|
|
|
.list_view
|
|
|
|
.set_model(Some(>k::NoSelection::new(Some(model))));
|
2023-12-22 22:32:45 +00:00
|
|
|
|
2023-12-28 19:34:32 +00:00
|
|
|
s.imp().list_view.connect_activate({
|
2023-12-25 05:36:13 +00:00
|
|
|
let on_select_day = on_select_day.clone();
|
|
|
|
move |s, idx| {
|
2024-01-26 14:53:42 +00:00
|
|
|
// This gets triggered whenever the user clicks on an item on the list.
|
2023-12-25 05:36:13 +00:00
|
|
|
let item = s.model().unwrap().item(idx).unwrap();
|
2024-01-29 15:22:21 +00:00
|
|
|
let date = item.downcast_ref::<Date>().unwrap();
|
|
|
|
on_select_day(date.date(), vec![]);
|
2023-12-25 05:36:13 +00:00
|
|
|
}
|
2023-12-24 17:00:12 +00:00
|
|
|
});
|
2023-12-22 22:32:45 +00:00
|
|
|
|
2023-12-28 19:34:32 +00:00
|
|
|
s.append(&s.imp().list_view);
|
2023-12-22 22:32:45 +00:00
|
|
|
|
|
|
|
s
|
|
|
|
}
|
2023-12-28 19:34:32 +00:00
|
|
|
|
|
|
|
pub fn time_window(&self) -> DayInterval {
|
|
|
|
self.imp().time_window.borrow().clone()
|
|
|
|
}
|
2023-12-22 22:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
2024-01-29 15:22:21 +00:00
|
|
|
pub struct DatePrivate {
|
2023-12-22 22:32:45 +00:00
|
|
|
date: RefCell<chrono::NaiveDate>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
2024-01-29 15:22:21 +00:00
|
|
|
impl ObjectSubclass for DatePrivate {
|
|
|
|
const NAME: &'static str = "Date";
|
|
|
|
type Type = Date;
|
2023-12-22 22:32:45 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 15:22:21 +00:00
|
|
|
impl ObjectImpl for DatePrivate {}
|
2023-12-22 22:32:45 +00:00
|
|
|
|
|
|
|
glib::wrapper! {
|
2024-01-29 15:22:21 +00:00
|
|
|
pub struct Date(ObjectSubclass<DatePrivate>);
|
2023-12-22 22:32:45 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 15:22:21 +00:00
|
|
|
impl Date {
|
|
|
|
pub fn new(date: chrono::NaiveDate) -> Self {
|
2023-12-22 22:32:45 +00:00
|
|
|
let s: Self = Object::builder().build();
|
|
|
|
*s.imp().date.borrow_mut() = date;
|
2023-12-22 19:54:38 +00:00
|
|
|
s
|
|
|
|
}
|
2023-12-22 22:32:45 +00:00
|
|
|
|
|
|
|
pub fn date(&self) -> chrono::NaiveDate {
|
2024-01-29 15:22:21 +00:00
|
|
|
self.imp().date.borrow().clone()
|
2023-12-28 02:49:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-25 00:13:49 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::GroupedRecords;
|
2024-01-20 19:35:10 +00:00
|
|
|
use crate::types::DayInterval;
|
2023-12-28 17:44:42 +00:00
|
|
|
use chrono::{FixedOffset, NaiveDate, TimeZone};
|
2023-12-25 00:13:49 +00:00
|
|
|
use dimensioned::si::{KG, M, S};
|
2023-12-28 17:44:42 +00:00
|
|
|
use emseries::{Record, RecordId};
|
2023-12-25 00:13:49 +00:00
|
|
|
use ft_core::{Steps, TimeDistance, TraxRecord, Weight};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn groups_records() {
|
|
|
|
let records = vec![
|
2023-12-27 22:14:47 +00:00
|
|
|
Record {
|
|
|
|
id: RecordId::default(),
|
|
|
|
data: TraxRecord::Steps(Steps {
|
|
|
|
date: NaiveDate::from_ymd_opt(2023, 10, 13).unwrap(),
|
|
|
|
count: 1500,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Record {
|
|
|
|
id: RecordId::default(),
|
|
|
|
data: TraxRecord::Weight(Weight {
|
|
|
|
date: NaiveDate::from_ymd_opt(2023, 10, 13).unwrap(),
|
|
|
|
weight: 85. * KG,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Record {
|
|
|
|
id: RecordId::default(),
|
|
|
|
data: TraxRecord::Weight(Weight {
|
|
|
|
date: NaiveDate::from_ymd_opt(2023, 10, 14).unwrap(),
|
|
|
|
weight: 86. * KG,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Record {
|
|
|
|
id: RecordId::default(),
|
|
|
|
data: TraxRecord::BikeRide(TimeDistance {
|
|
|
|
datetime: FixedOffset::west_opt(10 * 60 * 60)
|
|
|
|
.unwrap()
|
|
|
|
.with_ymd_and_hms(2019, 6, 15, 12, 0, 0)
|
|
|
|
.unwrap(),
|
|
|
|
distance: Some(1000. * M),
|
|
|
|
duration: Some(150. * S),
|
|
|
|
comments: Some("Test Comments".to_owned()),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Record {
|
|
|
|
id: RecordId::default(),
|
|
|
|
data: TraxRecord::BikeRide(TimeDistance {
|
|
|
|
datetime: FixedOffset::west_opt(10 * 60 * 60)
|
|
|
|
.unwrap()
|
|
|
|
.with_ymd_and_hms(2019, 6, 15, 23, 0, 0)
|
|
|
|
.unwrap(),
|
|
|
|
distance: Some(1000. * M),
|
|
|
|
duration: Some(150. * S),
|
|
|
|
comments: Some("Test Comments".to_owned()),
|
|
|
|
}),
|
|
|
|
},
|
2023-12-25 00:13:49 +00:00
|
|
|
];
|
|
|
|
|
2024-01-20 19:35:10 +00:00
|
|
|
let groups = GroupedRecords::new(DayInterval {
|
|
|
|
start: NaiveDate::from_ymd_opt(2023, 10, 14).unwrap(),
|
|
|
|
end: NaiveDate::from_ymd_opt(2023, 10, 14).unwrap(),
|
|
|
|
})
|
|
|
|
.with_data(records)
|
|
|
|
.data;
|
2023-12-25 00:13:49 +00:00
|
|
|
assert_eq!(groups.len(), 3);
|
|
|
|
}
|
2023-12-22 19:54:38 +00:00
|
|
|
}
|