Skip to content

Commit

Permalink
Fix preferring of exercises in training session
Browse files Browse the repository at this point in the history
  • Loading branch information
treiher committed Nov 30, 2024
1 parent 8e84a49 commit c37e0c8
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions frontend/src/ui/page/training_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1633,7 +1633,7 @@ fn replace_exercise(
}
}

fn prefer_exercise(elements: &mut [FormElement], element_idx: usize) {
fn prefer_exercise(elements: &mut Vec<FormElement>, element_idx: usize) {
let sections = determine_sections(elements);
let Some(preferred_section) = sections.iter().find(|s| (s.0..=s.1).contains(&element_idx))
else {
Expand All @@ -1648,8 +1648,18 @@ fn prefer_exercise(elements: &mut [FormElement], element_idx: usize) {
else {
return;
};
elements[deferred_section.0..=preferred_section.1]
.rotate_right(preferred_section.1 - preferred_section.0 + 1);
let mut trailing_rest = 0;
if preferred_section.1 + 1 == elements.len() {
if let Some(FormElement::Set { .. }) = elements.last() {
elements.push(FormElement::Rest {
target_time: 0,
automatic: true,
});
trailing_rest += 1;
}
}
elements[deferred_section.0..=preferred_section.1 + trailing_rest]
.rotate_right(preferred_section.1 + trailing_rest - preferred_section.0 + 1);
}

fn defer_exercise(elements: &mut Vec<FormElement>, element_idx: usize) {
Expand Down Expand Up @@ -3241,6 +3251,29 @@ mod tests {
);
}

#[test]
fn test_prefer_exercise_last_set_without_trailing_rest() {
let mut elements = vec![
set(vec![exercise(0, 0)]),
rest(0),
set(vec![exercise(1, 1)]),
rest(1),
set(vec![exercise(2, 2)]),
];
prefer_exercise(&mut elements, 4);
assert_eq!(
elements,
vec![
set(vec![exercise(0, 0)]),
rest(0),
set(vec![exercise(2, 2)]),
rest(0),
set(vec![exercise(1, 1)]),
rest(1),
]
);
}

#[test]
fn test_prefer_exercise_multiple_sets() {
let mut elements = vec![
Expand Down

0 comments on commit c37e0c8

Please sign in to comment.