From 3dc8be0d260f3ceafb8aa7ea3261a7ddac21254d Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Fri, 22 Dec 2023 18:53:29 -0500 Subject: [PATCH] Render a weight record --- Cargo.lock | 1 + fitnesstrax/app/Cargo.toml | 1 + fitnesstrax/app/src/components/day.rs | 18 ++++++++++++++++++ fitnesstrax/app/src/views/historical_view.rs | 9 +++++++-- fitnesstrax/core/src/lib.rs | 2 +- fitnesstrax/core/src/types.rs | 13 +++++++++++-- 6 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index acdedd3..cef9966 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1022,6 +1022,7 @@ version = "0.1.0" dependencies = [ "async-channel", "chrono", + "dimensioned 0.8.0", "emseries", "ft-core", "gio", diff --git a/fitnesstrax/app/Cargo.toml b/fitnesstrax/app/Cargo.toml index 3f36189..b35999e 100644 --- a/fitnesstrax/app/Cargo.toml +++ b/fitnesstrax/app/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" adw = { version = "0.5", package = "libadwaita", features = [ "v1_2" ] } async-channel = { version = "2.1" } chrono = { version = "0.4" } +dimensioned = { version = "0.8", features = [ "serde" ] } emseries = { path = "../../emseries" } ft-core = { path = "../core" } gio = { version = "0.18" } diff --git a/fitnesstrax/app/src/components/day.rs b/fitnesstrax/app/src/components/day.rs index 3b8bb75..db87773 100644 --- a/fitnesstrax/app/src/components/day.rs +++ b/fitnesstrax/app/src/components/day.rs @@ -16,12 +16,15 @@ You should have received a copy of the GNU General Public License along with Fit // use chrono::NaiveDate; // use ft_core::TraxRecord; +use ft_core::TraxRecord; use glib::Object; use gtk::{prelude::*, subclass::prelude::*}; +use std::cell::RefCell; #[derive(Default)] pub struct DaySummaryPrivate { date: gtk::Label, + weight: RefCell>, } #[glib::object_subclass] @@ -33,6 +36,7 @@ impl ObjectSubclass for DaySummaryPrivate { fn new() -> Self { Self { date: gtk::Label::new(None), + weight: RefCell::new(None), } } } @@ -60,4 +64,18 @@ impl DaySummary { .date .set_text(&date.format("%y-%m-%d").to_string()); } + + pub fn set_records(&self, records: Vec) { + if let Some(ref weight_label) = *self.imp().weight.borrow() { + self.remove(weight_label); + } + + if let Some(TraxRecord::Weight(weight_record)) = + records.iter().filter(|f| f.is_weight()).next() + { + let label = gtk::Label::new(Some(&format!("{}", weight_record.weight))); + self.append(&label); + *self.imp().weight.borrow_mut() = Some(label); + } + } } diff --git a/fitnesstrax/app/src/views/historical_view.rs b/fitnesstrax/app/src/views/historical_view.rs index 89cd6a3..3ad535e 100644 --- a/fitnesstrax/app/src/views/historical_view.rs +++ b/fitnesstrax/app/src/views/historical_view.rs @@ -15,7 +15,8 @@ You should have received a copy of the GNU General Public License along with Fit */ use crate::components::DaySummary; -use ft_core::TraxRecord; +use dimensioned::si::KG; +use ft_core::{TraxRecord, Weight}; use glib::Object; use gtk::{prelude::*, subclass::prelude::*}; use std::cell::RefCell; @@ -56,7 +57,10 @@ impl HistoricalView { let day_records: Vec = vec![DayRecords::new( chrono::NaiveDate::from_ymd_opt(2023, 10, 13).unwrap(), - vec![], + vec![TraxRecord::Weight(Weight { + date: chrono::NaiveDate::from_ymd_opt(2023, 10, 13).unwrap(), + weight: 100. * KG, + })], )]; let model = gio::ListStore::new::(); @@ -86,6 +90,7 @@ impl HistoricalView { .expect("should be a DaySummary"); summary.set_date(records.date()); + summary.set_records(records.records()); }); let lst = gtk::ListView::builder() diff --git a/fitnesstrax/core/src/lib.rs b/fitnesstrax/core/src/lib.rs index 9d7103a..b0c9d9d 100644 --- a/fitnesstrax/core/src/lib.rs +++ b/fitnesstrax/core/src/lib.rs @@ -4,4 +4,4 @@ use emseries::DateTimeTz; mod legacy; mod types; -pub use types::TraxRecord; +pub use types::{TraxRecord, Weight}; diff --git a/fitnesstrax/core/src/types.rs b/fitnesstrax/core/src/types.rs index 4c332d1..ca526d4 100644 --- a/fitnesstrax/core/src/types.rs +++ b/fitnesstrax/core/src/types.rs @@ -48,8 +48,8 @@ pub struct TimeDistance { /// need to track more than a single weight in a day. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct Weight { - date: NaiveDate, - weight: si::Kilogram, + pub date: NaiveDate, + pub weight: si::Kilogram, } /// The unified data structure for all records that are part of the app. @@ -64,6 +64,15 @@ pub enum TraxRecord { Weight(Weight), } +impl TraxRecord { + pub fn is_weight(&self) -> bool { + match self { + TraxRecord::Weight(_) => true, + _ => false, + } + } +} + impl Recordable for TraxRecord { fn timestamp(&self) -> Timestamp { match self {