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 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);

View File

@ -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::*};

View File

@ -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<si::Kilogram<f64>>,
}
impl WeightEdit {
pub fn new<OnUpdate>(weight: Option<si::Kilogram<f64>>, on_update: OnUpdate) -> Self
where
OnUpdate: Fn(si::Kilogram<f64>) + 'static,
{
Self {
entry: TextEntry::new(
"0 kg",
weight,
|val: &si::Kilogram<f64>| val.to_string(),
move |v: &str| {
let new_weight = v.parse::<f64>().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<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()
}
pub fn weight_editor<OnUpdate>(
weight: Option<si::Kilogram<f64>>,
on_update: OnUpdate,
) -> TextEntry<si::Kilogram<f64>>
where
OnUpdate: Fn(si::Kilogram<f64>) + 'static,
{
TextEntry::new(
"0 kg",
weight,
|val: &si::Kilogram<f64>| val.to_string(),
move |v: &str| {
let new_weight = v.parse::<f64>().map(|w| w * si::KG).map_err(|_| ParseError);
match new_weight {
Ok(w) => {
on_update(w);
Ok(w)
}
Err(err) => Err(err),
}
},
)
}