Simplify the weight editor

This commit is contained in:
Savanni D'Gerinel 2024-01-20 16:05:33 -05:00
parent d4c48c4443
commit 9461c387fe
3 changed files with 28 additions and 48 deletions

View File

@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with Fit
// use chrono::NaiveDate; // use chrono::NaiveDate;
// use ft_core::TraxRecord; // use ft_core::TraxRecord;
use crate::{ use crate::{
components::{steps_editor, ActionGroup, Steps, Weight, WeightEdit}, components::{steps_editor, weight_editor, ActionGroup, Steps, Weight},
view_models::DayDetailViewModel, view_models::DayDetailViewModel,
}; };
use emseries::Record; use emseries::Record;
@ -287,9 +287,11 @@ impl DayEdit {
.build(), .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( top_row.append(
&WeightEdit::new(view_model.weight(), { &weight_editor(view_model.weight(), {
let view_model = view_model.clone(); let view_model = view_model.clone();
move |w| { move |w| {
view_model.set_weight(w); view_model.set_weight(w);

View File

@ -33,7 +33,7 @@ mod time_distance;
pub use time_distance::TimeDistanceView; pub use time_distance::TimeDistanceView;
mod weight; mod weight;
pub use weight::{Weight, WeightEdit}; pub use weight::{weight_editor, Weight};
use glib::Object; use glib::Object;
use gtk::{prelude::*, subclass::prelude::*}; use gtk::{prelude::*, subclass::prelude::*};

View File

@ -18,9 +18,6 @@ use crate::components::{ParseError, TextEntry};
use dimensioned::si; use dimensioned::si;
use gtk::prelude::*; use gtk::prelude::*;
#[derive(Default)]
pub struct WeightViewPrivate {}
pub struct Weight { pub struct Weight {
label: gtk::Label, label: gtk::Label,
} }
@ -45,45 +42,26 @@ impl Weight {
} }
} }
#[derive(Debug)] pub fn weight_editor<OnUpdate>(
pub struct WeightEdit { weight: Option<si::Kilogram<f64>>,
entry: TextEntry<si::Kilogram<f64>>, on_update: OnUpdate,
} ) -> TextEntry<si::Kilogram<f64>>
where
impl WeightEdit { OnUpdate: Fn(si::Kilogram<f64>) + 'static,
pub fn new<OnUpdate>(weight: Option<si::Kilogram<f64>>, on_update: OnUpdate) -> Self {
where TextEntry::new(
OnUpdate: Fn(si::Kilogram<f64>) + 'static, "0 kg",
{ weight,
Self { |val: &si::Kilogram<f64>| val.to_string(),
entry: TextEntry::new( move |v: &str| {
"0 kg", let new_weight = v.parse::<f64>().map(|w| w * si::KG).map_err(|_| ParseError);
weight, match new_weight {
|val: &si::Kilogram<f64>| val.to_string(), Ok(w) => {
move |v: &str| { on_update(w);
let new_weight = v.parse::<f64>().map(|w| w * si::KG).map_err(|_| ParseError); Ok(w)
match new_weight { }
Ok(w) => { Err(err) => Err(err),
on_update(w); }
Ok(w) },
} )
Err(err) => Err(err),
}
},
),
}
}
#[allow(unused)]
pub fn set_value(&self, value: Option<si::Kilogram<f64>>) {
self.entry.set_value(value);
}
pub fn value(&self) -> Option<si::Kilogram<f64>> {
self.entry.value()
}
pub fn widget(&self) -> gtk::Widget {
self.entry.widget()
}
} }