Set up text entry fields for all of the common metrics

This commit is contained in:
Savanni D'Gerinel 2024-01-31 08:56:54 -05:00
parent bc31522c95
commit 772188b470
5 changed files with 72 additions and 23 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, WeightLabel}, components::{steps_editor, weight_field, ActionGroup, Steps, WeightLabel},
view_models::DayDetailViewModel, view_models::DayDetailViewModel,
}; };
use glib::Object; use glib::Object;
@ -283,7 +283,7 @@ impl DayEdit {
.orientation(gtk::Orientation::Horizontal) .orientation(gtk::Orientation::Horizontal)
.build(); .build();
top_row.append( top_row.append(
&weight_editor(view_model.weight(), { &weight_field(view_model.weight(), {
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),

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::TextEntry; pub use text_entry::{weight_field, 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, WeightLabel}; pub use weight::WeightLabel;
use glib::Object; use glib::Object;
use gtk::{prelude::*, subclass::prelude::*}; use gtk::{prelude::*, subclass::prelude::*};

View File

@ -14,7 +14,9 @@ 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 crate::types::{
DistanceFormatter, DurationFormatter, FormatOption, ParseError, TimeFormatter, WeightFormatter,
};
use gtk::prelude::*; use gtk::prelude::*;
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
@ -104,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)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View File

@ -43,19 +43,3 @@ impl WeightLabel {
self.label.clone().upcast() self.label.clone().upcast()
} }
} }
pub fn weight_editor<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,
)
}

View File

@ -60,7 +60,7 @@ pub enum FormatOption {
pub struct TimeFormatter(chrono::NaiveTime); pub struct TimeFormatter(chrono::NaiveTime);
impl TimeFormatter { impl TimeFormatter {
fn format(&self, option: FormatOption) -> String { pub fn format(&self, option: FormatOption) -> String {
match option { match option {
FormatOption::Abbreviated => self.0.format("%H:%M"), FormatOption::Abbreviated => self.0.format("%H:%M"),
FormatOption::Full => self.0.format("%H:%M:%S"), FormatOption::Full => self.0.format("%H:%M:%S"),
@ -68,7 +68,7 @@ impl TimeFormatter {
.to_string() .to_string()
} }
fn parse(s: &str) -> Result<TimeFormatter, ParseError> { pub fn parse(s: &str) -> Result<TimeFormatter, ParseError> {
let parts = s let parts = s
.split(':') .split(':')
.map(|part| part.parse::<u32>().map_err(|_| ParseError)) .map(|part| part.parse::<u32>().map_err(|_| ParseError))