Skip to content

Commit

Permalink
Apply settings system to steps and weight widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
savannidgerinel committed Feb 10, 2020
1 parent 4c6882d commit dae2cb8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 48 deletions.
13 changes: 4 additions & 9 deletions src/components/day.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl DayEdit {
weight_record_edit_c(
UniqueId::new(),
WeightRecord::new(DateTimeTz(date_.and_hms(0, 0, 0)), 0.0 * KG),
&settings.units,
&settings,
Box::new(move |id, rec| {
new_records_
.write()
Expand All @@ -328,6 +328,7 @@ impl DayEdit {
steps_edit_c(
UniqueId::new(),
StepRecord::new(DateTimeTz(date_.and_hms(0, 0, 0)), 0),
&settings,
Box::new(move |id, rec| {
new_records_
.write()
Expand All @@ -346,7 +347,7 @@ impl DayEdit {
weight_component = weight_record_edit_c(
id.clone(),
rec.clone(),
&settings.units,
&settings,
Box::new(move |id, rec| {
updates_.write().unwrap().insert(id, TraxRecord::from(rec));
}),
Expand All @@ -357,6 +358,7 @@ impl DayEdit {
step_component = steps_edit_c(
id.clone(),
rec.clone(),
&settings,
Box::new(move |id_, rec| {
updates_
.write()
Expand All @@ -375,15 +377,8 @@ impl DayEdit {
let time_distance_edit =
{ TimeDistanceEdit::new(date.clone(), time_distance_records, settings.clone()) };

let weight_label = match settings.units {
UnitSystem::SI => "kg",
UnitSystem::USA => "lbs",
};

first_row.pack_start(&weight_component, false, false, 5);
first_row.pack_start(&gtk::Label::new(Some(weight_label)), false, false, 5);
first_row.pack_start(&step_component, false, false, 5);
first_row.pack_start(&gtk::Label::new(Some("steps")), false, false, 5);
widget.pack_start(&time_distance_edit.widget, false, false, 5);

let buttons_row = gtk::Box::new(gtk::Orientation::Horizontal, 5);
Expand Down
15 changes: 12 additions & 3 deletions src/components/steps.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use emseries::{Recordable, UniqueId};
use fitnesstrax::steps::StepRecord;
use gtk::prelude::*;

use crate::components::validated_text_entry_c;
use crate::errors::Error;
Expand All @@ -12,12 +13,20 @@ pub fn steps_c(record: &fitnesstrax::steps::StepRecord, settings: &Settings) ->
pub fn steps_edit_c(
id: UniqueId,
record: StepRecord,
settings: &Settings,
on_update: Box<dyn Fn(UniqueId, StepRecord)>,
) -> gtk::Entry {
validated_text_entry_c(
) -> gtk::Box {
let b = gtk::Box::new(gtk::Orientation::Horizontal, 5);

let entry = validated_text_entry_c(
record.steps,
Box::new(|s| format!("{}", s)),
Box::new(|s| s.parse::<u32>().map_err(|_err| Error::ParseStepsError)),
Box::new(move |val| on_update(id.clone(), StepRecord::new(record.timestamp(), val))),
)
);
let label = gtk::Label::new(Some(&settings.text.steps_label()));

b.pack_start(&entry, false, false, 5);
b.pack_start(&label, false, false, 5);
b
}
26 changes: 16 additions & 10 deletions src/components/weight.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use emseries::{Recordable, UniqueId};
use fitnesstrax::weight::WeightRecord;
use gtk::prelude::*;

use crate::components::validated_text_entry_c;
use crate::conversions::{parse_mass, render_mass};
use crate::errors::Error;
use crate::i18n::UnitSystem;
use crate::settings::Settings;

pub fn weight_record_c(record: &WeightRecord, settings: &Settings) -> gtk::Label {
Expand All @@ -14,19 +13,26 @@ pub fn weight_record_c(record: &WeightRecord, settings: &Settings) -> gtk::Label
pub fn weight_record_edit_c(
id: UniqueId,
record: WeightRecord,
unit: &UnitSystem,
settings: &Settings,
on_update: Box<dyn Fn(UniqueId, WeightRecord)>,
) -> gtk::Entry {
let u1 = unit.clone();
let u2 = unit.clone();
validated_text_entry_c(
) -> gtk::Box {
let b = gtk::Box::new(gtk::Orientation::Horizontal, 5);
let u1 = settings.units.clone();
let u2 = settings.units.clone();
let entry = validated_text_entry_c(
record.weight,
Box::new(move |w| render_mass(&w, &u1, false)),
Box::new(move |s| match parse_mass(s, &u2) {
Box::new(move |w| format!("{:.1}", u1.mass(&w))),
Box::new(move |s| match u2.parse_mass(s) {
Ok(Some(v)) => Ok(v),
Ok(None) => Err(Error::ParseMassError),
Err(_) => Err(Error::ParseMassError),
}),
Box::new(move |val| on_update(id.clone(), WeightRecord::new(record.timestamp(), val))),
)
);

let units_label = gtk::Label::new(Some(&settings.text.mass_label()));

b.pack_start(&entry, false, false, 5);
b.pack_start(&units_label, false, false, 5);
b
}
22 changes: 0 additions & 22 deletions src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,6 @@ use dimensioned::si;
use crate::errors::Error;
use crate::i18n::UnitSystem;

pub fn render_mass(inp: &si::Kilogram<f64>, units: &UnitSystem, display_units: bool) -> String {
let value_str = match units {
UnitSystem::SI => inp.value_unsafe,
UnitSystem::USA => (*inp / si::LB).value_unsafe,
};
let unit_str = match (display_units, units) {
(false, _) => "",
(true, UnitSystem::SI) => " kg",
(true, UnitSystem::USA) => " lbs",
};
format!("{:.2}{}", value_str, unit_str)
}

pub fn parse_mass(inp: &str, units: &UnitSystem) -> Result<Option<si::Kilogram<f64>>, Error> {
inp.parse::<f64>()
.map(|v| match units {
UnitSystem::SI => Some(v * si::KG),
UnitSystem::USA => Some(v * si::LB),
})
.map_err(|_| Error::ParseMassError)
}

pub fn render_hours_minutes(inp: &NaiveTime) -> String {
format!("{}", inp.format("%H:%M"))
}
Expand Down
24 changes: 20 additions & 4 deletions src/i18n/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ mass = {$units ->
*[SI] {$value} kilograms
[USA] {$value} pounds
}
mass-label = {$units ->
*[SI] kilograms
[USA] pounds
}
preferences = Preferences
pushups = Pushups
running = Running
save = Save
situps = Situps
steps = Steps
step-count = {$count ->
[one] 1 Step
*[other] {$count} Steps
[one] 1 step
*[other] {$count} steps
}
swimming = Swimming
timezone = Timezone
Expand All @@ -53,15 +57,19 @@ mass = {$units ->
*[SI] {$value} kilogramoj
[USA] {$value} funtoj
}
mass-label = {$units ->
*[SI] kilogramoj
[USA] funtoj
}
preferences = Agdoroj
pushups = Supraj Puŝoj
running = Kurado
save = Ŝpari
situps = Sidiĝoj
steps = Paŝoj
step-count = {$count ->
[one] 1 Paŝo
*[other] {$count} Paŝoj
[one] 1 paŝo
*[other] {$count} paŝoj
}
swimming = Naĝado
timezone = Horzono
Expand Down Expand Up @@ -151,6 +159,10 @@ impl Text {
self.tr("mass", Some(&args)).unwrap()
}

pub fn mass_label(&self) -> String {
self.tr("mass-label", None).unwrap()
}

pub fn preferences(&self) -> String {
self.tr("preferences", None).unwrap()
}
Expand Down Expand Up @@ -185,6 +197,10 @@ impl Text {
.unwrap()
}

pub fn steps_label(&self) -> String {
self.tr("steps", None).unwrap()
}

pub fn swimming(&self) -> String {
self.tr("swimming", None).unwrap()
}
Expand Down

0 comments on commit dae2cb8

Please sign in to comment.