Compare commits
8 Commits
abac2d4df9
...
772188b470
Author | SHA1 | Date |
---|---|---|
Savanni D'Gerinel | 772188b470 | |
Savanni D'Gerinel | bc31522c95 | |
Savanni D'Gerinel | 6c68564a77 | |
Savanni D'Gerinel | 55c1a6372f | |
Savanni D'Gerinel | 2cbd539bf4 | |
Savanni D'Gerinel | 7d14308def | |
Savanni D'Gerinel | dcd6301bb9 | |
Savanni D'Gerinel | 69567db486 |
|
@ -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, weight_editor, ActionGroup, Steps, Weight},
|
||||
components::{steps_editor, weight_field, ActionGroup, Steps, WeightLabel},
|
||||
view_models::DayDetailViewModel,
|
||||
};
|
||||
use glib::Object;
|
||||
|
@ -162,7 +162,7 @@ impl DayDetail {
|
|||
let top_row = gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Horizontal)
|
||||
.build();
|
||||
let weight_view = Weight::new(view_model.weight());
|
||||
let weight_view = WeightLabel::new(view_model.weight());
|
||||
top_row.append(&weight_view.widget());
|
||||
|
||||
let steps_view = Steps::new(view_model.steps());
|
||||
|
@ -283,7 +283,7 @@ impl DayEdit {
|
|||
.orientation(gtk::Orientation::Horizontal)
|
||||
.build();
|
||||
top_row.append(
|
||||
&weight_editor(view_model.weight(), {
|
||||
&weight_field(view_model.weight(), {
|
||||
let view_model = view_model.clone();
|
||||
move |w| match w {
|
||||
Some(w) => view_model.set_weight(w),
|
||||
|
|
|
@ -27,13 +27,13 @@ mod steps;
|
|||
pub use steps::{steps_editor, Steps};
|
||||
|
||||
mod text_entry;
|
||||
pub use text_entry::{ParseError, TextEntry};
|
||||
pub use text_entry::{weight_field, TextEntry};
|
||||
|
||||
mod time_distance;
|
||||
pub use time_distance::TimeDistanceView;
|
||||
|
||||
mod weight;
|
||||
pub use weight::{weight_editor, Weight};
|
||||
pub use weight::WeightLabel;
|
||||
|
||||
use glib::Object;
|
||||
use gtk::{prelude::*, subclass::prelude::*};
|
||||
|
|
|
@ -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/>.
|
||||
*/
|
||||
|
||||
use crate::components::{ParseError, TextEntry};
|
||||
use crate::{components::TextEntry, types::ParseError};
|
||||
use gtk::prelude::*;
|
||||
|
||||
#[derive(Default)]
|
||||
|
|
|
@ -14,12 +14,12 @@ 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/>.
|
||||
*/
|
||||
|
||||
use crate::types::{
|
||||
DistanceFormatter, DurationFormatter, FormatOption, ParseError, TimeFormatter, WeightFormatter,
|
||||
};
|
||||
use gtk::prelude::*;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ParseError;
|
||||
|
||||
type Parser<T> = dyn Fn(&str) -> Result<T, ParseError>;
|
||||
type OnUpdate<T> = dyn Fn(Option<T>);
|
||||
|
||||
|
@ -106,6 +106,69 @@ impl<T: Clone + std::fmt::Debug + 'static> TextEntry<T> {
|
|||
}
|
||||
}
|
||||
|
||||
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>(
|
||||
weight: Option<WeightFormatter>,
|
||||
on_update: OnUpdate,
|
||||
) -> TextEntry<WeightFormatter>
|
||||
where
|
||||
OnUpdate: Fn(Option<WeightFormatter>) + 'static,
|
||||
{
|
||||
TextEntry::new(
|
||||
"0 kg",
|
||||
weight,
|
||||
|val| val.format(FormatOption::Abbreviated),
|
||||
WeightFormatter::parse,
|
||||
on_update,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
@ -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/>.
|
||||
*/
|
||||
|
||||
use crate::components::{ParseError, TextEntry};
|
||||
use dimensioned::si;
|
||||
use crate::{
|
||||
components::TextEntry,
|
||||
types::{FormatOption, WeightFormatter},
|
||||
};
|
||||
use gtk::prelude::*;
|
||||
|
||||
pub struct Weight {
|
||||
pub struct WeightLabel {
|
||||
label: gtk::Label,
|
||||
}
|
||||
|
||||
impl Weight {
|
||||
pub fn new(weight: Option<si::Kilogram<f64>>) -> Self {
|
||||
impl WeightLabel {
|
||||
pub fn new(weight: Option<WeightFormatter>) -> Self {
|
||||
let label = gtk::Label::builder()
|
||||
.css_classes(["card", "weight-view"])
|
||||
.can_focus(true)
|
||||
.build();
|
||||
|
||||
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"),
|
||||
}
|
||||
|
||||
|
@ -41,19 +43,3 @@ impl Weight {
|
|||
self.label.clone().upcast()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn weight_editor<OnUpdate>(
|
||||
weight: Option<si::Kilogram<f64>>,
|
||||
on_update: OnUpdate,
|
||||
) -> TextEntry<si::Kilogram<f64>>
|
||||
where
|
||||
OnUpdate: Fn(Option<si::Kilogram<f64>>) + 'static,
|
||||
{
|
||||
TextEntry::new(
|
||||
"0 kg",
|
||||
weight,
|
||||
|val: &si::Kilogram<f64>| val.to_string(),
|
||||
move |v: &str| v.parse::<f64>().map(|w| w * si::KG).map_err(|_| ParseError),
|
||||
on_update,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
use chrono::{Duration, Local, NaiveDate};
|
||||
use chrono::{Local, NaiveDate};
|
||||
use dimensioned::si;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct ParseError;
|
||||
|
||||
// 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
|
||||
|
@ -12,7 +16,7 @@ pub struct DayInterval {
|
|||
impl Default for DayInterval {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
start: (Local::now() - Duration::days(7)).date_naive(),
|
||||
start: (Local::now() - chrono::Duration::days(7)).date_naive(),
|
||||
end: Local::now().date_naive(),
|
||||
}
|
||||
}
|
||||
|
@ -38,10 +42,306 @@ impl Iterator for DayIterator {
|
|||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.current <= self.end {
|
||||
let val = self.current;
|
||||
self.current += Duration::days(1);
|
||||
self.current += chrono::Duration::days(1);
|
||||
Some(val)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum FormatOption {
|
||||
Abbreviated,
|
||||
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)]
|
||||
pub struct WeightFormatter(si::Kilogram<f64>);
|
||||
|
||||
impl WeightFormatter {
|
||||
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<WeightFormatter, ParseError> {
|
||||
s.parse::<f64>()
|
||||
.map(|w| WeightFormatter(w * si::KG))
|
||||
.map_err(|_| ParseError)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Add for WeightFormatter {
|
||||
type Output = WeightFormatter;
|
||||
fn add(self, rside: Self) -> Self::Output {
|
||||
Self::Output::from(self.0 + rside.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Sub for WeightFormatter {
|
||||
type Output = WeightFormatter;
|
||||
fn sub(self, rside: Self) -> Self::Output {
|
||||
Self::Output::from(self.0 - rside.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Deref for WeightFormatter {
|
||||
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>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<si::Meter<f64>> for DistanceFormatter {
|
||||
fn from(value: si::Meter<f64>) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
|
||||
pub struct DurationFormatter(si::Second<f64>);
|
||||
|
||||
impl DurationFormatter {
|
||||
pub fn format(&self, option: FormatOption) -> String {
|
||||
let (hours, minutes) = self.hours_and_minutes();
|
||||
let (h, m) = match option {
|
||||
FormatOption::Abbreviated => ("h", "m"),
|
||||
FormatOption::Full => (" hours", " minutes"),
|
||||
};
|
||||
if hours > 0 {
|
||||
format!("{}{} {}{}", hours, h, minutes, m)
|
||||
} else {
|
||||
format!("{}{}", minutes, m)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(s: &str) -> Result<DurationFormatter, ParseError> {
|
||||
let value = s.parse::<f64>().map_err(|_| ParseError)?;
|
||||
Ok(DurationFormatter(value * 60. * si::S))
|
||||
}
|
||||
|
||||
fn hours_and_minutes(&self) -> (i64, i64) {
|
||||
let minutes: i64 = (self.0.value_unsafe / 60.).round() as i64;
|
||||
let hours: i64 = minutes / 60;
|
||||
let minutes = minutes - (hours * 60);
|
||||
(hours, minutes)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Add for DurationFormatter {
|
||||
type Output = DurationFormatter;
|
||||
fn add(self, rside: Self) -> Self::Output {
|
||||
Self::Output::from(self.0 + rside.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Sub for DurationFormatter {
|
||||
type Output = DurationFormatter;
|
||||
fn sub(self, rside: Self) -> Self::Output {
|
||||
Self::Output::from(self.0 - rside.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Deref for DurationFormatter {
|
||||
type Target = si::Second<f64>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<si::Second<f64>> for DurationFormatter {
|
||||
fn from(value: si::Second<f64>) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
fn take_digits(s: String) -> String {
|
||||
s.chars()
|
||||
.take_while(|t| t.is_ascii_digit())
|
||||
.collect::<String>()
|
||||
}
|
||||
*/
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use dimensioned::si;
|
||||
|
||||
#[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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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/>.
|
||||
*/
|
||||
|
||||
use crate::app::App;
|
||||
use dimensioned::si;
|
||||
use crate::{app::App, types::WeightFormatter};
|
||||
use emseries::{Record, RecordId, Recordable};
|
||||
use ft_core::TraxRecord;
|
||||
use std::{
|
||||
|
@ -125,20 +124,22 @@ impl DayDetailViewModel {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn weight(&self) -> Option<si::Kilogram<f64>> {
|
||||
(*self.weight.read().unwrap()).as_ref().map(|w| w.weight)
|
||||
pub fn weight(&self) -> Option<WeightFormatter> {
|
||||
(*self.weight.read().unwrap())
|
||||
.as_ref()
|
||||
.map(|w| WeightFormatter::from(w.weight))
|
||||
}
|
||||
|
||||
pub fn set_weight(&self, new_weight: si::Kilogram<f64>) {
|
||||
pub fn set_weight(&self, new_weight: WeightFormatter) {
|
||||
let mut record = self.weight.write().unwrap();
|
||||
let new_record = match *record {
|
||||
Some(ref rstate) => rstate.clone().with_value(ft_core::Weight {
|
||||
date: self.date,
|
||||
weight: new_weight,
|
||||
weight: *new_weight,
|
||||
}),
|
||||
None => RecordState::New(ft_core::Weight {
|
||||
date: self.date,
|
||||
weight: new_weight,
|
||||
weight: *new_weight,
|
||||
}),
|
||||
};
|
||||
*record = Some(new_record);
|
||||
|
|
Loading…
Reference in New Issue