Introduce a structure for formatting and parsing Weight values

This commit is contained in:
Savanni D'Gerinel 2024-01-30 10:08:10 -05:00
parent f8d66bbb69
commit 69567db486
7 changed files with 86 additions and 33 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, weight_editor, ActionGroup, Steps, Weight}, components::{steps_editor, weight_editor, ActionGroup, Steps, WeightLabel},
view_models::DayDetailViewModel, view_models::DayDetailViewModel,
}; };
use glib::Object; use glib::Object;
@ -93,7 +93,7 @@ impl DaySummary {
.css_classes(["day-summary__weight"]) .css_classes(["day-summary__weight"])
.build(); .build();
if let Some(s) = view_model.steps() { if let Some(s) = view_model.steps() {
label.set_label(&format!("{} steps", s.to_string())); label.set_label(&format!("{} steps", s));
} }
row.append(&label); row.append(&label);
@ -162,7 +162,7 @@ impl DayDetail {
let top_row = gtk::Box::builder() let top_row = gtk::Box::builder()
.orientation(gtk::Orientation::Horizontal) .orientation(gtk::Orientation::Horizontal)
.build(); .build();
let weight_view = Weight::new(view_model.weight()); let weight_view = WeightLabel::new(view_model.weight());
top_row.append(&weight_view.widget()); top_row.append(&weight_view.widget());
let steps_view = Steps::new(view_model.steps()); let steps_view = Steps::new(view_model.steps());

View File

@ -27,13 +27,13 @@ mod steps;
pub use steps::{steps_editor, Steps}; pub use steps::{steps_editor, Steps};
mod text_entry; mod text_entry;
pub use text_entry::{ParseError, TextEntry}; pub use text_entry::TextEntry;
mod time_distance; mod time_distance;
pub use time_distance::TimeDistanceView; pub use time_distance::TimeDistanceView;
mod weight; mod weight;
pub use weight::{weight_editor, Weight}; pub use weight::{weight_editor, WeightLabel};
use glib::Object; use glib::Object;
use gtk::{prelude::*, subclass::prelude::*}; use gtk::{prelude::*, subclass::prelude::*};

View File

@ -14,7 +14,7 @@ General Public License for more details.
You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>. You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>.
*/ */
use crate::components::{ParseError, TextEntry}; use crate::{components::TextEntry, types::ParseError};
use gtk::prelude::*; use gtk::prelude::*;
#[derive(Default)] #[derive(Default)]

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2023, Savanni D'Gerinel <savanni@luminescent-dreams.com> Copyright 2023-2024, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of FitnessTrax. This file is part of FitnessTrax.
@ -14,12 +14,10 @@ General Public License for more details.
You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>. You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>.
*/ */
use crate::types::ParseError;
use gtk::prelude::*; use gtk::prelude::*;
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
#[derive(Clone, Debug)]
pub struct ParseError;
type Renderer<T> = dyn Fn(&T) -> String; type Renderer<T> = dyn Fn(&T) -> String;
type Parser<T> = dyn Fn(&str) -> Result<T, ParseError>; type Parser<T> = dyn Fn(&str) -> Result<T, ParseError>;

View File

@ -14,23 +14,25 @@ General Public License for more details.
You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>. You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>.
*/ */
use crate::components::{ParseError, TextEntry}; use crate::{
use dimensioned::si; components::TextEntry,
types::{FormatOption, Weight},
};
use gtk::prelude::*; use gtk::prelude::*;
pub struct Weight { pub struct WeightLabel {
label: gtk::Label, label: gtk::Label,
} }
impl Weight { impl WeightLabel {
pub fn new(weight: Option<si::Kilogram<f64>>) -> Self { pub fn new(weight: Option<Weight>) -> Self {
let label = gtk::Label::builder() let label = gtk::Label::builder()
.css_classes(["card", "weight-view"]) .css_classes(["card", "weight-view"])
.can_focus(true) .can_focus(true)
.build(); .build();
match weight { match weight {
Some(w) => label.set_text(&format!("{:?}", w)), Some(w) => label.set_text(&w.format(FormatOption::Abbreviated)),
None => label.set_text("No weight recorded"), None => label.set_text("No weight recorded"),
} }
@ -42,19 +44,16 @@ impl Weight {
} }
} }
pub fn weight_editor<OnUpdate>( pub fn weight_editor<OnUpdate>(weight: Option<Weight>, on_update: OnUpdate) -> TextEntry<Weight>
weight: Option<si::Kilogram<f64>>,
on_update: OnUpdate,
) -> TextEntry<si::Kilogram<f64>>
where where
OnUpdate: Fn(si::Kilogram<f64>) + 'static, OnUpdate: Fn(Weight) + 'static,
{ {
TextEntry::new( TextEntry::new(
"0 kg", "0 kg",
weight, weight,
|val: &si::Kilogram<f64>| val.to_string(), |val: &Weight| val.format(FormatOption::Abbreviated),
move |v: &str| { move |v: &str| {
let new_weight = v.parse::<f64>().map(|w| w * si::KG).map_err(|_| ParseError); let new_weight = Weight::parse(v);
match new_weight { match new_weight {
Ok(w) => { Ok(w) => {
on_update(w); on_update(w);

View File

@ -1,4 +1,8 @@
use chrono::{Duration, Local, NaiveDate}; use chrono::{Local, NaiveDate};
use dimensioned::si;
#[derive(Clone, Debug)]
pub struct ParseError;
// This interval doesn't feel right, either. The idea that I have a specific interval type for just // This interval doesn't feel right, either. The idea that I have a specific interval type for just
// NaiveDate is odd. This should be genericized, as should the iterator. Also, it shouldn't live // NaiveDate is odd. This should be genericized, as should the iterator. Also, it shouldn't live
@ -12,7 +16,7 @@ pub struct DayInterval {
impl Default for DayInterval { impl Default for DayInterval {
fn default() -> Self { fn default() -> Self {
Self { Self {
start: (Local::now() - Duration::days(7)).date_naive(), start: (Local::now() - chrono::Duration::days(7)).date_naive(),
end: Local::now().date_naive(), end: Local::now().date_naive(),
} }
} }
@ -38,10 +42,61 @@ impl Iterator for DayIterator {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
if self.current <= self.end { if self.current <= self.end {
let val = self.current; let val = self.current;
self.current += Duration::days(1); self.current += chrono::Duration::days(1);
Some(val) Some(val)
} else { } else {
None None
} }
} }
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FormatOption {
Abbreviated,
Full,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Weight(si::Kilogram<f64>);
impl Weight {
pub fn format(&self, option: FormatOption) -> String {
match option {
FormatOption::Abbreviated => format!("{} kg", self.0.value_unsafe),
FormatOption::Full => format!("{} kilograms", self.0.value_unsafe),
}
}
pub fn parse(s: &str) -> Result<Weight, ParseError> {
s.parse::<f64>()
.map(|w| Weight(w * si::KG))
.map_err(|_| ParseError)
}
}
impl std::ops::Add for Weight {
type Output = Weight;
fn add(self, rside: Self) -> Self::Output {
Self::Output::from(self.0 + rside.0)
}
}
impl std::ops::Sub for Weight {
type Output = Weight;
fn sub(self, rside: Self) -> Self::Output {
Self::Output::from(self.0 - rside.0)
}
}
impl std::ops::Deref for Weight {
type Target = si::Kilogram<f64>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<si::Kilogram<f64>> for Weight {
fn from(value: si::Kilogram<f64>) -> Self {
Self(value)
}
}

View File

@ -14,8 +14,7 @@ General Public License for more details.
You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>. You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>.
*/ */
use crate::app::App; use crate::{app::App, types::Weight};
use dimensioned::si;
use emseries::{Record, RecordId, Recordable}; use emseries::{Record, RecordId, Recordable};
use ft_core::TraxRecord; use ft_core::TraxRecord;
use std::{ use std::{
@ -125,20 +124,22 @@ impl DayDetailViewModel {
} }
} }
pub fn weight(&self) -> Option<si::Kilogram<f64>> { pub fn weight(&self) -> Option<Weight> {
(*self.weight.read().unwrap()).as_ref().map(|w| w.weight) (*self.weight.read().unwrap())
.as_ref()
.map(|w| Weight::from(w.weight))
} }
pub fn set_weight(&self, new_weight: si::Kilogram<f64>) { pub fn set_weight(&self, new_weight: Weight) {
let mut record = self.weight.write().unwrap(); let mut record = self.weight.write().unwrap();
let new_record = match *record { let new_record = match *record {
Some(ref rstate) => rstate.clone().with_value(ft_core::Weight { Some(ref rstate) => rstate.clone().with_value(ft_core::Weight {
date: self.date, date: self.date,
weight: new_weight, weight: *new_weight,
}), }),
None => RecordState::New(ft_core::Weight { None => RecordState::New(ft_core::Weight {
date: self.date, date: self.date,
weight: new_weight, weight: *new_weight,
}), }),
}; };
*record = Some(new_record); *record = Some(new_record);