Correctly set the date range picker when initializing the historical view

This commit is contained in:
Savanni D'Gerinel 2024-02-19 16:47:10 -05:00
parent 55b6327d42
commit d70ca08db2
2 changed files with 10 additions and 11 deletions

View File

@ -65,12 +65,12 @@ impl DateRangePicker {
*self.imp().on_search.borrow_mut() = Box::new(f); *self.imp().on_search.borrow_mut() = Box::new(f);
} }
fn set_interval(&self, start: chrono::NaiveDate, end: chrono::NaiveDate) { pub fn set_interval(&self, start: chrono::NaiveDate, end: chrono::NaiveDate) {
self.imp().start.set_date(start); self.imp().start.set_date(start);
self.imp().end.set_date(end); self.imp().end.set_date(end);
} }
fn interval(&self) -> DayInterval { pub fn interval(&self) -> DayInterval {
DayInterval { DayInterval {
start: self.imp().start.date(), start: self.imp().start.date(),
end: self.imp().end.date(), end: self.imp().end.date(),

View File

@ -30,6 +30,7 @@ use std::{cell::RefCell, rc::Rc};
pub struct HistoricalViewPrivate { pub struct HistoricalViewPrivate {
app: Rc<RefCell<Option<App>>>, app: Rc<RefCell<Option<App>>>,
list_view: gtk::ListView, list_view: gtk::ListView,
date_range_picker: DateRangePicker,
} }
#[glib::object_subclass] #[glib::object_subclass]
@ -47,12 +48,15 @@ impl ObjectSubclass for HistoricalViewPrivate {
.set_child(Some(&DaySummary::new())); .set_child(Some(&DaySummary::new()));
}); });
let date_range_picker = DateRangePicker::default();
let s = Self { let s = Self {
app: Rc::new(RefCell::new(None)), app: Rc::new(RefCell::new(None)),
list_view: gtk::ListView::builder() list_view: gtk::ListView::builder()
.factory(&factory) .factory(&factory)
.single_click_activate(true) .single_click_activate(true)
.build(), .build(),
date_range_picker,
}; };
factory.connect_bind({ factory.connect_bind({
@ -106,17 +110,11 @@ impl HistoricalView {
*s.imp().app.borrow_mut() = Some(app); *s.imp().app.borrow_mut() = Some(app);
let date_range_picker = DateRangePicker::default(); s.imp().date_range_picker.connect_on_search({
date_range_picker.connect_on_search({
let s = s.clone(); let s = s.clone();
move |interval| s.set_interval(interval) move |interval| s.set_interval(interval)
}); });
s.set_interval(interval);
let mut model = gio::ListStore::new::<Date>();
model.extend(interval.days().map(Date::new));
s.imp()
.list_view
.set_model(Some(&gtk::NoSelection::new(Some(model))));
s.imp().list_view.connect_activate({ s.imp().list_view.connect_activate({
let on_select_day = on_select_day.clone(); let on_select_day = on_select_day.clone();
@ -135,7 +133,7 @@ impl HistoricalView {
.hscrollbar_policy(gtk::PolicyType::Never) .hscrollbar_policy(gtk::PolicyType::Never)
.build(); .build();
s.append(&date_range_picker); s.append(&s.imp().date_range_picker);
s.append(&scroller); s.append(&scroller);
s s
@ -147,6 +145,7 @@ impl HistoricalView {
self.imp() self.imp()
.list_view .list_view
.set_model(Some(&gtk::NoSelection::new(Some(model)))); .set_model(Some(&gtk::NoSelection::new(Some(model))));
self.imp().date_range_picker.set_interval(interval.start, interval.end);
} }
} }