Skip to content

armgit / lyftr

Public Imported from GitHub
0

Self-hosted fitness tracker. Log workouts, track nutrition, and monitor progress. Docker deployment, mobile-friendly.

fix(gym): keep set completion available during rest (#225)

Thijs226738121b45c53
diff --git a/mobile/src/components/workouts/GymModeWorkout.test.tsx b/mobile/src/components/workouts/GymModeWorkout.test.tsx
new file mode 100644
index 0000000..ca8e235
--- /dev/null
+++ b/mobile/src/components/workouts/GymModeWorkout.test.tsx
@@ -0,0 +1,65 @@
+import { act, fireEvent, render, screen } from '@testing-library/react-native'
+import { i18n, type ActiveSession } from '@lyftr/shared'
+import { useSettingsStore, useWorkoutSession } from '../../lib/lyftr'
+import { GymModeWorkout } from './GymModeWorkout'
+
+jest.mock('expo-router', () => ({
+ router: { push: jest.fn(), navigate: jest.fn(), replace: jest.fn() },
+ usePathname: () => '/workouts/active',
+}))
+jest.mock('@react-native-async-storage/async-storage', () =>
+ jest.requireActual('@react-native-async-storage/async-storage/jest/async-storage-mock')
+)
+jest.mock('react-native-safe-area-context', () => ({
+ useSafeAreaInsets: () => ({ top: 0, bottom: 0, left: 0, right: 0 }),
+ SafeAreaView: ({ children }: { children: unknown }) => children,
+}))
+
+const session = (): ActiveSession => ({
+ name: 'Workout', started_at: new Date().toISOString(), tz_offset_minutes: 0,
+ exercises: [{
+ exercise_id: 1,
+ exercise: { id: 1, name: 'Bench Press', muscle_group: 'chest', secondary_muscles: [], category: 'strength', equipment: 'barbell', description: '' },
+ notes: '', rest_seconds: 90,
+ sets: [1, 2, 3].map((set_number) => ({
+ set_number, target_reps: 8, target_weight: 100, actual_reps: 8, actual_weight: 100,
+ completed: set_number === 1,
+ })),
+ }],
+})
+
+beforeEach(async () => {
+ jest.useFakeTimers()
+ await i18n.changeLanguage('en')
+ useSettingsStore.setState(({ settings }) => ({ settings: { ...settings, rest_enabled: true } }))
+ useWorkoutSession.setState({ session: session(), gymPhase: 'exercise', gymExIdx: 0, gymSetIdx: 1 })
+ useWorkoutSession.getState().startRest(90, 0, 0)
+})
+
+afterEach(() => {
+ useWorkoutSession.getState().cancelSession()
+ jest.useRealTimers()
+})
+
+it.each([false, true])('can complete the next set during rest (paused: %s)', (paused) => {
+ if (paused) useWorkoutSession.getState().pauseRest()
+ const view = render(<GymModeWorkout />)
+ act(() => { jest.advanceTimersByTime(15_000) })
+
+ fireEvent.press(screen.getByText('Complete Set'))
+
+ const state = useWorkoutSession.getState()
+ expect(state.session!.exercises[0].sets.map((set) => set.completed)).toEqual([true, true, false])
+ expect(state.gymSetIdx).toBe(2)
+ expect(state.restSetIdx).toBe(1)
+ expect(state.restEndsAt).toBe(Date.now() + 90_000)
+ expect(state.restPausedRemainingMs).toBeNull()
+ expect(screen.getByText('Complete Set')).toBeTruthy()
+
+ // Undoing the set that owns the new timer still clears that timer.
+ act(() => { state.setGymState('exercise', 0, 1) })
+ fireEvent.press(screen.getByText('Completed'))
+ expect(useWorkoutSession.getState().restEndsAt).toBeNull()
+ expect(useWorkoutSession.getState().session!.exercises[0].sets[1].completed).toBe(false)
+ view.unmount()
+})
diff --git a/mobile/src/components/workouts/GymModeWorkout.tsx b/mobile/src/components/workouts/GymModeWorkout.tsx
index 64a90af..0ae81fc 100644
--- a/mobile/src/components/workouts/GymModeWorkout.tsx
+++ b/mobile/src/components/workouts/GymModeWorkout.tsx
@@ -460,8 +460,6 @@ export function GymModeWorkout() {
if (!set) return null
const restingHere = restExIdx === activeIdx
- const restNextSet = restingHere && restSetIdx != null ? nextIncompleteSet(ex.sets, restSetIdx) : -1
- const hideCompleteForRest = restingHere && clampedSetIdx === restNextSet
return overlay(
<>
@@ -536,20 +534,17 @@ export function GymModeWorkout() {
</View>
</View>
- {/* complete + remove (hidden while resting before this set) */}
- {!hideCompleteForRest ? (
- <View className="w-full items-center gap-6">
- <Pressable onPress={() => handleCompleteSetGym(clampedSetIdx)} className={`w-full flex-row items-center justify-center gap-3 rounded-2xl py-5 active:scale-95 ${set.completed ? 'border-2 border-brand-500/40 bg-brand-500/15' : 'bg-brand-500'}`}>
- <CheckCircle2 size={24} color={set.completed ? accent : '#ffffff'} />
- <Text className={`font-sans-bold text-base ${set.completed ? '' : 'text-white'}`} style={set.completed ? { color: accent } : undefined}>{set.completed ? t('gym.completed') : t('gym.completeSet')}</Text>
+ <View className="w-full items-center gap-6">
+ <Pressable onPress={() => handleCompleteSetGym(clampedSetIdx)} className={`w-full flex-row items-center justify-center gap-3 rounded-2xl py-5 active:scale-95 ${set.completed ? 'border-2 border-brand-500/40 bg-brand-500/15' : 'bg-brand-500'}`}>
+ <CheckCircle2 size={24} color={set.completed ? accent : '#ffffff'} />
+ <Text className={`font-sans-bold text-base ${set.completed ? '' : 'text-white'}`} style={set.completed ? { color: accent } : undefined}>{set.completed ? t('gym.completed') : t('gym.completeSet')}</Text>
+ </Pressable>
+ {ex.sets.length > 1 ? (
+ <Pressable onPress={() => handleRemoveSet(clampedSetIdx)} className="active:opacity-60">
+ <Text className="font-sans text-xs text-tx-muted">{t('gym.removeSet')}</Text>
</Pressable>
- {ex.sets.length > 1 ? (
- <Pressable onPress={() => handleRemoveSet(clampedSetIdx)} className="active:opacity-60">
- <Text className="font-sans text-xs text-tx-muted">{t('gym.removeSet')}</Text>
- </Pressable>
- ) : null}
- </View>
- ) : null}
+ ) : null}
+ </View>
</View>
</ScrollView>
diff --git a/web/e2e/workouts.spec.ts b/web/e2e/workouts.spec.ts
index 339349e..1bfcae8 100644
--- a/web/e2e/workouts.spec.ts
+++ b/web/e2e/workouts.spec.ts
@@ -446,6 +446,40 @@ test.describe('Gym Mode', { tag: '@mobile' }, () => {
await expect(page.getByRole('button', { name: /completed/i })).toBeVisible({ timeout: 2000 })
})
+ test('gym mode can complete sets while the rest timer is running or paused', async ({ page }) => {
+ await seedGymSession(page, 'E2E Complete During Rest', 'Bench Press', [1, 2, 3].map(set_number => ({
+ set_number, target_reps: 5, target_weight: 100, actual_reps: 5, actual_weight: 100, completed: false,
+ })))
+ await page.addInitScript(() => {
+ localStorage.setItem('lyftr_rest_enabled', 'true')
+ localStorage.setItem('lyftr_rest_seconds', '90')
+ })
+ await page.goto('/workout/active')
+ await page.getByRole('button', { name: /start workout/i }).click()
+ await page.getByRole('button', { name: /begin exercise/i }).click()
+
+ const complete = page.getByRole('button', { name: /^complete set$/i })
+ await complete.click()
+ await expect(page.getByRole('button', { name: 'Pause rest timer' })).toBeVisible()
+ await page.getByRole('button', { name: 'Shorten rest by 15 seconds' }).click()
+ await page.getByRole('button', { name: 'Shorten rest by 15 seconds' }).click()
+
+ // The next set remains loggable without first skipping the previous rest.
+ await complete.click()
+ await expect(page.getByText('Set 2 done · set 3 next')).toBeVisible()
+ await expect(page.getByText(/^1:(?:2[0-9]|30)$/)).toBeVisible()
+ await page.getByRole('button', { name: 'Pause rest timer' }).click()
+ await expect(page.getByRole('button', { name: 'Resume rest timer' })).toBeVisible()
+
+ await complete.click()
+ await expect(page.getByRole('button', { name: /^completed$/i })).toBeVisible()
+ await expect(page.getByRole('button', { name: 'Pause rest timer' })).toBeVisible()
+ await expect.poll(() => page.evaluate(key => {
+ const session = JSON.parse(localStorage.getItem(key)!)
+ return session.exercises[0].sets.filter((set: { completed: boolean }) => set.completed).length
+ }, SESSION_KEY)).toBe(3)
+ })
+
// #193: the rest panel used to float over Begin Exercise / Start Workout. It is docked
// in-flow below each phase's CTA now, so it must sit under the button, not over it.
test('gym mode rest panel sits below Begin Exercise and Start Workout', async ({ page }) => {
diff --git a/web/src/pages/GymModeWorkout.tsx b/web/src/pages/GymModeWorkout.tsx
index 7cf4707..cfee24f 100644
--- a/web/src/pages/GymModeWorkout.tsx
+++ b/web/src/pages/GymModeWorkout.tsx
@@ -518,10 +518,6 @@ export default function GymModeWorkout({ wUnit }: GymModeWorkoutProps) {
// Rest-timer view state for this exercise (restExIdx/restSetIdx are cleared or
// remapped by the store on structural edits, so they're safe to key on here).
const restingHere = restExIdx === activeIdx
- // The set the rest is "before" — the one auto-advance focuses. We only collapse the
- // Complete Set control for THAT set, so navigating to a different set stays loggable.
- const restNextSet = restingHere && restSetIdx != null ? nextIncompleteSet(ex.sets, restSetIdx) : -1
- const hideCompleteForRest = restingHere && clampedSetIdx === restNextSet
return (
<div role="dialog" aria-modal="true" aria-label={t('gym.workout')} className="fixed inset-0 z-[60] bg-surface-base flex flex-col">
@@ -640,14 +636,7 @@ export default function GymModeWorkout({ wUnit }: GymModeWorkoutProps) {
</StepperTile>
</div>
- {/* While resting before this set, collapse + fade the Complete Set / Remove
- controls out of the way — you Skip the timer to begin it. This also frees
- the space so the set content never clips behind the docked rest panel.
- Keyed on the specific up-next set (not the whole exercise) so navigating to
- another set to log it still shows its Complete button. */}
- <div className={`w-full flex flex-col items-center gap-6 overflow-hidden transition-all duration-300 ${
- hideCompleteForRest ? 'max-h-0 opacity-0 pointer-events-none' : 'max-h-40 opacity-100'
- }`}>
+ <div className="w-full flex flex-col items-center gap-6">
{/* Complete button */}
<button
onClick={() => handleCompleteSetGym(clampedSetIdx)}