Workout: a rest countdown belongs to the exercise that started it; removing another exercise keeps it running
This commit is contained in:
1 parent
0c9e473b68
commit
0e74ad303c
4 files changed
+57
-17
No files matched your search
@@ -56,7 +56,8 @@ let workDone = null
|
||||
export const useUI = create((set, get) => ({
|
||||
sheets: [], // { id, render:(close)=>JSX, kind:'sheet'|'center', locked }
|
||||
toastMsg: '',
|
||||
timer: null, // rest countdown between sets — { left, total, endsAt }
|
||||
timer: null, // rest countdown between sets — { left, total, endsAt, forIdx }
|
||||
// forIdx: index of the active entry whose set started the rest (undefined when unknown)
|
||||
work: null, // work countdown DURING a timed set (issue #16) — { left, total, endsAt, label }
|
||||
|
||||
openSheet(render, { kind = 'sheet', locked = false } = {}) {
|
||||
@@ -74,13 +75,13 @@ export const useUI = create((set, get) => ({
|
||||
toastTm = setTimeout(() => set({ toastMsg: '' }), 2200)
|
||||
},
|
||||
|
||||
startRest(sec) {
|
||||
startRest(sec, forIdx) {
|
||||
get().stopRest()
|
||||
// Rest timer set to Off. Stopping and returning rather than starting a zero-length timer
|
||||
// keeps every caller honest: the four places that start a rest do not each need to know.
|
||||
if (!(sec > 0)) return
|
||||
const endsAt = Date.now() + sec * 1000
|
||||
set({ timer: { left: sec, total: sec, endsAt } })
|
||||
set({ timer: { left: sec, total: sec, endsAt, forIdx } })
|
||||
requestRestNotificationPermission()
|
||||
pushRestTimer(sec)
|
||||
timerTick = () => {
|
||||
@@ -109,6 +110,13 @@ export const useUI = create((set, get) => ({
|
||||
set({ timer: { ...tm, left, total: tm.total + sec, endsAt: tm.endsAt + sec * 1000 } })
|
||||
pushRestTimer(left)
|
||||
},
|
||||
// The active list changed shape (an exercise removed or inserted at `at`): keep the rest
|
||||
// pointing at the same exercise. Returns nothing; the caller decides whether to stop instead.
|
||||
shiftRestOwner(at, delta) {
|
||||
const tm = get().timer
|
||||
if (!tm || !(tm.forIdx >= at)) return
|
||||
set({ timer: { ...tm, forIdx: tm.forIdx + delta } })
|
||||
},
|
||||
stopRest() {
|
||||
if (timerInt) clearInterval(timerInt); timerInt = null
|
||||
if (timerTick) document.removeEventListener('visibilitychange', timerTick); timerTick = null
|
||||
|
||||
@@ -258,10 +258,16 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemov
|
||||
|
||||
/* ---------- active workout ---------- */
|
||||
export function removeActiveExercise(idx) {
|
||||
// Clear timer state and callbacks before indexes can shift. This also protects a confirmation
|
||||
// sheet that was opened first and confirmed after a timed hold or rest countdown started.
|
||||
// Clear the work callback before indexes can shift. This also protects a confirmation sheet
|
||||
// that was opened first and confirmed after a timed hold started.
|
||||
useUI.getState().stopWork()
|
||||
useUI.getState().stopRest()
|
||||
// A rest countdown belongs to the exercise whose set started it (timer.forIdx). Removing that
|
||||
// exercise ends the rest — there is nothing left to rest for. Removing any other exercise
|
||||
// keeps the countdown and only re-points it, so a pause you are in the middle of survives
|
||||
// tidying up the list.
|
||||
const rest = useUI.getState().timer
|
||||
if (rest && rest.forIdx === idx) useUI.getState().stopRest()
|
||||
else useUI.getState().shiftRestOwner(idx + 1, -1)
|
||||
useStore.getState().update(s => {
|
||||
if (!s.active || !Array.isArray(s.active.entries)) return
|
||||
if (idx < 0 || idx >= s.active.entries.length) return
|
||||
@@ -424,7 +430,7 @@ function ActiveWorkout() {
|
||||
// A re-check of finished work must not navigate or reopen a sheet, but it may still owe
|
||||
// you a rest — see restOnRecheck, and the other half of issue #3.
|
||||
if (!progress.isNew) {
|
||||
if (!restBeforeWarmup && restOnRecheck({ timerRunning: !!useUI.getState().timer, unitDone: freshUnitDone, lastUnit: freshWorkoutDone })) startRest(S.restSec)
|
||||
if (!restBeforeWarmup && restOnRecheck({ timerRunning: !!useUI.getState().timer, unitDone: freshUnitDone, lastUnit: freshWorkoutDone })) startRest(S.restSec, idx)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -434,7 +440,7 @@ function ActiveWorkout() {
|
||||
if (freshUnitDone) stopRest()
|
||||
if (!freshUnit || freshUnit.length <= 1) {
|
||||
if (freshUnitDone && !askTop && nextUnit?.length) update(s => { if (s.active) s.active.cur = nextUnit[0] })
|
||||
if (!restBeforeWarmup && restAfterSet({ unitDone: freshUnitDone, lastUnit: freshWorkoutDone })) startRest(S.restSec)
|
||||
if (!restBeforeWarmup && restAfterSet({ unitDone: freshUnitDone, lastUnit: freshWorkoutDone })) startRest(S.restSec, idx)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -444,11 +450,11 @@ function ActiveWorkout() {
|
||||
if (nextUnit?.length) {
|
||||
// The top-weight sheet's explicit "Just close" path owns the choice not to advance.
|
||||
if (!askTop) update(s => { if (s.active) s.active.cur = nextUnit[0] })
|
||||
if (!restBeforeWarmup) startRest(S.restSec)
|
||||
if (!restBeforeWarmup) startRest(S.restSec, idx)
|
||||
}
|
||||
} else {
|
||||
if (step.nextIdx != null) update(s => { if (s.active) s.active.cur = step.nextIdx })
|
||||
if (step.roundDone) startRest(S.restSec)
|
||||
if (step.roundDone) startRest(S.restSec, idx)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -527,6 +533,7 @@ function ActiveWorkout() {
|
||||
const insertAt = insertionIndexAfterCurrentUnit(supersetUnits(s.active.entries), s.active.cur, s.active.entries.length)
|
||||
s.active.entries.splice(insertAt, 0, { id: ex.id, target: { ...cfg }, plan, sets: applyIntensifierPlan(progressed, full) })
|
||||
s.active.cur = insertAt
|
||||
useUI.getState().shiftRestOwner(insertAt, 1)
|
||||
}), null, routine, seed)
|
||||
})} icon="plus">{t('Add exercise')}</Button>
|
||||
{A.entries.length > 0 && <>
|
||||
|
||||
@@ -112,9 +112,9 @@ describe('active-session exercise removal', () => {
|
||||
expect(active.entries[0].sets[0].sec).toBeUndefined()
|
||||
})
|
||||
|
||||
it('cancels a running rest countdown before removing the selected occurrence', () => {
|
||||
it('cancels a running rest countdown when the exercise it belongs to is removed', () => {
|
||||
setActive([entry('1001'), entry('1002')], 0)
|
||||
useUI.getState().startRest(90)
|
||||
useUI.getState().startRest(90, 0)
|
||||
expect(useUI.getState().timer).not.toBeNull()
|
||||
|
||||
act(() => { removeActiveExercise(0) })
|
||||
@@ -123,6 +123,30 @@ describe('active-session exercise removal', () => {
|
||||
expect(useStore.getState().S.active.entries.map(e => e.id)).toEqual(['1002'])
|
||||
})
|
||||
|
||||
it('keeps a rest countdown that belongs to another exercise, re-pointed at it', () => {
|
||||
setActive([entry('1001'), entry('1002'), entry('1003')], 1)
|
||||
useUI.getState().startRest(90, 1)
|
||||
|
||||
act(() => { removeActiveExercise(0) })
|
||||
expect(useUI.getState().timer?.forIdx).toBe(0)
|
||||
expect(useStore.getState().S.active.entries.map(e => e.id)).toEqual(['1002', '1003'])
|
||||
|
||||
act(() => { removeActiveExercise(1) })
|
||||
expect(useUI.getState().timer?.forIdx).toBe(0)
|
||||
expect(useStore.getState().S.active.entries.map(e => e.id)).toEqual(['1002'])
|
||||
|
||||
act(() => { removeActiveExercise(0) })
|
||||
expect(useUI.getState().timer).toBeNull()
|
||||
})
|
||||
|
||||
it('leaves a rest countdown without a known owner running', () => {
|
||||
setActive([entry('1001'), entry('1002')], 0)
|
||||
useUI.getState().startRest(90)
|
||||
|
||||
act(() => { removeActiveExercise(0) })
|
||||
expect(useUI.getState().timer).not.toBeNull()
|
||||
})
|
||||
|
||||
it('does not mutate before confirmation, leaves state unchanged on Cancel, and removes on Confirm', () => {
|
||||
renderWorkout([entry('1001'), entry('1002')])
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ const mocks = vi.hoisted(() => {
|
||||
startRest: state.startRest,
|
||||
stopRest: state.stopRest,
|
||||
stopWork: state.stopWork,
|
||||
shiftRestOwner: vi.fn(),
|
||||
startWork: vi.fn(),
|
||||
toast: state.toast,
|
||||
})
|
||||
@@ -171,7 +172,7 @@ describe('Workout set completion flow', () => {
|
||||
await toggleSet(0)
|
||||
|
||||
expect(mocks.startRest).toHaveBeenCalledOnce()
|
||||
expect(mocks.startRest).toHaveBeenCalledWith(90)
|
||||
expect(mocks.startRest).toHaveBeenCalledWith(90, expect.any(Number))
|
||||
expect(mocks.stopRest).not.toHaveBeenCalled()
|
||||
|
||||
await unmount()
|
||||
@@ -275,7 +276,7 @@ describe('Workout set completion flow', () => {
|
||||
|
||||
expect(mocks.topWeightSheet).toHaveBeenCalledWith(1)
|
||||
expect(mocks.S.active.cur).toBe(1)
|
||||
expect(mocks.startRest).toHaveBeenCalledWith(90)
|
||||
expect(mocks.startRest).toHaveBeenCalledWith(90, expect.any(Number))
|
||||
})
|
||||
|
||||
it('skips completed intervening units and selects the next unfinished superset as one unit', async () => {
|
||||
@@ -291,7 +292,7 @@ describe('Workout set completion flow', () => {
|
||||
expect(mocks.S.active.cur).toBe(2)
|
||||
expect(mocks.workoutCompleteSheet).not.toHaveBeenCalled()
|
||||
expect(mocks.toast).toHaveBeenCalledWith('Hold logged')
|
||||
expect(mocks.startRest).toHaveBeenCalledWith(90)
|
||||
expect(mocks.startRest).toHaveBeenCalledWith(90, expect.any(Number))
|
||||
})
|
||||
|
||||
it('wraps to earlier unfinished work instead of showing a false completion prompt', async () => {
|
||||
@@ -304,7 +305,7 @@ describe('Workout set completion flow', () => {
|
||||
|
||||
expect(mocks.S.active.cur).toBe(0)
|
||||
expect(mocks.workoutCompleteSheet).not.toHaveBeenCalled()
|
||||
expect(mocks.startRest).toHaveBeenCalledWith(90)
|
||||
expect(mocks.startRest).toHaveBeenCalledWith(90, expect.any(Number))
|
||||
})
|
||||
|
||||
it('keeps top-weight confirmation in control without declaring completion while work remains', async () => {
|
||||
@@ -444,6 +445,6 @@ describe('superset flow survives an exercise being removed mid-session', () => {
|
||||
|
||||
// Partner closes the round (each still has a second set), which is what starts the rest.
|
||||
await toggleSet(2)
|
||||
expect(mocks.startRest).toHaveBeenCalledWith(90)
|
||||
expect(mocks.startRest).toHaveBeenCalledWith(90, expect.any(Number))
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user