From 9461c387fe29970ea402e907fc0a12dff19ab2e6 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sat, 20 Jan 2024 16:05:33 -0500 Subject: [PATCH] Simplify the weight editor --- fitnesstrax/app/src/components/day.rs | 8 +-- fitnesstrax/app/src/components/mod.rs | 2 +- fitnesstrax/app/src/components/weight.rs | 66 ++++++++---------------- 3 files changed, 28 insertions(+), 48 deletions(-) diff --git a/fitnesstrax/app/src/components/day.rs b/fitnesstrax/app/src/components/day.rs index 776e5af..7bbd3db 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::{steps_editor, ActionGroup, Steps, Weight, WeightEdit}, + components::{steps_editor, weight_editor, ActionGroup, Steps, Weight}, view_models::DayDetailViewModel, }; use emseries::Record; @@ -287,9 +287,11 @@ impl DayEdit { .build(), ); - let top_row = gtk::Box::builder().orientation(gtk::Orientation::Horizontal).build(); + let top_row = gtk::Box::builder() + .orientation(gtk::Orientation::Horizontal) + .build(); top_row.append( - &WeightEdit::new(view_model.weight(), { + &weight_editor(view_model.weight(), { let view_model = view_model.clone(); move |w| { view_model.set_weight(w); diff --git a/fitnesstrax/app/src/components/mod.rs b/fitnesstrax/app/src/components/mod.rs index 134e504..880664e 100644 --- a/fitnesstrax/app/src/components/mod.rs +++ b/fitnesstrax/app/src/components/mod.rs @@ -33,7 +33,7 @@ mod time_distance; pub use time_distance::TimeDistanceView; mod weight; -pub use weight::{Weight, WeightEdit}; +pub use weight::{weight_editor, Weight}; use glib::Object; use gtk::{prelude::*, subclass::prelude::*}; diff --git a/fitnesstrax/app/src/components/weight.rs b/fitnesstrax/app/src/components/weight.rs index 220b51e..059e903 100644 --- a/fitnesstrax/app/src/components/weight.rs +++ b/fitnesstrax/app/src/components/weight.rs @@ -18,9 +18,6 @@ use crate::components::{ParseError, TextEntry}; use dimensioned::si; use gtk::prelude::*; -#[derive(Default)] -pub struct WeightViewPrivate {} - pub struct Weight { label: gtk::Label, } @@ -45,45 +42,26 @@ impl Weight { } } -#[derive(Debug)] -pub struct WeightEdit { - entry: TextEntry>, -} - -impl WeightEdit { - pub fn new(weight: Option>, on_update: OnUpdate) -> Self - where - OnUpdate: Fn(si::Kilogram) + 'static, - { - Self { - entry: TextEntry::new( - "0 kg", - weight, - |val: &si::Kilogram| val.to_string(), - move |v: &str| { - let new_weight = v.parse::().map(|w| w * si::KG).map_err(|_| ParseError); - match new_weight { - Ok(w) => { - on_update(w); - Ok(w) - } - Err(err) => Err(err), - } - }, - ), - } - } - - #[allow(unused)] - pub fn set_value(&self, value: Option>) { - self.entry.set_value(value); - } - - pub fn value(&self) -> Option> { - self.entry.value() - } - - pub fn widget(&self) -> gtk::Widget { - self.entry.widget() - } +pub fn weight_editor( + weight: Option>, + on_update: OnUpdate, +) -> TextEntry> +where + OnUpdate: Fn(si::Kilogram) + 'static, +{ + TextEntry::new( + "0 kg", + weight, + |val: &si::Kilogram| val.to_string(), + move |v: &str| { + let new_weight = v.parse::().map(|w| w * si::KG).map_err(|_| ParseError); + match new_weight { + Ok(w) => { + on_update(w); + Ok(w) + } + Err(err) => Err(err), + } + }, + ) }