Skip to content

Commit

Permalink
Rebuild how variable settings are handled
Browse files Browse the repository at this point in the history
Variable settings, like language, timezone, and units, which are
basically needed by every component, are now bundled together more
reasonably, and accessor methods provide centralized string generation
and some limited parsing.
  • Loading branch information
savannidgerinel committed Feb 10, 2020
1 parent 5b31256 commit 4c6882d
Show file tree
Hide file tree
Showing 20 changed files with 353 additions and 219 deletions.
2 changes: 1 addition & 1 deletion src/components/basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::conversions::{
parse_distance, parse_duration, parse_hours_minutes, render_distance, render_duration,
render_hours_minutes,
};
use crate::preferences::UnitSystem;
use crate::i18n::UnitSystem;

pub fn date_c(date: &chrono::Date<chrono_tz::Tz>) -> gtk::Label {
let lbl = gtk::Label::new(Some(&format!("{}", date.format("%B %e, %Y"))));
Expand Down
58 changes: 27 additions & 31 deletions src/components/day.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use crate::components::time_distance_row::time_distance_c;
use crate::components::weight::{weight_record_c, weight_record_edit_c};
use crate::components::{Component, SwappableComponent};
use crate::context::AppContext;
use crate::i18n::Messages;
use crate::preferences::{Preferences, UnitSystem};
use crate::i18n::UnitSystem;
use crate::settings::Settings;

#[derive(Clone)]
enum DayState {
Expand All @@ -42,16 +42,14 @@ struct SwappableDay {

date: chrono::Date<chrono_tz::Tz>,
records: Vec<Record<TraxRecord>>,
messages: Messages,
prefs: Preferences,
settings: Settings,
}

impl SwappableDay {
fn new(
date: chrono::Date<chrono_tz::Tz>,
records: Vec<Record<TraxRecord>>,
messages: Messages,
prefs: Preferences,
settings: Settings,
) -> SwappableDay {
let default_view = DayState::View(gtk::Box::new(gtk::Orientation::Vertical, 5));

Expand All @@ -61,8 +59,7 @@ impl SwappableDay {

date,
records,
messages,
prefs,
settings,
};
s.view();
s
Expand All @@ -81,6 +78,7 @@ impl SwappableDay {
}
}

/*
pub fn set_messages(&mut self, messages: Messages) {
self.messages = messages;
match self.state {
Expand All @@ -96,13 +94,13 @@ impl SwappableDay {
DayState::Edit(ref e) => {}
}
}
*/

fn view(&mut self) {
let v = day_c(
&self.date,
self.records.iter().map(|rec| &rec.data).collect(),
&self.messages,
&self.prefs,
&self.settings,
);
self.state = DayState::View(v);
self.component.swap(Box::new(self.state.clone()));
Expand All @@ -120,8 +118,7 @@ impl SwappableDay {
self.state = DayState::Edit(DayEdit::new(
&self.date,
&record_map,
self.messages.clone(),
&self.prefs,
&self.settings,
on_save,
on_cancel,
));
Expand Down Expand Up @@ -154,20 +151,19 @@ impl Day {
ctx: Arc<RwLock<AppContext>>,
date: chrono::Date<chrono_tz::Tz>,
records: Vec<Record<TraxRecord>>,
messages: Messages,
prefs: Preferences,
settings: Settings,
) -> Day {
let widget = gtk::Box::new(gtk::Orientation::Vertical, 5);
let header = gtk::Box::new(gtk::Orientation::Horizontal, 5);
let edit_button = gtk::Button::new_with_label(&messages.edit());
let edit_button = gtk::Button::new_with_label(&settings.text.edit());
edit_button.show();

header.pack_start(&date_c(&date), false, false, 5);
header.pack_start(&edit_button, false, false, 5);
header.show();
widget.pack_start(&header, false, false, 5);

let swappable = SwappableDay::new(date, records, messages, prefs);
let swappable = SwappableDay::new(date, records, settings);

widget.pack_start(&swappable.render(), true, true, 5);

Expand All @@ -193,13 +189,15 @@ impl Day {
self.swappable.write().unwrap().set_records(date, records);
}

/*
fn set_messages(&mut self, messages: Messages) {
self.swappable.write().unwrap().set_messages(messages);
}
fn set_preferences(&mut self, prefs: Preferences) {
self.swappable.write().unwrap().set_preferences(prefs);
}
*/

fn view(&self) {
let mut c = self.swappable.write().unwrap();
Expand Down Expand Up @@ -234,8 +232,7 @@ impl Day {
fn day_c(
_date: &chrono::Date<chrono_tz::Tz>,
data: Vec<&TraxRecord>,
messages: &Messages,
prefs: &Preferences,
settings: &Settings,
) -> gtk::Box {
let container = gtk::Box::new(gtk::Orientation::Vertical, 5);

Expand All @@ -253,15 +250,15 @@ fn day_c(
match record {
TraxRecord::Comments(ref _rec) => (),
TraxRecord::RepDuration(ref rec) => {
rep_duration_components.push(rep_duration_c(&rec, &messages))
rep_duration_components.push(rep_duration_c(&rec, &settings))
}
TraxRecord::SetRep(ref rec) => set_rep_components.push(set_rep_c(&rec, &messages)),
TraxRecord::Steps(ref rec) => step_component = Some(steps_c(&rec, &messages)),
TraxRecord::SetRep(ref rec) => set_rep_components.push(set_rep_c(&rec, &settings)),
TraxRecord::Steps(ref rec) => step_component = Some(steps_c(&rec, &settings)),
TraxRecord::TimeDistance(ref rec) => {
time_distance_components.push(time_distance_c(&rec, &messages, &prefs))
time_distance_components.push(time_distance_c(&rec, &settings))
}
TraxRecord::Weight(ref rec) => {
weight_component = Some(weight_record_c(&rec, &messages, &prefs.units))
weight_component = Some(weight_record_c(&rec, &settings))
}
}
}
Expand Down Expand Up @@ -297,8 +294,7 @@ impl DayEdit {
fn new(
date: &chrono::Date<chrono_tz::Tz>,
data: &HashMap<UniqueId, TraxRecord>,
messages: Messages,
prefs: &Preferences,
settings: &Settings,
on_save: Box<dyn Fn(Vec<(UniqueId, TraxRecord)>, Vec<TraxRecord>)>,
on_cancel: Box<dyn Fn()>,
) -> DayEdit {
Expand All @@ -316,7 +312,7 @@ impl DayEdit {
weight_record_edit_c(
UniqueId::new(),
WeightRecord::new(DateTimeTz(date_.and_hms(0, 0, 0)), 0.0 * KG),
&prefs.units,
&settings.units,
Box::new(move |id, rec| {
new_records_
.write()
Expand Down Expand Up @@ -350,7 +346,7 @@ impl DayEdit {
weight_component = weight_record_edit_c(
id.clone(),
rec.clone(),
&prefs.units,
&settings.units,
Box::new(move |id, rec| {
updates_.write().unwrap().insert(id, TraxRecord::from(rec));
}),
Expand All @@ -377,9 +373,9 @@ impl DayEdit {
}

let time_distance_edit =
{ TimeDistanceEdit::new(date.clone(), time_distance_records, &prefs) };
{ TimeDistanceEdit::new(date.clone(), time_distance_records, settings.clone()) };

let weight_label = match prefs.units {
let weight_label = match settings.units {
UnitSystem::SI => "kg",
UnitSystem::USA => "lbs",
};
Expand All @@ -391,8 +387,8 @@ impl DayEdit {
widget.pack_start(&time_distance_edit.widget, false, false, 5);

let buttons_row = gtk::Box::new(gtk::Orientation::Horizontal, 5);
let save_button = gtk::Button::new_with_label(&messages.save());
let cancel_button = gtk::Button::new_with_label(&messages.cancel());
let save_button = gtk::Button::new_with_label(&settings.text.save());
let cancel_button = gtk::Button::new_with_label(&settings.text.cancel());
buttons_row.pack_start(&save_button, false, false, 5);
buttons_row.pack_start(&cancel_button, false, false, 5);
widget.pack_start(&buttons_row, false, false, 5);
Expand Down
11 changes: 4 additions & 7 deletions src/components/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use std::sync::{Arc, RwLock};

use crate::components::{Component, Day, RangeSelector};
use crate::context::AppContext;
use crate::i18n::Messages;
use crate::preferences::Preferences;
use crate::range::group_by_date;
use crate::settings::Settings;
use crate::types::DateRange;

struct HistoryComponent {
Expand All @@ -32,8 +31,7 @@ impl History {

pub fn render(
&mut self,
messages: Messages,
prefs: Preferences,
settings: Settings,
range: DateRange,
records: Vec<Record<TraxRecord>>,
) -> &gtk::Box {
Expand Down Expand Up @@ -66,7 +64,7 @@ impl History {
history_box,
});

self.render(messages, prefs, range, records)
self.render(settings, range, records)
}
Some(HistoryComponent {
ref widget,
Expand All @@ -84,8 +82,7 @@ impl History {
ctx,
*date.clone(),
grouped_history.get(date).unwrap().clone(),
messages.clone(),
prefs.clone(),
settings.clone(),
);
history_box.pack_start(&day.render(), true, true, 25);
});
Expand Down
59 changes: 24 additions & 35 deletions src/components/main_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ use gtk::prelude::*;
use std::sync::{Arc, RwLock};

use crate::components::*;
use crate::i18n::Messages;
use crate::preferences;
use crate::settings::Settings;
use crate::types::DateRange;

struct MainWindowComponent {
notebook: gtk::Notebook,
history_label: gtk::Label,
preferences_label: gtk::Label,
settings_label: gtk::Label,
history: History,
preferences: Preferences,
settings_ui: Preferences,
}

pub struct MainWindow {
Expand All @@ -38,32 +37,25 @@ impl MainWindow {

pub fn render(
&mut self,
messages: Messages,
prefs: preferences::Preferences,
settings: Settings,
range: DateRange,
records: Vec<Record<TraxRecord>>,
) -> &gtk::ApplicationWindow {
match self.component {
None => {
let notebook = gtk::Notebook::new();
let mut history = History::new(self.ctx.clone());
let mut preferences = Preferences::new(self.ctx.clone());
let mut settings_ui = Preferences::new(self.ctx.clone());

let ctx = self.ctx.read().unwrap();
let messages = ctx.get_messages();
let history_label = gtk::Label::new(Some(&messages.history()));
let preferences_label = gtk::Label::new(Some(&messages.preferences()));
let history_label = gtk::Label::new(Some(&settings.text.history()));
let settings_label = gtk::Label::new(Some(&settings.text.preferences()));

notebook.append_page(
history.render(
messages,
ctx.get_preferences(),
ctx.get_range(),
ctx.get_history().unwrap(),
),
history.render(settings, range, records),
Some(&history_label),
);
notebook.append_page(preferences.render(), Some(&preferences_label));
notebook.append_page(settings_ui.render(), Some(&settings_label));

notebook.show();
self.widget.add(&notebook);
Expand All @@ -72,23 +64,23 @@ impl MainWindow {
let component = MainWindowComponent {
notebook,
history_label,
preferences_label,
settings_label,
history,
preferences,
settings_ui,
};
self.component = Some(component);
}
Some(MainWindowComponent {
ref history_label,
ref preferences_label,
ref settings_label,
ref mut history,
ref mut preferences,
ref mut settings_ui,
..
}) => {
history_label.set_markup(&messages.history());
preferences_label.set_markup(&messages.preferences());
history.render(messages, prefs, range, records);
preferences.render();
history_label.set_markup(&settings.text.history());
settings_label.set_markup(&settings.text.preferences());
history.render(settings, range, records);
settings_ui.render();
}
}
&self.widget
Expand All @@ -97,28 +89,25 @@ impl MainWindow {
pub fn update_from(&mut self, message: Message) {
match message {
Message::ChangeRange {
prefs,
messages,
settings,
range,
records,
} => {
self.render(messages, prefs, range, records);
self.render(settings, range, records);
}
Message::ChangePreferences {
prefs,
messages,
Message::ChangeSettings {
settings,
range,
records,
} => {
self.render(messages, prefs, range, records);
self.render(settings, range, records);
}
Message::RecordsUpdated {
prefs,
messages,
settings,
range,
records,
} => {
self.render(messages, prefs, range, records);
self.render(settings, range, records);
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod date_selector;
mod day;
mod history;
mod main_window;
mod menu_bar;
mod preferences;
mod range_selector;
mod rep_duration;
Expand All @@ -21,7 +20,6 @@ pub use date_selector::date_selector_c;
pub use day::Day;
pub use history::History;
pub use main_window::MainWindow;
pub use menu_bar::MenuBar;
pub use preferences::Preferences;
pub use range_selector::RangeSelector;
pub use setting::setting_c;
Expand Down
Loading

0 comments on commit 4c6882d

Please sign in to comment.