From d4c48c44432bd93576863d3ee0c461de71c48161 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sat, 20 Jan 2024 15:59:03 -0500 Subject: [PATCH] Add a step count editor field --- fitnesstrax/app/resources/style.css | 7 ++- fitnesstrax/app/src/components/day.rs | 26 +++++++++-- fitnesstrax/app/src/components/mod.rs | 5 +- fitnesstrax/app/src/components/steps.rs | 61 +++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 fitnesstrax/app/src/components/steps.rs diff --git a/fitnesstrax/app/resources/style.css b/fitnesstrax/app/resources/style.css index 2814994..b375633 100644 --- a/fitnesstrax/app/resources/style.css +++ b/fitnesstrax/app/resources/style.css @@ -11,8 +11,7 @@ padding: 8px; } -.welcome__footer { -} +.welcome__footer {} .historical { margin: 32px; @@ -37,3 +36,7 @@ margin: 8px; } +.step-view { + padding: 8px; + margin: 8px; +} \ No newline at end of file diff --git a/fitnesstrax/app/src/components/day.rs b/fitnesstrax/app/src/components/day.rs index 631600e..776e5af 100644 --- a/fitnesstrax/app/src/components/day.rs +++ b/fitnesstrax/app/src/components/day.rs @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with Fit // use chrono::NaiveDate; // use ft_core::TraxRecord; use crate::{ - components::{ActionGroup, Weight}, + components::{steps_editor, ActionGroup, Steps, Weight, WeightEdit}, view_models::DayDetailViewModel, }; use emseries::Record; @@ -25,8 +25,6 @@ use glib::Object; use gtk::{prelude::*, subclass::prelude::*}; use std::cell::RefCell; -use super::weight::WeightEdit; - pub struct DaySummaryPrivate { date: gtk::Label, weight: RefCell>, @@ -169,8 +167,16 @@ impl DayDetail { }); */ + let top_row = gtk::Box::builder() + .orientation(gtk::Orientation::Horizontal) + .build(); let weight_view = Weight::new(view_model.weight()); - s.append(&weight_view.widget()); + top_row.append(&weight_view.widget()); + + let steps_view = Steps::new(view_model.steps()); + top_row.append(&steps_view.widget()); + + s.append(&top_row); /* records.into_iter().for_each(|record| { @@ -281,7 +287,8 @@ impl DayEdit { .build(), ); - s.append( + let top_row = gtk::Box::builder().orientation(gtk::Orientation::Horizontal).build(); + top_row.append( &WeightEdit::new(view_model.weight(), { let view_model = view_model.clone(); move |w| { @@ -291,6 +298,15 @@ impl DayEdit { .widget(), ); + top_row.append( + &steps_editor(view_model.steps(), { + let view_model = view_model.clone(); + move |s| view_model.set_steps(s) + }) + .widget(), + ); + s.append(&top_row); + s } diff --git a/fitnesstrax/app/src/components/mod.rs b/fitnesstrax/app/src/components/mod.rs index 78e9b9b..134e504 100644 --- a/fitnesstrax/app/src/components/mod.rs +++ b/fitnesstrax/app/src/components/mod.rs @@ -23,6 +23,9 @@ pub use day::{DayDetail, DayEdit, DaySummary}; mod singleton; pub use singleton::{Singleton, SingletonImpl}; +mod steps; +pub use steps::{steps_editor, Steps}; + mod text_entry; pub use text_entry::{ParseError, TextEntry}; @@ -30,7 +33,7 @@ mod time_distance; pub use time_distance::TimeDistanceView; mod weight; -pub use weight::Weight; +pub use weight::{Weight, WeightEdit}; use glib::Object; use gtk::{prelude::*, subclass::prelude::*}; diff --git a/fitnesstrax/app/src/components/steps.rs b/fitnesstrax/app/src/components/steps.rs new file mode 100644 index 0000000..e81c840 --- /dev/null +++ b/fitnesstrax/app/src/components/steps.rs @@ -0,0 +1,61 @@ +/* +Copyright 2024, Savanni D'Gerinel + +This file is part of FitnessTrax. + +FitnessTrax is free software: you can redistribute it and/or modify it under the terms of the GNU +General Public License as published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +FitnessTrax is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see . +*/ + +use crate::components::{ParseError, TextEntry}; +use gtk::prelude::*; + +#[derive(Default)] +pub struct Steps { + label: gtk::Label, +} + +impl Steps { + pub fn new(steps: Option) -> Self { + let label = gtk::Label::builder() + .css_classes(["card", "step-view"]) + .can_focus(true) + .build(); + + match steps { + Some(s) => label.set_text(&format!("{}", s)), + None => label.set_text("No steps recorded"), + } + + Self { label } + } + + pub fn widget(&self) -> gtk::Widget { + self.label.clone().upcast() + } +} + +pub fn steps_editor(value: Option, on_update: OnUpdate) -> TextEntry +where + OnUpdate: Fn(u32) + 'static, +{ + TextEntry::new( + "0", + value, + |v| format!("{}", v), + move |v| match v.parse::() { + Ok(val) => { + on_update(val); + Ok(val) + } + Err(_) => Err(ParseError), + }, + ) +}