Open and style the day detail view and add it to the navigation stack

This commit is contained in:
Savanni D'Gerinel 2023-12-25 00:36:13 -05:00
parent d269924827
commit 383f809191
6 changed files with 141 additions and 44 deletions

View File

@ -2,32 +2,33 @@
margin: 64px; margin: 64px;
} }
.welcome-title { .welcome__title {
font-size: larger; font-size: larger;
padding: 8px; padding: 8px;
} }
.welcome-content { .welcome__content {
padding: 8px; padding: 8px;
} }
.welcome-footer { .welcome__footer {
} }
.dialog-row { .historical {
margin: 8px 0px 8px 0px; margin: 32px;
border-radius: 8px;
}
.day-summary {
padding: 8px; padding: 8px;
} }
.daysummary { .day-summary__date {
padding: 8px;
}
.daysummary-date {
font-size: larger; font-size: larger;
margin-bottom: 8px; margin-bottom: 8px;
} }
.daysummary-weight { .day-summary__weight {
margin: 4px; margin: 4px;
} }

View File

@ -16,10 +16,13 @@ You should have received a copy of the GNU General Public License along with Fit
use crate::{ use crate::{
app::App, app::App,
components::DayDetail,
views::{HistoricalView, PlaceholderView, View, WelcomeView}, views::{HistoricalView, PlaceholderView, View, WelcomeView},
}; };
use adw::prelude::*; use adw::prelude::*;
use chrono::{Duration, Local}; use chrono::{Duration, Local};
use emseries::Record;
use ft_core::TraxRecord;
use gio::resources_lookup_data; use gio::resources_lookup_data;
use gtk::STYLE_PROVIDER_PRIORITY_USER; use gtk::STYLE_PROVIDER_PRIORITY_USER;
use std::{cell::RefCell, path::PathBuf, rc::Rc}; use std::{cell::RefCell, path::PathBuf, rc::Rc};
@ -81,7 +84,6 @@ impl AppWindow {
let initial_view = View::Placeholder(PlaceholderView::new().upcast()); let initial_view = View::Placeholder(PlaceholderView::new().upcast());
// layout.append(&header);
layout.append(&initial_view.widget()); layout.append(&initial_view.widget());
let nav_layout = gtk::Box::new(gtk::Orientation::Vertical, 0); let nav_layout = gtk::Box::new(gtk::Orientation::Vertical, 0);
@ -98,6 +100,10 @@ impl AppWindow {
window.set_content(Some(&navigation)); window.set_content(Some(&navigation));
window.present(); window.present();
let gesture = gtk::GestureClick::new();
gesture.connect_released(|_, _, _, _| println!("detected gesture"));
layout.add_controller(gesture);
let s = Self { let s = Self {
app: ft_app, app: ft_app,
app_id: app_id.to_owned(), app_id: app_id.to_owned(),
@ -107,17 +113,7 @@ impl AppWindow {
navigation, navigation,
}; };
glib::spawn_future_local({ s.load_records();
let s = s.clone();
async move {
let end = Local::now().date_naive();
let start = end - Duration::days(7);
match s.app.records(start, end).await {
Ok(_) => s.show_historical_view(),
Err(_) => s.show_welcome_view(),
}
}
});
s s
} }
@ -130,11 +126,37 @@ impl AppWindow {
self.swap_main(view); self.swap_main(view);
} }
fn show_historical_view(&self) { fn show_historical_view(&self, records: Vec<Record<TraxRecord>>) {
let view = View::Historical(HistoricalView::new(vec![])); let view = View::Historical(HistoricalView::new(records, {
let s = self.clone();
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));
let page = &adw::NavigationPage::builder()
.title(date.format("%Y-%m-%d").to_string())
.child(&layout)
.build();
s.navigation.push(page);
})
}));
self.swap_main(view); self.swap_main(view);
} }
fn load_records(&self) {
glib::spawn_future_local({
let s = self.clone();
async move {
let end = Local::now().date_naive();
let start = end - Duration::days(7);
match s.app.records(start, end).await {
Ok(records) => s.show_historical_view(records),
Err(_) => s.show_welcome_view(),
}
}
});
}
// Switch views. // Switch views.
// //
// This function only replaces the old view with the one which matches the current view state. // This function only replaces the old view with the one which matches the current view state.
@ -153,7 +175,7 @@ impl AppWindow {
async move { async move {
if s.app.open_db(path.clone()).await.is_ok() { if s.app.open_db(path.clone()).await.is_ok() {
let _ = s.settings.set("series-path", path.to_str().unwrap()); let _ = s.settings.set("series-path", path.to_str().unwrap());
s.show_historical_view(); s.load_records();
} }
} }
}); });

View File

@ -21,7 +21,6 @@ use glib::Object;
use gtk::{prelude::*, subclass::prelude::*}; use gtk::{prelude::*, subclass::prelude::*};
use std::cell::RefCell; use std::cell::RefCell;
#[derive(Default)]
pub struct DaySummaryPrivate { pub struct DaySummaryPrivate {
date: gtk::Label, date: gtk::Label,
weight: RefCell<Option<gtk::Label>>, weight: RefCell<Option<gtk::Label>>,
@ -35,7 +34,7 @@ impl ObjectSubclass for DaySummaryPrivate {
fn new() -> Self { fn new() -> Self {
let date = gtk::Label::builder() let date = gtk::Label::builder()
.css_classes(["daysummary-date"]) .css_classes(["day-summary__date"])
.halign(gtk::Align::Start) .halign(gtk::Align::Start)
.build(); .build();
Self { Self {
@ -59,7 +58,7 @@ impl DaySummary {
pub fn new() -> Self { pub fn new() -> Self {
let s: Self = Object::builder().build(); let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Vertical); s.set_orientation(gtk::Orientation::Vertical);
s.set_css_classes(&["daysummary"]); s.set_css_classes(&["day-summary"]);
s.append(&s.imp().date); s.append(&s.imp().date);
@ -81,10 +80,74 @@ impl DaySummary {
let label = gtk::Label::builder() let label = gtk::Label::builder()
.halign(gtk::Align::Start) .halign(gtk::Align::Start)
.label(&format!("{}", weight_record.weight)) .label(&format!("{}", weight_record.weight))
.css_classes(["daysummary-weight"]) .css_classes(["day-summary__weight"])
.build(); .build();
self.append(&label); self.append(&label);
*self.imp().weight.borrow_mut() = Some(label); *self.imp().weight.borrow_mut() = Some(label);
} }
self.append(
&gtk::Label::builder()
.halign(gtk::Align::Start)
.label("15km of biking in 60 minutes")
.build(),
);
}
}
pub struct DayDetailPrivate {
date: gtk::Label,
weight: RefCell<Option<gtk::Label>>,
}
#[glib::object_subclass]
impl ObjectSubclass for DayDetailPrivate {
const NAME: &'static str = "DayDetail";
type Type = DayDetail;
type ParentType = gtk::Box;
fn new() -> Self {
let date = gtk::Label::builder()
.css_classes(["daysummary-date"])
.halign(gtk::Align::Start)
.build();
Self {
date,
weight: RefCell::new(None),
}
}
}
impl ObjectImpl for DayDetailPrivate {}
impl WidgetImpl for DayDetailPrivate {}
impl BoxImpl for DayDetailPrivate {}
glib::wrapper! {
pub struct DayDetail(ObjectSubclass<DayDetailPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
}
impl DayDetail {
pub fn new(date: chrono::NaiveDate, records: Vec<TraxRecord>) -> Self {
let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Vertical);
records.into_iter().for_each(|record| {
let record_view = match record {
TraxRecord::BikeRide(_) => gtk::Label::new(Some("BikeRide")),
TraxRecord::Row(_) => gtk::Label::new(Some("Row")),
TraxRecord::Run(_) => gtk::Label::new(Some("Run")),
TraxRecord::Steps(_) => gtk::Label::new(Some("Steps")),
TraxRecord::Swim(_) => gtk::Label::new(Some("Swim")),
TraxRecord::Walk(_) => gtk::Label::new(Some("Walk")),
TraxRecord::Weight(_) => gtk::Label::new(Some("Weight")),
};
record_view.add_css_class("day-detail");
record_view.set_halign(gtk::Align::Start);
s.append(&record_view);
});
s
} }
} }

View File

@ -15,8 +15,8 @@ You should have received a copy of the GNU General Public License along with Fit
*/ */
mod day; mod day;
pub use day::{DayDetail, DaySummary};
pub use day::DaySummary;
use glib::Object; use glib::Object;
use gtk::{prelude::*, subclass::prelude::*}; use gtk::{prelude::*, subclass::prelude::*};
use std::{cell::RefCell, path::PathBuf, rc::Rc}; use std::{cell::RefCell, path::PathBuf, rc::Rc};

View File

@ -15,11 +15,11 @@ You should have received a copy of the GNU General Public License along with Fit
*/ */
use crate::components::DaySummary; use crate::components::DaySummary;
use emseries::{Recordable, Timestamp}; use emseries::{Record, Recordable, Timestamp};
use ft_core::TraxRecord; use ft_core::TraxRecord;
use glib::Object; use glib::Object;
use gtk::{prelude::*, subclass::prelude::*}; use gtk::{prelude::*, subclass::prelude::*};
use std::{cell::RefCell, collections::HashMap}; use std::{cell::RefCell, collections::HashMap, rc::Rc};
/// The historical view will show a window into the main database. It will show some version of /// 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 /// daily summaries, daily details, and will provide all functions the user may need for editing
@ -46,11 +46,20 @@ glib::wrapper! {
} }
impl HistoricalView { impl HistoricalView {
pub fn new(records: Vec<TraxRecord>) -> Self { pub fn new<SelectFn>(records: Vec<Record<TraxRecord>>, on_select_day: Rc<SelectFn>) -> Self
where
SelectFn: Fn(chrono::NaiveDate, Vec<TraxRecord>) + 'static,
{
let s: Self = Object::builder().build(); let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Vertical); s.set_orientation(gtk::Orientation::Vertical);
s.set_css_classes(&["historical"]);
let day_records: GroupedRecords = GroupedRecords::from(records); let day_records: GroupedRecords = GroupedRecords::from(
records
.into_iter()
.map(|r| r.data)
.collect::<Vec<TraxRecord>>(),
);
let model = gio::ListStore::new::<DayRecords>(); let model = gio::ListStore::new::<DayRecords>();
model.extend_from_slice(&day_records.0); model.extend_from_slice(&day_records.0);
@ -86,13 +95,16 @@ impl HistoricalView {
.factory(&factory) .factory(&factory)
.single_click_activate(true) .single_click_activate(true)
.build(); .build();
lst.connect_activate(|s, idx| { lst.connect_activate({
// This gets triggered whenever the user clicks on an item on the list. What we let on_select_day = on_select_day.clone();
// actually want to do here is to open a modal dialog that shows all of the details of move |s, idx| {
// the day and which allows the user to edit items within that dialog. // This gets triggered whenever the user clicks on an item on the list. What we
let item = s.model().unwrap().item(idx).unwrap(); // actually want to do here is to open a modal dialog that shows all of the details of
let records = item.downcast_ref::<DayRecords>().unwrap(); // the day and which allows the user to edit items within that dialog.
println!("list item activated: [{:?}] {:?}", idx, records.date()); let item = s.model().unwrap().item(idx).unwrap();
let records = item.downcast_ref::<DayRecords>().unwrap();
on_select_day(records.date(), records.records());
}
}); });
s.append(&lst); s.append(&lst);

View File

@ -55,11 +55,10 @@ impl WelcomeView {
// branch. // branch.
let title = gtk::Label::builder() let title = gtk::Label::builder()
.label("Welcome to FitnessTrax") .label("Welcome to FitnessTrax")
.css_classes(["welcome-title"]) .css_classes(["welcome__title"])
.build(); .build();
let content = gtk::Box::builder() let content = gtk::Box::builder()
.css_classes(["model-content"])
.orientation(gtk::Orientation::Vertical) .orientation(gtk::Orientation::Vertical)
.vexpand(true) .vexpand(true)
.build(); .build();