Improve the TextEntry field #178

Merged
savanni merged 3 commits from fitnesstrax/text-entry-improvements into main 2024-01-31 14:04:08 +00:00
5 changed files with 72 additions and 23 deletions
Showing only changes of commit 772188b470 - Show all commits

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, weight_editor, ActionGroup, Steps, WeightLabel},
components::{steps_editor, weight_field, ActionGroup, Steps, WeightLabel},
view_models::DayDetailViewModel,
};
use glib::Object;
@ -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),

View File

@ -27,13 +27,13 @@ mod steps;
pub use steps::{steps_editor, Steps};
mod text_entry;
pub use text_entry::TextEntry;
pub use text_entry::{weight_field, TextEntry};
mod time_distance;
pub use time_distance::TimeDistanceView;
mod weight;
pub use weight::{weight_editor, WeightLabel};
pub use weight::WeightLabel;
use glib::Object;
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/>.
*/
use crate::types::ParseError;
use crate::types::{
DistanceFormatter, DurationFormatter, FormatOption, ParseError, TimeFormatter, WeightFormatter,
};
use gtk::prelude::*;
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)]
mod test {
use super::*;

View File

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