Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ interface CardioExerciseProps {
onStartTimer: (setIndex: number) => void;
onEditExercise: (() => void) | undefined;
onRemoveExercise: () => void;
onMoveExerciseUp: (() => void) | undefined;
onMoveExerciseDown: (() => void) | undefined;
}

export function CardioExercise(props: CardioExerciseProps) {
Expand All @@ -52,6 +54,8 @@ export function CardioExercise(props: CardioExerciseProps) {
updateExercise={updateExercise}
onEditExercise={props.onEditExercise}
onRemoveExercise={props.onRemoveExercise}
onMoveExerciseUp={props.onMoveExerciseUp}
onMoveExerciseDown={props.onMoveExerciseDown}
>
<View style={{ gap: spacing[4] }}>
{recordedExercise.sets.map((set, setIndex) => (
Expand Down
22 changes: 22 additions & 0 deletions app/src/components/presentation/workout/exercise-section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ interface ExerciseSectionProps<T extends RecordedExercise> {
updateExercise: (update: Updater<T>) => void;
onEditExercise: (() => void) | undefined;
onRemoveExercise: () => void;
onMoveExerciseUp: (() => void) | undefined;
onMoveExerciseDown: (() => void) | undefined;
}

export default function ExerciseSection<T extends RecordedExercise>(props: ExerciseSectionProps<T>) {
Expand Down Expand Up @@ -75,6 +77,26 @@ export default function ExerciseSection<T extends RecordedExercise>(props: Exerc
} satisfies MenuItem,
]
: []),
...(props.onMoveExerciseUp
? [
{
label: t('exercise.move_up.button'),
icon: 'arrowUpward',
systemImage: 'arrow.up',
onPress: props.onMoveExerciseUp,
} satisfies MenuItem,
]
: []),
...(props.onMoveExerciseDown
? [
{
label: t('exercise.move_down.button'),
icon: 'arrowDownward',
systemImage: 'arrow.down',
onPress: props.onMoveExerciseDown,
} satisfies MenuItem,
]
: []),
{
label: t('generic.notes.label'),
icon: 'notes',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ interface WeightedExerciseProps {
resetSetTimer: () => void;
onEditExercise: (() => void) | undefined;
onRemoveExercise: () => void;
onMoveExerciseUp: (() => void) | undefined;
onMoveExerciseDown: (() => void) | undefined;
}

export default function WeightedExercise(props: WeightedExerciseProps) {
Expand All @@ -38,6 +40,8 @@ export default function WeightedExercise(props: WeightedExerciseProps) {
updateExercise={props.updateExercise}
onEditExercise={props.onEditExercise}
onRemoveExercise={props.onRemoveExercise}
onMoveExerciseUp={props.onMoveExerciseUp}
onMoveExerciseDown={props.onMoveExerciseDown}
>
<View style={{ flexDirection: 'row', gap: spacing[2], flexWrap: 'wrap' }}>
{recordedExercise.potentialSets.map((set, index) => (
Expand Down
16 changes: 16 additions & 0 deletions app/src/components/smart/session-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ export default function SessionComponent(props: {
editableSessionId ? () => push(getSessionExerciseEditorHref(editableSessionId, index)) : undefined
}
onRemoveExercise={() => updateSession((s) => s.withRemovedExercise(index))}
onMoveExerciseUp={
!isReadonly && index > 0 ? () => updateSession((s) => s.withExerciseMovedUp(index)) : undefined
}
onMoveExerciseDown={
!isReadonly && index < session.recordedExercises.length - 1
? () => updateSession((s) => s.withExerciseMovedDown(index))
: undefined
}
isReadonly={isReadonly}
showPreviousButton={!!isActiveWorkout}
previousRecordedExercises={recentlyCompletedExercises(item.movementKey()) as RecordedWeightedExercise[]}
Expand All @@ -169,6 +177,14 @@ export default function SessionComponent(props: {
editableSessionId ? () => push(getSessionExerciseEditorHref(editableSessionId, index)) : undefined
}
onRemoveExercise={() => updateSession((s) => s.withRemovedExercise(index))}
onMoveExerciseUp={
!isReadonly && index > 0 ? () => updateSession((s) => s.withExerciseMovedUp(index)) : undefined
}
onMoveExerciseDown={
!isReadonly && index < session.recordedExercises.length - 1
? () => updateSession((s) => s.withExerciseMovedDown(index))
: undefined
}
isReadonly={isReadonly}
showPreviousButton={!!isActiveWorkout}
previousRecordedExercises={recentlyCompletedExercises(item.movementKey()) as RecordedCardioExercise[]}
Expand Down
2 changes: 2 additions & 0 deletions app/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@
"exercise.manage.subtitle": "Manage your exercise list",
"exercise.manage.title": "Manage Exercises",
"exercise.name.label": "Exercise name",
"exercise.move_up.button": "Move up",
"exercise.move_down.button": "Move down",
"exercise.description.cardio_set.body": "Set <em>{setNumber}</em> - {targetType}: <em>{targetValue}</em>",
"exercise.never_done_before.message": "You have never done this exercise before",
"exercise.no_exercises_added.message": "No exercises added yet",
Expand Down
49 changes: 49 additions & 0 deletions app/src/models/session-models/session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,55 @@ describe('Session structural mutations', () => {
expect(result.recordedExercises[0]!.blueprint.name).toBe('Bench');
});

describe('withExerciseMovedUp/Down', () => {
it('moving up keeps recordedExercises and blueprint exercises in lockstep', () => {
const session = makeSession([
makeWeightedBlueprint({ name: 'Squat' }),
makeWeightedBlueprint({ name: 'Bench' }),
makeWeightedBlueprint({ name: 'Row' }),
]);

const result = session.withExerciseMovedUp(1);

expect(result.recordedExercises.map((x) => x.blueprint.name)).toEqual(['Bench', 'Squat', 'Row']);
expect(result.blueprint.exercises.map((x) => x.name)).toEqual(['Bench', 'Squat', 'Row']);
});

it('moving down keeps recordedExercises and blueprint exercises in lockstep', () => {
const session = makeSession([
makeWeightedBlueprint({ name: 'Squat' }),
makeWeightedBlueprint({ name: 'Bench' }),
makeWeightedBlueprint({ name: 'Row' }),
]);

const result = session.withExerciseMovedDown(1);

expect(result.recordedExercises.map((x) => x.blueprint.name)).toEqual(['Squat', 'Row', 'Bench']);
expect(result.blueprint.exercises.map((x) => x.name)).toEqual(['Squat', 'Row', 'Bench']);
});

it('preserves the recorded state of the moved exercise', () => {
const session = makeSession([makeWeightedBlueprint({ name: 'Squat' }), makeWeightedBlueprint({ name: 'Bench' })]);
const moved = session.withExerciseMovedUp(1).recordedExercises[0] as RecordedWeightedExercise;

expect(moved.potentialSets[0]!.set?.repsCompleted).toBe(
(session.recordedExercises[1] as RecordedWeightedExercise).potentialSets[0]!.set?.repsCompleted,
);
});

it('moving the first exercise up is a no-op', () => {
const session = makeSession([makeWeightedBlueprint({ name: 'Squat' }), makeWeightedBlueprint({ name: 'Bench' })]);

expect(session.withExerciseMovedUp(0)).toBe(session);
});

it('moving the last exercise down is a no-op', () => {
const session = makeSession([makeWeightedBlueprint({ name: 'Squat' }), makeWeightedBlueprint({ name: 'Bench' })]);

expect(session.withExerciseMovedDown(1)).toBe(session);
});
});

it('withName renames the blueprint', () => {
const session = makeSession([makeWeightedBlueprint()]);
expect(session.withName('Push Day').blueprint.name).toBe('Push Day');
Expand Down
30 changes: 30 additions & 0 deletions app/src/models/session-models/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,36 @@ export class Session {
});
}

withExerciseMovedUp(exerciseIndex: number): Session {
return this.withExerciseMoved(exerciseIndex, exerciseIndex - 1);
}

withExerciseMovedDown(exerciseIndex: number): Session {
return this.withExerciseMoved(exerciseIndex, exerciseIndex + 1);
}

withExerciseMoved(exerciseIndex: number, newIndex: number): Session {
if (
exerciseIndex < 0 ||
exerciseIndex >= this.recordedExercises.length ||
newIndex < 0 ||
newIndex >= this.recordedExercises.length ||
exerciseIndex === newIndex
) {
return this;
}
// Remove first, then re-insert at the target index: since removal shifts nothing before it,
// inserting at newIndex lands the item at its final position for moves in either direction.
const move = <T>(items: T[]): T[] =>
items.toSpliced(exerciseIndex, 1).toSpliced(newIndex, 0, items[exerciseIndex]!);
return this.with({
recordedExercises: move(this.recordedExercises),
blueprint: this.blueprint.with({
exercises: move(this.blueprint.exercises),
}),
});
}

toJSON(): SessionJSON {
return {
version: 7,
Expand Down
Loading