Skip to content

Commit

Permalink
Change default interval
Browse files Browse the repository at this point in the history
  • Loading branch information
treiher committed Oct 27, 2024
1 parent 330bbae commit e4088af
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Omit charts that contain no data
- Hide empty columns in training tables
- Consider sets without RPE value to be hard sets
- Default interval on exercise, body weight, body fat and menstrual cycle page to three months

### Fixed

Expand Down
33 changes: 22 additions & 11 deletions frontend/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ impl From<std::ops::RangeInclusive<NaiveDate>> for Interval {
}
}

#[derive(Clone, Copy, PartialEq)]
pub enum DefaultInterval {
All,
_1Y = 365,
_6M = 182,
_3M = 91,
_1M = 30,
}

#[derive(Clone)]
#[cfg_attr(test, derive(Debug, PartialEq))]
pub struct InputField<T> {
Expand Down Expand Up @@ -67,13 +76,15 @@ impl<T> InputField<T> {
}
}

pub fn init_interval(dates: &[NaiveDate], show_all: bool) -> Interval {
pub fn init_interval(dates: &[NaiveDate], default_interval: DefaultInterval) -> Interval {
let today = Local::now().date_naive();
let mut first = dates.iter().copied().min().unwrap_or(today);
let mut last = dates.iter().copied().max().unwrap_or(today);

if not(show_all) && last >= today - Duration::days(30) {
first = today - Duration::days(30);
if default_interval != DefaultInterval::All
&& last >= today - Duration::days(default_interval as i64)
{
first = today - Duration::days(default_interval as i64);
};

last = today;
Expand Down Expand Up @@ -268,27 +279,27 @@ where
),
(
"1Y",
today - Duration::days(365),
today - Duration::days(DefaultInterval::_1Y as i64),
today,
current.last == today && duration == Duration::days(366),
current.last == today && duration == Duration::days(DefaultInterval::_1Y as i64 + 1),
),
(
"6M",
today - Duration::days(182),
today - Duration::days(DefaultInterval::_6M as i64),
today,
current.last == today && duration == Duration::days(183),
current.last == today && duration == Duration::days(DefaultInterval::_6M as i64 + 1),
),
(
"3M",
today - Duration::days(91),
today - Duration::days(DefaultInterval::_3M as i64),
today,
current.last == today && duration == Duration::days(92),
current.last == today && duration == Duration::days(DefaultInterval::_3M as i64 + 1),
),
(
"1M",
today - Duration::days(30),
today - Duration::days(DefaultInterval::_1M as i64),
today,
current.last == today && duration == Duration::days(31),
current.last == today && duration == Duration::days(DefaultInterval::_1M as i64 + 1),
),
(
"+",
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/page/body_fat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn init(
.keys()
.copied()
.collect::<Vec<NaiveDate>>(),
false,
common::DefaultInterval::_3M,
),
dialog: Dialog::Hidden,
loading: false,
Expand Down Expand Up @@ -418,7 +418,7 @@ pub fn update(
.keys()
.copied()
.collect::<Vec<NaiveDate>>(),
false,
common::DefaultInterval::_3M,
);
}
data::Event::BodyFatCreatedOk
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/page/body_weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn init(
.keys()
.copied()
.collect::<Vec<NaiveDate>>(),
false,
common::DefaultInterval::_3M,
),
dialog: Dialog::Hidden,
loading: false,
Expand Down Expand Up @@ -195,7 +195,7 @@ pub fn update(
.keys()
.copied()
.collect::<Vec<NaiveDate>>(),
false,
common::DefaultInterval::_3M,
);
}
data::Event::BodyWeightCreatedOk
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/page/exercise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn init(
navbar.title = String::from("Exercise");

let mut model = Model {
interval: common::init_interval(&[], true),
interval: common::init_interval(&[], common::DefaultInterval::_3M),
exercise_id,
name: common::InputField::default(),
muscle_stimulus: BTreeMap::new(),
Expand Down Expand Up @@ -216,7 +216,7 @@ fn update_model(model: &mut Model, data_model: &data::Model) {
.filter(|t| t.exercises().contains(&model.exercise_id))
.map(|t| t.date)
.collect::<Vec<NaiveDate>>(),
false,
common::DefaultInterval::_3M,
);

let exercise = &data_model.exercises.get(&model.exercise_id);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/page/menstrual_cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn init(
.keys()
.copied()
.collect::<Vec<NaiveDate>>(),
false,
common::DefaultInterval::_3M,
),
dialog: Dialog::Hidden,
loading: false,
Expand Down Expand Up @@ -185,7 +185,7 @@ pub fn update(
.keys()
.copied()
.collect::<Vec<NaiveDate>>(),
false,
common::DefaultInterval::_3M,
);
}
data::Event::PeriodCreatedOk
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/page/muscles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn init(data_model: &data::Model, navbar: &mut crate::Navbar) -> Model {
.values()
.map(|t| t.date)
.collect::<Vec<NaiveDate>>(),
false,
common::DefaultInterval::_1M,
),
}
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/page/routine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn init(
navbar.title = String::from("Routine");

let mut model = Model {
interval: common::init_interval(&[], true),
interval: common::init_interval(&[], common::DefaultInterval::All),
routine_id,
name: common::InputField::default(),
sections: vec![],
Expand Down Expand Up @@ -679,7 +679,7 @@ fn update_model(model: &mut Model, data_model: &data::Model) {
.filter(|t| t.routine_id == Some(model.routine_id))
.map(|t| t.date)
.collect::<Vec<NaiveDate>>(),
true,
common::DefaultInterval::All,
);

let routine = &data_model.routines.get(&model.routine_id);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/page/training.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn init(
.values()
.map(|t| t.date)
.collect::<Vec<NaiveDate>>(),
false,
common::DefaultInterval::_1M,
),
dialog: Dialog::Hidden,
loading: false,
Expand Down Expand Up @@ -182,7 +182,7 @@ pub fn update(
.values()
.map(|t| t.date)
.collect::<Vec<NaiveDate>>(),
false,
common::DefaultInterval::_1M,
);
}
data::Event::TrainingSessionCreatedOk => {
Expand Down

0 comments on commit e4088af

Please sign in to comment.