Compare commits
No commits in common. "772188b4706fad590c3d51aa63af2192f5f73e12" and "55c1a6372fd35a466872c3ae2f0d36f2fe66d556" have entirely different histories.
772188b470
...
55c1a6372f
|
@ -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_field, ActionGroup, Steps, WeightLabel},
|
components::{steps_editor, weight_editor, ActionGroup, Steps, WeightLabel},
|
||||||
view_models::DayDetailViewModel,
|
view_models::DayDetailViewModel,
|
||||||
};
|
};
|
||||||
use glib::Object;
|
use glib::Object;
|
||||||
|
@ -283,11 +283,10 @@ impl DayEdit {
|
||||||
.orientation(gtk::Orientation::Horizontal)
|
.orientation(gtk::Orientation::Horizontal)
|
||||||
.build();
|
.build();
|
||||||
top_row.append(
|
top_row.append(
|
||||||
&weight_field(view_model.weight(), {
|
&weight_editor(view_model.weight(), {
|
||||||
let view_model = view_model.clone();
|
let view_model = view_model.clone();
|
||||||
move |w| match w {
|
move |w| {
|
||||||
Some(w) => view_model.set_weight(w),
|
view_model.set_weight(w);
|
||||||
None => eprintln!("have not implemented record delete"),
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.widget(),
|
.widget(),
|
||||||
|
@ -296,10 +295,7 @@ impl DayEdit {
|
||||||
top_row.append(
|
top_row.append(
|
||||||
&steps_editor(view_model.steps(), {
|
&steps_editor(view_model.steps(), {
|
||||||
let view_model = view_model.clone();
|
let view_model = view_model.clone();
|
||||||
move |s| match s {
|
move |s| view_model.set_steps(s)
|
||||||
Some(s) => view_model.set_steps(s),
|
|
||||||
None => eprintln!("have not implemented record delete"),
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.widget(),
|
.widget(),
|
||||||
);
|
);
|
||||||
|
|
|
@ -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::{weight_field, TextEntry};
|
pub use text_entry::TextEntry;
|
||||||
|
|
||||||
mod time_distance;
|
mod time_distance;
|
||||||
pub use time_distance::TimeDistanceView;
|
pub use time_distance::TimeDistanceView;
|
||||||
|
|
||||||
mod weight;
|
mod weight;
|
||||||
pub use weight::WeightLabel;
|
pub use weight::{weight_editor, WeightLabel};
|
||||||
|
|
||||||
use glib::Object;
|
use glib::Object;
|
||||||
use gtk::{prelude::*, subclass::prelude::*};
|
use gtk::{prelude::*, subclass::prelude::*};
|
||||||
|
|
|
@ -44,13 +44,18 @@ impl Steps {
|
||||||
|
|
||||||
pub fn steps_editor<OnUpdate>(value: Option<u32>, on_update: OnUpdate) -> TextEntry<u32>
|
pub fn steps_editor<OnUpdate>(value: Option<u32>, on_update: OnUpdate) -> TextEntry<u32>
|
||||||
where
|
where
|
||||||
OnUpdate: Fn(Option<u32>) + 'static,
|
OnUpdate: Fn(u32) + 'static,
|
||||||
{
|
{
|
||||||
TextEntry::new(
|
TextEntry::new(
|
||||||
"0",
|
"0",
|
||||||
value,
|
value,
|
||||||
|v| format!("{}", v),
|
|v| format!("{}", v),
|
||||||
move |v| v.parse::<u32>().map_err(|_| ParseError),
|
move |v| match v.parse::<u32>() {
|
||||||
on_update,
|
Ok(val) => {
|
||||||
|
on_update(val);
|
||||||
|
Ok(val)
|
||||||
|
}
|
||||||
|
Err(_) => Err(ParseError),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,22 +14,20 @@ 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::ParseError;
|
||||||
DistanceFormatter, DurationFormatter, FormatOption, ParseError, TimeFormatter, WeightFormatter,
|
|
||||||
};
|
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
|
type Renderer<T> = dyn Fn(&T) -> String;
|
||||||
type Parser<T> = dyn Fn(&str) -> Result<T, ParseError>;
|
type Parser<T> = dyn Fn(&str) -> Result<T, ParseError>;
|
||||||
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,
|
||||||
|
#[allow(unused)]
|
||||||
|
renderer: Rc<Renderer<T>>,
|
||||||
parser: Rc<Parser<T>>,
|
parser: Rc<Parser<T>>,
|
||||||
on_update: Rc<OnUpdate<T>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Clone + std::fmt::Debug> std::fmt::Debug for TextEntry<T> {
|
impl<T: Clone + std::fmt::Debug> std::fmt::Debug for TextEntry<T> {
|
||||||
|
@ -44,17 +42,10 @@ impl<T: Clone + std::fmt::Debug> std::fmt::Debug for TextEntry<T> {
|
||||||
|
|
||||||
// I do not understand why the data should be 'static.
|
// I do not understand why the data should be 'static.
|
||||||
impl<T: Clone + std::fmt::Debug + 'static> TextEntry<T> {
|
impl<T: Clone + std::fmt::Debug + 'static> TextEntry<T> {
|
||||||
pub fn new<R, V, U>(
|
pub fn new<R, V>(placeholder: &str, value: Option<T>, renderer: R, parser: V) -> Self
|
||||||
placeholder: &str,
|
|
||||||
value: Option<T>,
|
|
||||||
renderer: R,
|
|
||||||
parser: V,
|
|
||||||
on_update: U,
|
|
||||||
) -> Self
|
|
||||||
where
|
where
|
||||||
R: Fn(&T) -> String + 'static,
|
R: Fn(&T) -> String + 'static,
|
||||||
V: Fn(&str) -> Result<T, ParseError> + 'static,
|
V: Fn(&str) -> Result<T, ParseError> + 'static,
|
||||||
U: Fn(Option<T>) + 'static,
|
|
||||||
{
|
{
|
||||||
let widget = gtk::Entry::builder().placeholder_text(placeholder).build();
|
let widget = gtk::Entry::builder().placeholder_text(placeholder).build();
|
||||||
if let Some(ref v) = value {
|
if let Some(ref v) = value {
|
||||||
|
@ -64,8 +55,8 @@ impl<T: Clone + std::fmt::Debug + 'static> TextEntry<T> {
|
||||||
let s = Self {
|
let s = Self {
|
||||||
value: Rc::new(RefCell::new(value)),
|
value: Rc::new(RefCell::new(value)),
|
||||||
widget,
|
widget,
|
||||||
|
renderer: Rc::new(renderer),
|
||||||
parser: Rc::new(parser),
|
parser: Rc::new(parser),
|
||||||
on_update: Rc::new(on_update),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
s.widget.buffer().connect_text_notify({
|
s.widget.buffer().connect_text_notify({
|
||||||
|
@ -80,14 +71,12 @@ 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()) {
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
*self.value.borrow_mut() = Some(v.clone());
|
*self.value.borrow_mut() = Some(v);
|
||||||
self.widget.remove_css_class("error");
|
self.widget.remove_css_class("error");
|
||||||
(self.on_update)(Some(v));
|
|
||||||
}
|
}
|
||||||
// need to change the border to provide a visual indicator of an error
|
// need to change the border to provide a visual indicator of an error
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
|
@ -96,147 +85,25 @@ impl<T: Clone + std::fmt::Debug + 'static> TextEntry<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
pub fn value(&self) -> Option<T> {
|
||||||
|
let v = self.value.borrow().clone();
|
||||||
|
self.value.borrow().clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_value(&self, value: Option<T>) {
|
||||||
|
if let Some(ref v) = value {
|
||||||
|
self.widget.set_text(&(self.renderer)(v))
|
||||||
|
}
|
||||||
|
*self.value.borrow_mut() = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
pub fn grab_focus(&self) {
|
||||||
|
self.widget.grab_focus();
|
||||||
|
}
|
||||||
|
|
||||||
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>(
|
|
||||||
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::*;
|
|
||||||
use crate::gtk_init::gtk_init;
|
|
||||||
|
|
||||||
fn setup_u32_entry() -> (Rc<RefCell<Option<u32>>>, TextEntry<u32>) {
|
|
||||||
let current_value = Rc::new(RefCell::new(None));
|
|
||||||
|
|
||||||
let entry = TextEntry::new(
|
|
||||||
"step count",
|
|
||||||
None,
|
|
||||||
|steps| format!("{}", steps),
|
|
||||||
|v| v.parse::<u32>().map_err(|_| ParseError),
|
|
||||||
{
|
|
||||||
let current_value = current_value.clone();
|
|
||||||
move |v| *current_value.borrow_mut() = v
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
(current_value, entry)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_responds_to_field_changes() {
|
|
||||||
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("15");
|
|
||||||
assert_eq!(*current_value.borrow(), Some(15));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_preserves_last_value_in_parse_error() {
|
|
||||||
crate::gtk_init::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("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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,3 +43,27 @@ 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(WeightFormatter) + 'static,
|
||||||
|
{
|
||||||
|
TextEntry::new(
|
||||||
|
"0 kg",
|
||||||
|
weight,
|
||||||
|
|val: &WeightFormatter| val.format(FormatOption::Abbreviated),
|
||||||
|
move |v: &str| {
|
||||||
|
let new_weight = WeightFormatter::parse(v);
|
||||||
|
match new_weight {
|
||||||
|
Ok(w) => {
|
||||||
|
on_update(w);
|
||||||
|
Ok(w)
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -60,7 +60,7 @@ pub enum FormatOption {
|
||||||
pub struct TimeFormatter(chrono::NaiveTime);
|
pub struct TimeFormatter(chrono::NaiveTime);
|
||||||
|
|
||||||
impl TimeFormatter {
|
impl TimeFormatter {
|
||||||
pub fn format(&self, option: FormatOption) -> String {
|
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()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(s: &str) -> Result<TimeFormatter, ParseError> {
|
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))
|
||||||
|
|
Loading…
Reference in New Issue