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
1 changed files with 54 additions and 36 deletions
Showing only changes of commit b5dcee3737 - Show all commits

View File

@ -27,6 +27,7 @@ use std::{cell::RefCell, collections::HashMap, rc::Rc};
/// records. /// records.
pub struct HistoricalViewPrivate { pub struct HistoricalViewPrivate {
time_window: RefCell<DayInterval>, time_window: RefCell<DayInterval>,
list_view: gtk::ListView,
} }
#[glib::object_subclass] #[glib::object_subclass]
@ -36,35 +37,6 @@ impl ObjectSubclass for HistoricalViewPrivate {
type ParentType = gtk::Box; type ParentType = gtk::Box;
fn new() -> Self { fn new() -> Self {
Self {
time_window: RefCell::new(DayInterval::default()),
}
}
}
impl ObjectImpl for HistoricalViewPrivate {}
impl WidgetImpl for HistoricalViewPrivate {}
impl BoxImpl for HistoricalViewPrivate {}
glib::wrapper! {
pub struct HistoricalView(ObjectSubclass<HistoricalViewPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
}
impl HistoricalView {
pub fn new<SelectFn>(records: Vec<Record<TraxRecord>>, on_select_day: Rc<SelectFn>) -> Self
where
SelectFn: Fn(chrono::NaiveDate, Vec<Record<TraxRecord>>) + 'static,
{
let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Vertical);
s.set_css_classes(&["historical"]);
let grouped_records =
GroupedRecords::new((*s.imp().time_window.borrow()).clone()).with_data(records);
let mut model = gio::ListStore::new::<DayRecords>();
model.extend(grouped_records.items());
let factory = gtk::SignalListItemFactory::new(); let factory = gtk::SignalListItemFactory::new();
factory.connect_setup(move |_, list_item| { factory.connect_setup(move |_, list_item| {
list_item list_item
@ -91,12 +63,43 @@ impl HistoricalView {
summary.set_data(records.date(), records.records()); summary.set_data(records.date(), records.records());
}); });
let lst = gtk::ListView::builder() Self {
.model(&gtk::NoSelection::new(Some(model))) time_window: RefCell::new(DayInterval::default()),
.factory(&factory) list_view: gtk::ListView::builder()
.single_click_activate(true) .factory(&factory)
.build(); .single_click_activate(true)
lst.connect_activate({ .build(),
}
}
}
impl ObjectImpl for HistoricalViewPrivate {}
impl WidgetImpl for HistoricalViewPrivate {}
impl BoxImpl for HistoricalViewPrivate {}
glib::wrapper! {
pub struct HistoricalView(ObjectSubclass<HistoricalViewPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
}
impl HistoricalView {
pub fn new<SelectFn>(records: Vec<Record<TraxRecord>>, on_select_day: Rc<SelectFn>) -> Self
where
SelectFn: Fn(chrono::NaiveDate, Vec<Record<TraxRecord>>) + 'static,
{
let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Vertical);
s.set_css_classes(&["historical"]);
let grouped_records =
GroupedRecords::new((*s.imp().time_window.borrow()).clone()).with_data(records);
let mut model = gio::ListStore::new::<DayRecords>();
model.extend(grouped_records.items());
s.imp()
.list_view
.set_model(Some(&gtk::NoSelection::new(Some(model))));
s.imp().list_view.connect_activate({
let on_select_day = on_select_day.clone(); let on_select_day = on_select_day.clone();
move |s, idx| { move |s, idx| {
// This gets triggered whenever the user clicks on an item on the list. What we // This gets triggered whenever the user clicks on an item on the list. What we
@ -108,10 +111,25 @@ impl HistoricalView {
} }
}); });
s.append(&lst); s.append(&s.imp().list_view);
s s
} }
pub fn set_records(&self, records: Vec<Record<TraxRecord>>) {
println!("set_records: {:?}", records);
let grouped_records =
GroupedRecords::new((self.imp().time_window.borrow()).clone()).with_data(records);
let mut model = gio::ListStore::new::<DayRecords>();
model.extend(grouped_records.items());
self.imp()
.list_view
.set_model(Some(&gtk::NoSelection::new(Some(model))));
}
pub fn time_window(&self) -> DayInterval {
self.imp().time_window.borrow().clone()
}
} }
#[derive(Default)] #[derive(Default)]