From aaca733a4cb62c141a3cf4240f438069089c59e0 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Tue, 23 Jan 2024 08:28:39 -0500 Subject: [PATCH] Render time distance details in the day detail view --- fitnesstrax/app/src/components/day.rs | 57 ++----- fitnesstrax/app/src/components/mod.rs | 2 +- .../app/src/components/time_distance.rs | 142 ++++++++---------- 3 files changed, 73 insertions(+), 128 deletions(-) diff --git a/fitnesstrax/app/src/components/day.rs b/fitnesstrax/app/src/components/day.rs index 5ba2d8d..e18cc18 100644 --- a/fitnesstrax/app/src/components/day.rs +++ b/fitnesstrax/app/src/components/day.rs @@ -26,6 +26,8 @@ use glib::Object; use gtk::{prelude::*, subclass::prelude::*}; use std::cell::RefCell; +use super::time_distance_detail; + pub struct DaySummaryPrivate { date: gtk::Label, } @@ -174,51 +176,18 @@ impl DayDetail { s.append(&top_row); - /* - records.into_iter().for_each(|record| { - let record_view = match record { - Record { - data: ft_core::TraxRecord::BikeRide(record), - .. - } => Some( - TimeDistanceView::new(ft_core::RecordType::BikeRide, record) - .upcast::(), - ), - Record { - data: ft_core::TraxRecord::Row(record), - .. - } => Some( - TimeDistanceView::new(ft_core::RecordType::Row, record).upcast::(), - ), - Record { - data: ft_core::TraxRecord::Run(record), - .. - } => Some( - TimeDistanceView::new(ft_core::RecordType::Row, record).upcast::(), - ), - Record { - data: ft_core::TraxRecord::Swim(record), - .. - } => Some( - TimeDistanceView::new(ft_core::RecordType::Row, record).upcast::(), - ), - Record { - data: ft_core::TraxRecord::Walk(record), - .. - } => Some( - TimeDistanceView::new(ft_core::RecordType::Row, record).upcast::(), - ), - _ => None, - }; - - if let Some(record_view) = record_view { - record_view.add_css_class("day-detail"); - record_view.set_halign(gtk::Align::Start); - - s.append(&record_view); + let records = view_model.records(); + for emseries::Record { data, .. } in records { + match data { + TraxRecord::BikeRide(ride) => { + s.append(&time_distance_detail(RecordType::BikeRide, ride)) + } + TraxRecord::Row(row) => s.append(&time_distance_detail(RecordType::Row, row)), + TraxRecord::Run(run) => s.append(&time_distance_detail(RecordType::Run, run)), + TraxRecord::Walk(walk) => s.append(&time_distance_detail(RecordType::Walk, walk)), + _ => {} } - }); - */ + } s } diff --git a/fitnesstrax/app/src/components/mod.rs b/fitnesstrax/app/src/components/mod.rs index 7d65146..858f7d9 100644 --- a/fitnesstrax/app/src/components/mod.rs +++ b/fitnesstrax/app/src/components/mod.rs @@ -30,7 +30,7 @@ mod text_entry; pub use text_entry::{ParseError, TextEntry}; mod time_distance; -pub use time_distance::{time_distance_summary, TimeDistanceView}; +pub use time_distance::{time_distance_detail, time_distance_summary}; mod weight; pub use weight::{weight_editor, Weight}; diff --git a/fitnesstrax/app/src/components/time_distance.rs b/fitnesstrax/app/src/components/time_distance.rs index 53c0881..201b284 100644 --- a/fitnesstrax/app/src/components/time_distance.rs +++ b/fitnesstrax/app/src/components/time_distance.rs @@ -44,87 +44,63 @@ pub fn time_distance_summary( text.map(|text| gtk::Label::new(Some(&text))) } -#[derive(Default)] -pub struct TimeDistanceViewPrivate { - #[allow(unused)] - record: RefCell>, -} - -#[glib::object_subclass] -impl ObjectSubclass for TimeDistanceViewPrivate { - const NAME: &'static str = "TimeDistanceView"; - type Type = TimeDistanceView; - type ParentType = gtk::Box; -} - -impl ObjectImpl for TimeDistanceViewPrivate {} -impl WidgetImpl for TimeDistanceViewPrivate {} -impl BoxImpl for TimeDistanceViewPrivate {} - -glib::wrapper! { - pub struct TimeDistanceView(ObjectSubclass) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable; -} - -impl TimeDistanceView { - pub fn new(type_: RecordType, record: TimeDistance) -> Self { - let s: Self = Object::builder().build(); - s.set_orientation(gtk::Orientation::Vertical); - s.set_hexpand(true); - - let first_row = gtk::Box::builder().homogeneous(true).build(); - - first_row.append( - >k::Label::builder() - .halign(gtk::Align::Start) - .label(record.datetime.format("%H:%M").to_string()) - .build(), - ); - - first_row.append( - >k::Label::builder() - .halign(gtk::Align::Start) - .label(format!("{:?}", type_)) - .build(), - ); - - first_row.append( - >k::Label::builder() - .halign(gtk::Align::Start) - .label( - record - .distance - .map(|dist| format!("{}", dist)) - .unwrap_or("".to_owned()), - ) - .build(), - ); - - first_row.append( - >k::Label::builder() - .halign(gtk::Align::Start) - .label( - record - .duration - .map(|duration| format!("{}", duration)) - .unwrap_or("".to_owned()), - ) - .build(), - ); - - s.append(&first_row); - - s.append( - >k::Label::builder() - .halign(gtk::Align::Start) - .label( - record - .comments - .map(|comments| comments.to_string()) - .unwrap_or("".to_owned()), - ) - .build(), - ); - - s - } +pub fn time_distance_detail(type_: ft_core::RecordType, record: ft_core::TimeDistance) -> gtk::Box { + let layout = gtk::Box::builder() + .orientation(gtk::Orientation::Vertical) + .hexpand(true) + .build(); + let first_row = gtk::Box::builder().homogeneous(true).build(); + + first_row.append( + >k::Label::builder() + .halign(gtk::Align::Start) + .label(record.datetime.format("%H:%M").to_string()) + .build(), + ); + + first_row.append( + >k::Label::builder() + .halign(gtk::Align::Start) + .label(format!("{:?}", type_)) + .build(), + ); + + first_row.append( + >k::Label::builder() + .halign(gtk::Align::Start) + .label( + record + .distance + .map(|dist| format!("{}", dist)) + .unwrap_or("".to_owned()), + ) + .build(), + ); + + first_row.append( + >k::Label::builder() + .halign(gtk::Align::Start) + .label( + record + .duration + .map(|duration| format!("{}", duration)) + .unwrap_or("".to_owned()), + ) + .build(), + ); + + layout.append(&first_row); + + layout.append( + >k::Label::builder() + .halign(gtk::Align::Start) + .label( + record + .comments + .map(|comments| comments.to_string()) + .unwrap_or("".to_owned()), + ) + .build(), + ); + layout }