Compare commits
14 Commits
31d74588fb
...
e5e33f29f6
Author | SHA1 | Date |
---|---|---|
Savanni D'Gerinel | e5e33f29f6 | |
Savanni D'Gerinel | 33c85ee7b3 | |
Savanni D'Gerinel | 9874b6081b | |
Savanni D'Gerinel | 7d5d639ed9 | |
Savanni D'Gerinel | 4fd377a3f1 | |
Savanni D'Gerinel | 2277055f84 | |
Savanni D'Gerinel | 39acfe7950 | |
Savanni D'Gerinel | d0cce4ee58 | |
Savanni D'Gerinel | 3a716ee546 | |
Savanni D'Gerinel | 22ba4f575d | |
Savanni D'Gerinel | 3b2130fa01 | |
Savanni D'Gerinel | 8f53bc4de6 | |
Savanni D'Gerinel | fbe21616e3 | |
Savanni D'Gerinel | bbf07ef818 |
|
@ -17,9 +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::{
|
components::{steps_editor, time_distance_summary, weight_field, ActionGroup, Steps, Weight},
|
||||||
steps_editor, time_distance_summary, weight_field, ActionGroup, Steps, WeightLabel,
|
|
||||||
},
|
|
||||||
view_models::DayDetailViewModel,
|
view_models::DayDetailViewModel,
|
||||||
};
|
};
|
||||||
use emseries::{Record, RecordId};
|
use emseries::{Record, RecordId};
|
||||||
|
@ -147,7 +145,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 = WeightLabel::new(view_model.weight());
|
let weight_view = Weight::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());
|
||||||
|
@ -341,7 +339,7 @@ fn weight_and_steps_row(view_model: &DayDetailViewModel) -> gtk::Box {
|
||||||
let view_model = view_model.clone();
|
let view_model = view_model.clone();
|
||||||
move |w| match w {
|
move |w| match w {
|
||||||
Some(w) => view_model.set_weight(w),
|
Some(w) => view_model.set_weight(w),
|
||||||
None => eprintln!("have not implemented record delete"),
|
None => unimplemented!("need to delete the weight entry"),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.widget(),
|
.widget(),
|
||||||
|
@ -352,7 +350,7 @@ fn weight_and_steps_row(view_model: &DayDetailViewModel) -> gtk::Box {
|
||||||
let view_model = view_model.clone();
|
let view_model = view_model.clone();
|
||||||
move |s| match s {
|
move |s| match s {
|
||||||
Some(s) => view_model.set_steps(s),
|
Some(s) => view_model.set_steps(s),
|
||||||
None => eprintln!("have not implemented record delete"),
|
None => unimplemented!("need to delete the steps entry"),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.widget(),
|
.widget(),
|
||||||
|
|
|
@ -34,7 +34,7 @@ mod time_distance;
|
||||||
pub use time_distance::{time_distance_detail, time_distance_summary};
|
pub use time_distance::{time_distance_detail, time_distance_summary};
|
||||||
|
|
||||||
mod weight;
|
mod weight;
|
||||||
pub use weight::WeightLabel;
|
pub use weight::Weight;
|
||||||
|
|
||||||
use glib::Object;
|
use glib::Object;
|
||||||
use gtk::{prelude::*, subclass::prelude::*};
|
use gtk::{prelude::*, subclass::prelude::*};
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Copyright 2023-2024, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
Copyright 2023, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
||||||
|
|
||||||
This file is part of FitnessTrax.
|
This file is part of FitnessTrax.
|
||||||
|
|
||||||
|
@ -14,9 +14,8 @@ 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::{
|
use crate::types::{Distance, Duration, FormatOption, ParseError};
|
||||||
DistanceFormatter, DurationFormatter, FormatOption, ParseError, TimeFormatter, WeightFormatter,
|
use dimensioned::si;
|
||||||
};
|
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
|
@ -26,7 +25,6 @@ pub type OnUpdate<T> = dyn Fn(Option<T>);
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct TextEntry<T: Clone + std::fmt::Debug> {
|
pub struct TextEntry<T: Clone + std::fmt::Debug> {
|
||||||
value: Rc<RefCell<Option<T>>>,
|
value: Rc<RefCell<Option<T>>>,
|
||||||
|
|
||||||
widget: gtk::Entry,
|
widget: gtk::Entry,
|
||||||
parser: Rc<Parser<T>>,
|
parser: Rc<Parser<T>>,
|
||||||
on_update: Rc<OnUpdate<T>>,
|
on_update: Rc<OnUpdate<T>>,
|
||||||
|
@ -80,7 +78,6 @@ impl<T: Clone + std::fmt::Debug + 'static> TextEntry<T> {
|
||||||
if buffer.text().is_empty() {
|
if buffer.text().is_empty() {
|
||||||
*self.value.borrow_mut() = None;
|
*self.value.borrow_mut() = None;
|
||||||
self.widget.remove_css_class("error");
|
self.widget.remove_css_class("error");
|
||||||
(self.on_update)(None);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
match (self.parser)(buffer.text().as_str()) {
|
match (self.parser)(buffer.text().as_str()) {
|
||||||
|
@ -99,144 +96,62 @@ impl<T: Clone + std::fmt::Debug + 'static> TextEntry<T> {
|
||||||
pub fn widget(&self) -> gtk::Widget {
|
pub fn widget(&self) -> gtk::Widget {
|
||||||
self.widget.clone().upcast::<gtk::Widget>()
|
self.widget.clone().upcast::<gtk::Widget>()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
fn has_parse_error(&self) -> bool {
|
|
||||||
self.widget.has_css_class("error")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn time_field<OnUpdate>(
|
|
||||||
value: Option<TimeFormatter>,
|
|
||||||
on_update: OnUpdate,
|
|
||||||
) -> TextEntry<TimeFormatter>
|
|
||||||
where
|
|
||||||
OnUpdate: Fn(Option<TimeFormatter>) + 'static,
|
|
||||||
{
|
|
||||||
TextEntry::new(
|
|
||||||
"HH:MM",
|
|
||||||
value,
|
|
||||||
|val| val.format(FormatOption::Abbreviated),
|
|
||||||
TimeFormatter::parse,
|
|
||||||
on_update,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn distance_field<OnUpdate>(
|
|
||||||
value: Option<DistanceFormatter>,
|
|
||||||
on_update: OnUpdate,
|
|
||||||
) -> TextEntry<DistanceFormatter>
|
|
||||||
where
|
|
||||||
OnUpdate: Fn(Option<DistanceFormatter>) + 'static,
|
|
||||||
{
|
|
||||||
TextEntry::new(
|
|
||||||
"0 km",
|
|
||||||
value,
|
|
||||||
|val| val.format(FormatOption::Abbreviated),
|
|
||||||
DistanceFormatter::parse,
|
|
||||||
on_update,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn duration_field<OnUpdate>(
|
|
||||||
value: Option<DurationFormatter>,
|
|
||||||
on_update: OnUpdate,
|
|
||||||
) -> TextEntry<DurationFormatter>
|
|
||||||
where
|
|
||||||
OnUpdate: Fn(Option<DurationFormatter>) + 'static,
|
|
||||||
{
|
|
||||||
TextEntry::new(
|
|
||||||
"0 m",
|
|
||||||
value,
|
|
||||||
|val| val.format(FormatOption::Abbreviated),
|
|
||||||
DurationFormatter::parse,
|
|
||||||
on_update,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
pub fn weight_field<OnUpdate>(
|
pub fn weight_field<OnUpdate>(
|
||||||
weight: Option<WeightFormatter>,
|
weight: Option<si::Kilogram<f64>>,
|
||||||
on_update: OnUpdate,
|
on_update: OnUpdate,
|
||||||
) -> TextEntry<WeightFormatter>
|
) -> TextEntry<si::Kilogram<f64>>
|
||||||
where
|
where
|
||||||
OnUpdate: Fn(Option<WeightFormatter>) + 'static,
|
OnUpdate: Fn(Option<si::Kilogram<f64>>) + 'static,
|
||||||
{
|
{
|
||||||
TextEntry::new(
|
TextEntry::new(
|
||||||
"0 kg",
|
"0 kg",
|
||||||
weight,
|
weight,
|
||||||
|val| val.format(FormatOption::Abbreviated),
|
|val: &si::Kilogram<f64>| val.to_string(),
|
||||||
WeightFormatter::parse,
|
move |v: &str| v.parse::<f64>().map(|w| w * si::KG).map_err(|_| ParseError),
|
||||||
on_update,
|
on_update,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
pub fn time_field<OnUpdate>(
|
||||||
mod test {
|
value: chrono::NaiveTime,
|
||||||
use super::*;
|
on_update: OnUpdate,
|
||||||
use crate::gtk_init::gtk_init;
|
) -> TextEntry<chrono::NaiveTime>
|
||||||
|
where
|
||||||
fn setup_u32_entry() -> (Rc<RefCell<Option<u32>>>, TextEntry<u32>) {
|
OnUpdate: Fn(Option<chrono::NaiveTime>) + 'static,
|
||||||
let current_value = Rc::new(RefCell::new(None));
|
{
|
||||||
|
TextEntry::new(
|
||||||
let entry = TextEntry::new(
|
"hh:mm",
|
||||||
"step count",
|
Some(value),
|
||||||
None,
|
|v| v.format("%H:%M").to_string(),
|
||||||
|steps| format!("{}", steps),
|
|s| chrono::NaiveTime::parse_from_str(s, "%H:%M").map_err(|_| ParseError),
|
||||||
|v| v.parse::<u32>().map_err(|_| ParseError),
|
on_update,
|
||||||
{
|
)
|
||||||
let current_value = current_value.clone();
|
}
|
||||||
move |v| *current_value.borrow_mut() = v
|
|
||||||
},
|
pub fn distance_field<OnUpdate>(value: Option<Distance>, on_update: OnUpdate) -> TextEntry<Distance>
|
||||||
);
|
where
|
||||||
|
OnUpdate: Fn(Option<Distance>) + 'static,
|
||||||
(current_value, entry)
|
{
|
||||||
}
|
TextEntry::new(
|
||||||
|
"0 km",
|
||||||
#[test]
|
value,
|
||||||
fn it_responds_to_field_changes() {
|
|v| format!("{} km", v.value_unsafe / 1000.),
|
||||||
gtk_init();
|
Distance::parse,
|
||||||
let (current_value, entry) = setup_u32_entry();
|
on_update,
|
||||||
let buffer = entry.widget.buffer();
|
)
|
||||||
|
}
|
||||||
buffer.set_text("1");
|
|
||||||
assert_eq!(*current_value.borrow(), Some(1));
|
pub fn duration_field<OnUpdate>(value: Option<Duration>, on_update: OnUpdate) -> TextEntry<Duration>
|
||||||
|
where
|
||||||
buffer.set_text("15");
|
OnUpdate: Fn(Option<Duration>) + 'static,
|
||||||
assert_eq!(*current_value.borrow(), Some(15));
|
{
|
||||||
}
|
TextEntry::new(
|
||||||
|
"0 minutes",
|
||||||
#[test]
|
value,
|
||||||
fn it_preserves_last_value_in_parse_error() {
|
|v| v.format(FormatOption::Abbreviated),
|
||||||
crate::gtk_init::gtk_init();
|
Duration::parse,
|
||||||
let (current_value, entry) = setup_u32_entry();
|
on_update,
|
||||||
let buffer = entry.widget.buffer();
|
)
|
||||||
|
|
||||||
buffer.set_text("1");
|
|
||||||
assert_eq!(*current_value.borrow(), Some(1));
|
|
||||||
|
|
||||||
buffer.set_text("a5");
|
|
||||||
assert_eq!(*current_value.borrow(), Some(1));
|
|
||||||
assert!(entry.has_parse_error());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_update_on_empty_strings() {
|
|
||||||
gtk_init();
|
|
||||||
let (current_value, entry) = setup_u32_entry();
|
|
||||||
let buffer = entry.widget.buffer();
|
|
||||||
|
|
||||||
buffer.set_text("1");
|
|
||||||
assert_eq!(*current_value.borrow(), Some(1));
|
|
||||||
buffer.set_text("");
|
|
||||||
assert_eq!(*current_value.borrow(), None);
|
|
||||||
|
|
||||||
buffer.set_text("1");
|
|
||||||
assert_eq!(*current_value.borrow(), Some(1));
|
|
||||||
buffer.set_text("1a");
|
|
||||||
assert_eq!(*current_value.borrow(), Some(1));
|
|
||||||
assert!(entry.has_parse_error());
|
|
||||||
|
|
||||||
buffer.set_text("");
|
|
||||||
assert_eq!(*current_value.borrow(), None);
|
|
||||||
assert!(!entry.has_parse_error());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ You should have received a copy of the GNU General Public License along with Fit
|
||||||
// use dimensioned::si;
|
// use dimensioned::si;
|
||||||
use crate::{
|
use crate::{
|
||||||
components::{distance_field, duration_field, time_field},
|
components::{distance_field, duration_field, time_field},
|
||||||
types::{DistanceFormatter, DurationFormatter, FormatOption, TimeFormatter},
|
types::{Distance, Duration, FormatOption},
|
||||||
};
|
};
|
||||||
use dimensioned::si;
|
use dimensioned::si;
|
||||||
use ft_core::{RecordType, TimeDistance};
|
use ft_core::{RecordType, TimeDistance};
|
||||||
|
@ -27,10 +27,7 @@ use glib::Object;
|
||||||
use gtk::{prelude::*, subclass::prelude::*};
|
use gtk::{prelude::*, subclass::prelude::*};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
|
||||||
pub fn time_distance_summary(
|
pub fn time_distance_summary(distance: Distance, duration: Duration) -> Option<gtk::Label> {
|
||||||
distance: DistanceFormatter,
|
|
||||||
duration: DurationFormatter,
|
|
||||||
) -> Option<gtk::Label> {
|
|
||||||
let text = match (*distance > si::M, *duration > si::S) {
|
let text = match (*distance > si::M, *duration > si::S) {
|
||||||
(true, true) => Some(format!(
|
(true, true) => Some(format!(
|
||||||
"{} of biking in {}",
|
"{} of biking in {}",
|
||||||
|
@ -72,7 +69,7 @@ pub fn time_distance_detail(type_: ft_core::RecordType, record: ft_core::TimeDis
|
||||||
.label(
|
.label(
|
||||||
record
|
record
|
||||||
.distance
|
.distance
|
||||||
.map(|dist| DistanceFormatter::from(dist).format(FormatOption::Abbreviated))
|
.map(|dist| Distance::from(dist).format(FormatOption::Abbreviated))
|
||||||
.unwrap_or("".to_owned()),
|
.unwrap_or("".to_owned()),
|
||||||
)
|
)
|
||||||
.build(),
|
.build(),
|
||||||
|
@ -84,9 +81,7 @@ pub fn time_distance_detail(type_: ft_core::RecordType, record: ft_core::TimeDis
|
||||||
.label(
|
.label(
|
||||||
record
|
record
|
||||||
.duration
|
.duration
|
||||||
.map(|duration| {
|
.map(|duration| Duration::from(duration).format(FormatOption::Abbreviated))
|
||||||
DurationFormatter::from(duration).format(FormatOption::Abbreviated)
|
|
||||||
})
|
|
||||||
.unwrap_or("".to_owned()),
|
.unwrap_or("".to_owned()),
|
||||||
)
|
)
|
||||||
.build(),
|
.build(),
|
||||||
|
@ -167,26 +162,23 @@ impl TimeDistanceEdit {
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
details_row.append(
|
details_row.append(
|
||||||
&time_field(
|
&time_field(workout.datetime.naive_local().time(), {
|
||||||
Some(TimeFormatter::from(workout.datetime.naive_local().time())),
|
|
||||||
{
|
|
||||||
let s = s.clone();
|
let s = s.clone();
|
||||||
move |t| s.update_time(t)
|
move |t| s.update_time(t)
|
||||||
},
|
|
||||||
)
|
|
||||||
.widget(),
|
|
||||||
);
|
|
||||||
details_row.append(
|
|
||||||
&distance_field(workout.distance.map(DistanceFormatter::from), {
|
|
||||||
let s = s.clone();
|
|
||||||
move |d| s.update_distance(d)
|
|
||||||
})
|
})
|
||||||
.widget(),
|
.widget(),
|
||||||
);
|
);
|
||||||
details_row.append(
|
details_row.append(
|
||||||
&duration_field(workout.duration.map(DurationFormatter::from), {
|
&distance_field(workout.distance.map(Distance::from), {
|
||||||
let s = s.clone();
|
let s = s.clone();
|
||||||
move |d| s.update_duration(d)
|
move |d| s.update_distance(d.map(|d| *d))
|
||||||
|
})
|
||||||
|
.widget(),
|
||||||
|
);
|
||||||
|
details_row.append(
|
||||||
|
&duration_field(workout.duration.map(Duration::from), {
|
||||||
|
let s = s.clone();
|
||||||
|
move |d| s.update_duration(d.map(|d| *d))
|
||||||
})
|
})
|
||||||
.widget(),
|
.widget(),
|
||||||
);
|
);
|
||||||
|
@ -196,19 +188,19 @@ impl TimeDistanceEdit {
|
||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_time(&self, time: Option<TimeFormatter>) {
|
fn update_time(&self, _time: Option<chrono::NaiveTime>) {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_distance(&self, distance: Option<DistanceFormatter>) {
|
fn update_distance(&self, distance: Option<si::Meter<f64>>) {
|
||||||
let mut workout = self.imp().workout.borrow_mut();
|
let mut workout = self.imp().workout.borrow_mut();
|
||||||
workout.distance = distance.map(|d| *d);
|
workout.distance = distance;
|
||||||
(self.imp().on_update.borrow())(self.imp().type_.borrow().clone(), workout.clone());
|
(self.imp().on_update.borrow())(self.imp().type_.borrow().clone(), workout.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_duration(&self, duration: Option<DurationFormatter>) {
|
fn update_duration(&self, duration: Option<si::Second<f64>>) {
|
||||||
let mut workout = self.imp().workout.borrow_mut();
|
let mut workout = self.imp().workout.borrow_mut();
|
||||||
workout.duration = duration.map(|d| *d);
|
workout.duration = duration;
|
||||||
(self.imp().on_update.borrow())(self.imp().type_.borrow().clone(), workout.clone());
|
(self.imp().on_update.borrow())(self.imp().type_.borrow().clone(), workout.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Copyright 2023-2024, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
Copyright 2023, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
||||||
|
|
||||||
This file is part of FitnessTrax.
|
This file is part of FitnessTrax.
|
||||||
|
|
||||||
|
@ -14,26 +14,22 @@ 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::TextEntry,
|
|
||||||
types::{FormatOption, WeightFormatter},
|
|
||||||
};
|
|
||||||
use dimensioned::si;
|
use dimensioned::si;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
|
|
||||||
pub struct WeightLabel {
|
pub struct Weight {
|
||||||
label: gtk::Label,
|
label: gtk::Label,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WeightLabel {
|
impl Weight {
|
||||||
pub fn new(weight: Option<WeightFormatter>) -> Self {
|
pub fn new(weight: Option<si::Kilogram<f64>>) -> 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(&w.format(FormatOption::Abbreviated)),
|
Some(w) => label.set_text(&format!("{:?}", w)),
|
||||||
None => label.set_text("No weight recorded"),
|
None => label.set_text("No weight recorded"),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
use std::sync::Once;
|
|
||||||
|
|
||||||
static INITIALIZED: Once = Once::new();
|
|
||||||
|
|
||||||
pub fn gtk_init() {
|
|
||||||
INITIALIZED.call_once(|| {
|
|
||||||
eprintln!("initializing GTK");
|
|
||||||
let _ = gtk::init();
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -17,8 +17,6 @@ You should have received a copy of the GNU General Public License along with Fit
|
||||||
mod app;
|
mod app;
|
||||||
mod app_window;
|
mod app_window;
|
||||||
mod components;
|
mod components;
|
||||||
#[cfg(test)]
|
|
||||||
mod gtk_init;
|
|
||||||
mod types;
|
mod types;
|
||||||
mod view_models;
|
mod view_models;
|
||||||
mod views;
|
mod views;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use chrono::{Local, NaiveDate};
|
use chrono::{Local, NaiveDate};
|
||||||
use dimensioned::si;
|
use dimensioned::si;
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct ParseError;
|
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
|
||||||
|
@ -56,148 +56,66 @@ pub enum FormatOption {
|
||||||
Full,
|
Full,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
||||||
pub struct TimeFormatter(chrono::NaiveTime);
|
|
||||||
|
|
||||||
impl TimeFormatter {
|
|
||||||
pub fn format(&self, option: FormatOption) -> String {
|
|
||||||
match option {
|
|
||||||
FormatOption::Abbreviated => self.0.format("%H:%M"),
|
|
||||||
FormatOption::Full => self.0.format("%H:%M:%S"),
|
|
||||||
}
|
|
||||||
.to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse(s: &str) -> Result<TimeFormatter, ParseError> {
|
|
||||||
let parts = s
|
|
||||||
.split(':')
|
|
||||||
.map(|part| part.parse::<u32>().map_err(|_| ParseError))
|
|
||||||
.collect::<Result<Vec<u32>, ParseError>>()?;
|
|
||||||
match parts.len() {
|
|
||||||
0 => Err(ParseError),
|
|
||||||
1 => Err(ParseError),
|
|
||||||
2 => Ok(TimeFormatter(
|
|
||||||
chrono::NaiveTime::from_hms_opt(parts[0], parts[1], 0).unwrap(),
|
|
||||||
)),
|
|
||||||
3 => Ok(TimeFormatter(
|
|
||||||
chrono::NaiveTime::from_hms_opt(parts[0], parts[1], parts[2]).unwrap(),
|
|
||||||
)),
|
|
||||||
_ => Err(ParseError),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::ops::Deref for TimeFormatter {
|
|
||||||
type Target = chrono::NaiveTime;
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<chrono::NaiveTime> for TimeFormatter {
|
|
||||||
fn from(value: chrono::NaiveTime) -> Self {
|
|
||||||
Self(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
|
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
|
||||||
pub struct WeightFormatter(si::Kilogram<f64>);
|
pub struct Distance {
|
||||||
|
value: si::Meter<f64>,
|
||||||
|
}
|
||||||
|
|
||||||
impl WeightFormatter {
|
impl Distance {
|
||||||
pub fn format(&self, option: FormatOption) -> String {
|
pub fn format(&self, option: FormatOption) -> String {
|
||||||
match option {
|
match option {
|
||||||
FormatOption::Abbreviated => format!("{} kg", self.0.value_unsafe),
|
FormatOption::Abbreviated => format!("{} km", self.value.value_unsafe / 1000.),
|
||||||
FormatOption::Full => format!("{} kilograms", self.0.value_unsafe),
|
FormatOption::Full => format!("{} kilometers", self.value.value_unsafe / 1000.),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(s: &str) -> Result<WeightFormatter, ParseError> {
|
pub fn parse(s: &str) -> Result<Distance, ParseError> {
|
||||||
s.parse::<f64>()
|
let digits = take_digits(s.to_owned());
|
||||||
.map(|w| WeightFormatter(w * si::KG))
|
let value = digits.parse::<f64>().map_err(|_| ParseError)?;
|
||||||
.map_err(|_| ParseError)
|
Ok(Distance {
|
||||||
|
value: value * 1000. * si::M,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Add for WeightFormatter {
|
impl std::ops::Add for Distance {
|
||||||
type Output = WeightFormatter;
|
type Output = Distance;
|
||||||
fn add(self, rside: Self) -> Self::Output {
|
fn add(self, rside: Self) -> Self::Output {
|
||||||
Self::Output::from(self.0 + rside.0)
|
Self::Output::from(self.value + rside.value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Sub for WeightFormatter {
|
impl std::ops::Sub for Distance {
|
||||||
type Output = WeightFormatter;
|
type Output = Distance;
|
||||||
fn sub(self, rside: Self) -> Self::Output {
|
fn sub(self, rside: Self) -> Self::Output {
|
||||||
Self::Output::from(self.0 - rside.0)
|
Self::Output::from(self.value - rside.value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Deref for WeightFormatter {
|
impl std::ops::Deref for Distance {
|
||||||
type Target = si::Kilogram<f64>;
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<si::Kilogram<f64>> for WeightFormatter {
|
|
||||||
fn from(value: si::Kilogram<f64>) -> Self {
|
|
||||||
Self(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
|
|
||||||
pub struct DistanceFormatter(si::Meter<f64>);
|
|
||||||
|
|
||||||
impl DistanceFormatter {
|
|
||||||
pub fn format(&self, option: FormatOption) -> String {
|
|
||||||
match option {
|
|
||||||
FormatOption::Abbreviated => format!("{} km", self.0.value_unsafe / 1000.),
|
|
||||||
FormatOption::Full => format!("{} kilometers", self.0.value_unsafe / 1000.),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse(s: &str) -> Result<DistanceFormatter, ParseError> {
|
|
||||||
let value = s.parse::<f64>().map_err(|_| ParseError)?;
|
|
||||||
Ok(DistanceFormatter(value * 1000. * si::M))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::ops::Add for DistanceFormatter {
|
|
||||||
type Output = DistanceFormatter;
|
|
||||||
fn add(self, rside: Self) -> Self::Output {
|
|
||||||
Self::Output::from(self.0 + rside.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::ops::Sub for DistanceFormatter {
|
|
||||||
type Output = DistanceFormatter;
|
|
||||||
fn sub(self, rside: Self) -> Self::Output {
|
|
||||||
Self::Output::from(self.0 - rside.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::ops::Deref for DistanceFormatter {
|
|
||||||
type Target = si::Meter<f64>;
|
type Target = si::Meter<f64>;
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
&self.0
|
&self.value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<si::Meter<f64>> for DistanceFormatter {
|
impl From<si::Meter<f64>> for Distance {
|
||||||
fn from(value: si::Meter<f64>) -> Self {
|
fn from(value: si::Meter<f64>) -> Self {
|
||||||
Self(value)
|
Self { value }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
|
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
|
||||||
pub struct DurationFormatter(si::Second<f64>);
|
pub struct Duration {
|
||||||
|
value: si::Second<f64>,
|
||||||
|
}
|
||||||
|
|
||||||
impl DurationFormatter {
|
impl Duration {
|
||||||
pub fn format(&self, option: FormatOption) -> String {
|
pub fn format(&self, option: FormatOption) -> String {
|
||||||
let (hours, minutes) = self.hours_and_minutes();
|
let (hours, minutes) = self.hours_and_minutes();
|
||||||
let (h, m) = match option {
|
let (h, m) = match option {
|
||||||
FormatOption::Abbreviated => ("h", "m"),
|
FormatOption::Abbreviated => ("h", "m"),
|
||||||
FormatOption::Full => (" hours", " minutes"),
|
FormatOption::Full => ("hours", "minutes"),
|
||||||
};
|
};
|
||||||
if hours > 0 {
|
if hours > 0 {
|
||||||
format!("{}{} {}{}", hours, h, minutes, m)
|
format!("{}{} {}{}", hours, h, minutes, m)
|
||||||
|
@ -206,134 +124,51 @@ impl DurationFormatter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(s: &str) -> Result<DurationFormatter, ParseError> {
|
pub fn parse(s: &str) -> Result<Duration, ParseError> {
|
||||||
let value = s.parse::<f64>().map_err(|_| ParseError)?;
|
let digits = take_digits(s.to_owned());
|
||||||
Ok(DurationFormatter(value * 60. * si::S))
|
let value = digits.parse::<f64>().map_err(|_| ParseError)?;
|
||||||
|
Ok(Duration {
|
||||||
|
value: value * 60. * si::S,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hours_and_minutes(&self) -> (i64, i64) {
|
fn hours_and_minutes(&self) -> (i64, i64) {
|
||||||
let minutes: i64 = (self.0.value_unsafe / 60.).round() as i64;
|
let minutes: i64 = (self.value.value_unsafe / 60.).round() as i64;
|
||||||
let hours: i64 = minutes / 60;
|
let hours: i64 = minutes / 60;
|
||||||
let minutes = minutes - (hours * 60);
|
let minutes = minutes - (hours * 60);
|
||||||
(hours, minutes)
|
(hours, minutes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Add for DurationFormatter {
|
impl std::ops::Add for Duration {
|
||||||
type Output = DurationFormatter;
|
type Output = Duration;
|
||||||
fn add(self, rside: Self) -> Self::Output {
|
fn add(self, rside: Self) -> Self::Output {
|
||||||
Self::Output::from(self.0 + rside.0)
|
Self::Output::from(self.value + rside.value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Sub for DurationFormatter {
|
impl std::ops::Sub for Duration {
|
||||||
type Output = DurationFormatter;
|
type Output = Duration;
|
||||||
fn sub(self, rside: Self) -> Self::Output {
|
fn sub(self, rside: Self) -> Self::Output {
|
||||||
Self::Output::from(self.0 - rside.0)
|
Self::Output::from(self.value - rside.value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Deref for DurationFormatter {
|
impl std::ops::Deref for Duration {
|
||||||
type Target = si::Second<f64>;
|
type Target = si::Second<f64>;
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
&self.0
|
&self.value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<si::Second<f64>> for DurationFormatter {
|
impl From<si::Second<f64>> for Duration {
|
||||||
fn from(value: si::Second<f64>) -> Self {
|
fn from(value: si::Second<f64>) -> Self {
|
||||||
Self(value)
|
Self { value }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
fn take_digits(s: String) -> String {
|
||||||
mod test {
|
s.chars()
|
||||||
use super::*;
|
.take_while(|t| t.is_ascii_digit())
|
||||||
use dimensioned::si;
|
.collect::<String>()
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_parses_weight_values() {
|
|
||||||
assert_eq!(
|
|
||||||
WeightFormatter::parse("15.3"),
|
|
||||||
Ok(WeightFormatter(15.3 * si::KG))
|
|
||||||
);
|
|
||||||
assert_eq!(WeightFormatter::parse("15.ab"), Err(ParseError));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_formats_weight_values() {
|
|
||||||
assert_eq!(
|
|
||||||
WeightFormatter::from(15.3 * si::KG).format(FormatOption::Abbreviated),
|
|
||||||
"15.3 kg"
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
WeightFormatter::from(15.3 * si::KG).format(FormatOption::Full),
|
|
||||||
"15.3 kilograms"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_parses_distance_values() {
|
|
||||||
assert_eq!(
|
|
||||||
DistanceFormatter::parse("70"),
|
|
||||||
Ok(DistanceFormatter(70000. * si::M))
|
|
||||||
);
|
|
||||||
assert_eq!(DistanceFormatter::parse("15.ab"), Err(ParseError));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_formats_distance_values() {
|
|
||||||
assert_eq!(
|
|
||||||
DistanceFormatter::from(70000. * si::M).format(FormatOption::Abbreviated),
|
|
||||||
"70 km"
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
DistanceFormatter::from(70000. * si::M).format(FormatOption::Full),
|
|
||||||
"70 kilometers"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_parses_duration_values() {
|
|
||||||
assert_eq!(
|
|
||||||
DurationFormatter::parse("70"),
|
|
||||||
Ok(DurationFormatter(4200. * si::S))
|
|
||||||
);
|
|
||||||
assert_eq!(DurationFormatter::parse("15.ab"), Err(ParseError));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_formats_duration_values() {
|
|
||||||
assert_eq!(
|
|
||||||
DurationFormatter::from(4200. * si::S).format(FormatOption::Abbreviated),
|
|
||||||
"1h 10m"
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
DurationFormatter::from(4200. * si::S).format(FormatOption::Full),
|
|
||||||
"1 hours 10 minutes"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_parses_time_values() {
|
|
||||||
assert_eq!(
|
|
||||||
TimeFormatter::parse("13:25"),
|
|
||||||
Ok(TimeFormatter::from(
|
|
||||||
chrono::NaiveTime::from_hms_opt(13, 25, 0).unwrap()
|
|
||||||
)),
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
TimeFormatter::parse("13:25:50"),
|
|
||||||
Ok(TimeFormatter::from(
|
|
||||||
chrono::NaiveTime::from_hms_opt(13, 25, 50).unwrap()
|
|
||||||
)),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_formats_time_values() {
|
|
||||||
let time = TimeFormatter::from(chrono::NaiveTime::from_hms_opt(13, 25, 50).unwrap());
|
|
||||||
assert_eq!(time.format(FormatOption::Abbreviated), "13:25".to_owned());
|
|
||||||
assert_eq!(time.format(FormatOption::Full), "13:25:50".to_owned());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,9 @@ You should have received a copy of the GNU General Public License along with Fit
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
app::App,
|
app::App,
|
||||||
types::{DistanceFormatter, DurationFormatter, WeightFormatter},
|
types::{Distance, Duration},
|
||||||
};
|
};
|
||||||
|
use dimensioned::si;
|
||||||
use emseries::{Record, RecordId, Recordable};
|
use emseries::{Record, RecordId, Recordable};
|
||||||
use ft_core::{RecordType, TimeDistance, TraxRecord};
|
use ft_core::{RecordType, TimeDistance, TraxRecord};
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -115,24 +116,22 @@ impl DayDetailViewModel {
|
||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn weight(&self) -> Option<WeightFormatter> {
|
pub fn weight(&self) -> Option<si::Kilogram<f64>> {
|
||||||
(*self.weight.read().unwrap())
|
(*self.weight.read().unwrap()).as_ref().map(|w| w.weight)
|
||||||
.as_ref()
|
|
||||||
.map(|w| WeightFormatter::from(w.weight))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_weight(&self, new_weight: WeightFormatter) {
|
pub fn set_weight(&self, new_weight: si::Kilogram<f64>) {
|
||||||
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(Record {
|
None => RecordState::New(Record {
|
||||||
id: RecordId::default(),
|
id: RecordId::default(),
|
||||||
data: ft_core::Weight {
|
data: ft_core::Weight {
|
||||||
date: self.date,
|
date: self.date,
|
||||||
weight: *new_weight,
|
weight: new_weight,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
@ -161,9 +160,9 @@ impl DayDetailViewModel {
|
||||||
*record = Some(new_record);
|
*record = Some(new_record);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn biking_summary(&self) -> (DistanceFormatter, DurationFormatter) {
|
pub fn biking_summary(&self) -> (Distance, Duration) {
|
||||||
self.records.read().unwrap().iter().fold(
|
self.records.read().unwrap().iter().fold(
|
||||||
(DistanceFormatter::default(), DurationFormatter::default()),
|
(Distance::default(), Duration::default()),
|
||||||
|(acc_distance, acc_duration), (_, record)| match record.data() {
|
|(acc_distance, acc_duration), (_, record)| match record.data() {
|
||||||
Some(Record {
|
Some(Record {
|
||||||
data:
|
data:
|
||||||
|
@ -173,10 +172,10 @@ impl DayDetailViewModel {
|
||||||
..
|
..
|
||||||
}) => (
|
}) => (
|
||||||
distance
|
distance
|
||||||
.map(|distance| acc_distance + DistanceFormatter::from(distance))
|
.map(|distance| acc_distance + Distance::from(distance))
|
||||||
.unwrap_or(acc_distance),
|
.unwrap_or(acc_distance),
|
||||||
duration
|
duration
|
||||||
.map(|duration| acc_duration + DurationFormatter::from(duration))
|
.map(|duration| acc_duration + Duration::from(duration))
|
||||||
.unwrap_or(acc_duration),
|
.unwrap_or(acc_duration),
|
||||||
),
|
),
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue