Build some convenienc functions for measurement entry fields
Move the weight field into text_entry
This commit is contained in:
parent
22ba4f575d
commit
3a716ee546
|
@ -17,7 +17,10 @@ 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, time_distance_summary, weight_editor, ActionGroup, Steps, Weight},
|
||||
components::{
|
||||
steps_editor, text_entry::distance_field, time_distance_summary, weight_field, ActionGroup,
|
||||
Steps, Weight,
|
||||
},
|
||||
view_models::DayDetailViewModel,
|
||||
};
|
||||
use dimensioned::si;
|
||||
|
@ -268,7 +271,7 @@ fn weight_and_steps_row(view_model: &DayDetailViewModel) -> gtk::Box {
|
|||
.orientation(gtk::Orientation::Horizontal)
|
||||
.build();
|
||||
row.append(
|
||||
&weight_editor(view_model.weight(), {
|
||||
&weight_field(view_model.weight(), {
|
||||
let view_model = view_model.clone();
|
||||
move |w| {
|
||||
view_model.set_weight(w);
|
||||
|
|
|
@ -27,13 +27,15 @@ mod steps;
|
|||
pub use steps::{steps_editor, Steps};
|
||||
|
||||
mod text_entry;
|
||||
pub use text_entry::{ParseError, TextEntry};
|
||||
pub use text_entry::{
|
||||
distance_field, duration_field, time_field, weight_field, ParseError, TextEntry,
|
||||
};
|
||||
|
||||
mod time_distance;
|
||||
pub use time_distance::{time_distance_detail, time_distance_summary};
|
||||
|
||||
mod weight;
|
||||
pub use weight::{weight_editor, Weight};
|
||||
pub use weight::Weight;
|
||||
|
||||
use glib::Object;
|
||||
use gtk::{prelude::*, subclass::prelude::*};
|
||||
|
|
|
@ -14,6 +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 dimensioned::si;
|
||||
use gtk::prelude::*;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
|
@ -27,7 +28,6 @@ type Parser<T> = dyn Fn(&str) -> Result<T, ParseError>;
|
|||
pub struct TextEntry<T: Clone + std::fmt::Debug> {
|
||||
value: Rc<RefCell<Option<T>>>,
|
||||
widget: gtk::Entry,
|
||||
#[allow(unused)]
|
||||
renderer: Rc<Renderer<T>>,
|
||||
parser: Rc<Parser<T>>,
|
||||
}
|
||||
|
@ -109,3 +109,67 @@ impl<T: Clone + std::fmt::Debug + 'static> TextEntry<T> {
|
|||
self.widget.clone().upcast::<gtk::Widget>()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn weight_field<OnUpdate>(
|
||||
weight: Option<si::Kilogram<f64>>,
|
||||
on_update: OnUpdate,
|
||||
) -> TextEntry<si::Kilogram<f64>>
|
||||
where
|
||||
OnUpdate: Fn(si::Kilogram<f64>) + 'static,
|
||||
{
|
||||
TextEntry::new(
|
||||
"0 kg",
|
||||
weight,
|
||||
|val: &si::Kilogram<f64>| val.to_string(),
|
||||
move |v: &str| {
|
||||
let new_weight = v.parse::<f64>().map(|w| w * si::KG).map_err(|_| ParseError);
|
||||
match new_weight {
|
||||
Ok(w) => {
|
||||
on_update(w);
|
||||
Ok(w)
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub fn time_field(value: chrono::NaiveTime) -> TextEntry<chrono::NaiveTime> {
|
||||
TextEntry::new(
|
||||
"hh:mm",
|
||||
Some(value),
|
||||
|v| v.format("%H:%M").to_string(),
|
||||
|s| chrono::NaiveTime::parse_from_str(s, "%H:%M").map_err(|_| ParseError),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn distance_field(value: Option<si::Meter<f64>>) -> TextEntry<si::Meter<f64>> {
|
||||
TextEntry::new(
|
||||
"0 km",
|
||||
value,
|
||||
|v| format!("{} km", v.value_unsafe / 1000.),
|
||||
|s| {
|
||||
let digits = take_digits(s.to_owned());
|
||||
let value = digits.parse::<f64>().map_err(|_| ParseError)?;
|
||||
println!("value: {}", value);
|
||||
Ok(value * 1000. * si::M)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub fn duration_field(value: Option<si::Second<f64>>) -> TextEntry<si::Second<f64>> {
|
||||
TextEntry::new(
|
||||
"0 minutes",
|
||||
value,
|
||||
|v| format!("{} minutes", v.value_unsafe / 1000.),
|
||||
|s| {
|
||||
let digits = take_digits(s.to_owned());
|
||||
let value = digits.parse::<f64>().map_err(|_| ParseError)?;
|
||||
Ok(value * 60. * si::S)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn take_digits(s: String) -> String {
|
||||
s.chars().take_while(|t| t.is_digit(10)).collect::<String>()
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License along with Fit
|
|||
// use crate::components::{EditView, ParseError, TextEntry};
|
||||
// use chrono::{Local, NaiveDate};
|
||||
// use dimensioned::si;
|
||||
use crate::components::distance_field;
|
||||
use dimensioned::si;
|
||||
use ft_core::{RecordType, TimeDistance};
|
||||
use glib::Object;
|
||||
|
@ -102,5 +103,54 @@ pub fn time_distance_detail(type_: ft_core::RecordType, record: ft_core::TimeDis
|
|||
)
|
||||
.build(),
|
||||
);
|
||||
|
||||
layout
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct TimeDistanceEditPrivate {
|
||||
type_: RefCell<Option<ft_core::RecordType>>,
|
||||
record: RefCell<Option<ft_core::RecordType>>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
impl ObjectSubclass for TimeDistanceEditPrivate {
|
||||
const NAME: &'static str = "TimeDistanceEdit";
|
||||
type Type = TimeDistanceEdit;
|
||||
type ParentType = gtk::Box;
|
||||
}
|
||||
|
||||
impl ObjectImpl for TimeDistanceEditPrivate {}
|
||||
impl WidgetImpl for TimeDistanceEditPrivate {}
|
||||
impl BoxImpl for TimeDistanceEditPrivate {}
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct TimeDistanceEdit(ObjectSubclass<TimeDistanceEditPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
|
||||
}
|
||||
|
||||
impl Default for TimeDistanceEdit {
|
||||
fn default() -> Self {
|
||||
let s: Self = Object::builder().build();
|
||||
s.set_orientation(gtk::Orientation::Horizontal);
|
||||
s.set_css_classes(&["time-distance-edit"]);
|
||||
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
impl TimeDistanceEdit {
|
||||
fn empty<OnUpdate>(on_update: OnUpdate) -> Self
|
||||
where
|
||||
OnUpdate: Fn(&ft_core::RecordType, &ft_core::TimeDistance),
|
||||
{
|
||||
let s = Self::default();
|
||||
|
||||
s
|
||||
}
|
||||
|
||||
/*
|
||||
fn with_record<OnUpdate>(type_: ft_core::RecordType, record: ft_core::TimeDistance, on_update: OnUpdate) -> Self
|
||||
where OnUpdate: Fn(&ft_core::RecordType, &ft_core::TimeDistance) {
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
|
|
@ -14,7 +14,6 @@ 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 gtk::prelude::*;
|
||||
|
||||
|
@ -41,27 +40,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(si::Kilogram<f64>) + 'static,
|
||||
{
|
||||
TextEntry::new(
|
||||
"0 kg",
|
||||
weight,
|
||||
|val: &si::Kilogram<f64>| val.to_string(),
|
||||
move |v: &str| {
|
||||
let new_weight = v.parse::<f64>().map(|w| w * si::KG).map_err(|_| ParseError);
|
||||
match new_weight {
|
||||
Ok(w) => {
|
||||
on_update(w);
|
||||
Ok(w)
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
|
@ -56,6 +56,7 @@ impl ObjectSubclass for HistoricalViewPrivate {
|
|||
.single_click_activate(true)
|
||||
.build(),
|
||||
};
|
||||
|
||||
factory.connect_bind({
|
||||
let app = s.app.clone();
|
||||
move |_, list_item| {
|
||||
|
@ -122,9 +123,7 @@ impl HistoricalView {
|
|||
s.imp().list_view.connect_activate({
|
||||
let on_select_day = on_select_day.clone();
|
||||
move |s, idx| {
|
||||
// This gets triggered whenever the user clicks on an item on the list. What we
|
||||
// actually want to do here is to open a modal dialog that shows all of the details of
|
||||
// the day and which allows the user to edit items within that dialog.
|
||||
// This gets triggered whenever the user clicks on an item on the list.
|
||||
let item = s.model().unwrap().item(idx).unwrap();
|
||||
let records = item.downcast_ref::<DayRecords>().unwrap();
|
||||
on_select_day(records.date(), records.records());
|
||||
|
|
Loading…
Reference in New Issue