Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Rest timer: a reload mid-rest brings the countdown back without booking its alarm twice

Duarte Santosabf38022c684
diff --git a/frontend/src/store/useUI.js b/frontend/src/store/useUI.js
index a481117ab..f303ff252 100644
--- a/frontend/src/store/useUI.js
+++ b/frontend/src/store/useUI.js
@@ -351,6 +351,44 @@ export const useUI = create((set, get) => ({
}
}))
+// A reload in the middle of a rest (pull to refresh, a WebView restart, an update) used to drop
+// the countdown while its alarm still rang later. The rest lives on in sessionStorage for this
+// tab and comes back at boot while its end is still ahead, held if it was paused. Its end was
+// booked when it started (the server's push, the Android alarm), and that booking survives the
+// reload, so it is not booked again: the alert fires once, and the bar's own tick at zero calls
+// the push off as it always does. A rest that ended meanwhile, or one left with no session
+// running, is dropped.
+export const REST_KEY = 'gym_rest'
+const restStore = () => { try { return typeof sessionStorage === 'undefined' ? null : sessionStorage } catch { return null } }
+const saveRest = tm => {
+ const ss = restStore()
+ if (!ss) return
+ try {
+ if (!tm || tm.ready) ss.removeItem(REST_KEY)
+ else ss.setItem(REST_KEY, JSON.stringify({ endsAt: tm.endsAt, total: tm.total, forIdx: tm.forIdx ?? null, kind: tm.kind || null, paused: !!tm.paused, left: tm.left }))
+ } catch { /* the rest just does not outlive a reload */ }
+}
+export function restoreRest(now = Date.now()) {
+ const ss = restStore()
+ let saved = null
+ try { saved = JSON.parse(ss?.getItem(REST_KEY) || 'null') } catch { saved = null }
+ if (!saved || useUI.getState().timer) return false
+ const total = Math.round(Number(saved.total))
+ const ok = useStore.getState().S?.active && total > 0 && (saved.paused ? saved.left > 0 : saved.endsAt > now)
+ if (!ok) { try { ss.removeItem(REST_KEY) } catch { /* nothing to drop */ } return false }
+ const base = { total, forIdx: saved.forIdx ?? undefined, ...(saved.kind === 'switch' ? { kind: 'switch' } : {}) }
+ if (saved.paused) {
+ useUI.setState({ timer: { ...base, left: Math.round(saved.left), endsAt: saved.endsAt, paused: true } })
+ return true
+ }
+ pageHiddenAt = typeof document !== 'undefined' && document.hidden ? now : null
+ useUI.setState({ timer: { ...base, left: Math.max(1, Math.round((saved.endsAt - now) / 1000)), endsAt: saved.endsAt } })
+ runRest(useUI.setState, useUI.getState)
+ return true
+}
+useUI.subscribe((s, prev) => { if (s.timer !== prev.timer) saveRest(s.timer) })
+restoreRest()
+
// Buttons on the rest notification (pause, ±15s, skip) change the countdown in the
// service first, then mirror that into the in-app timer. skip ends it. Seconds round up, as the
// notification's clock does, so a pause in the last half second still holds a second here.
diff --git a/frontend/src/store/useUI.rest-reload.test.js b/frontend/src/store/useUI.rest-reload.test.js
new file mode 100644
index 000000000..2dacde94a
--- /dev/null
+++ b/frontend/src/store/useUI.rest-reload.test.js
@@ -0,0 +1,73 @@
+// @vitest-environment happy-dom
+// A reload mid-rest keeps the countdown (v1.3.11): the rest is kept in sessionStorage and comes
+// back at boot while its end is ahead, without booking its end a second time.
+import { describe, expect, it, beforeEach, afterEach, vi } from 'vitest'
+
+vi.mock('../lib/api.js', () => ({ api: vi.fn(() => Promise.resolve({ ok: true })) }))
+const { chime } = vi.hoisted(() => ({ chime: vi.fn() }))
+vi.mock('../lib/sound.js', () => ({ beep: vi.fn(), chime, vibrate: vi.fn(), alertBuzz: vi.fn() }))
+
+import { api } from '../lib/api.js'
+import { useUI, restoreRest, REST_KEY } from './useUI.js'
+import { useStore } from './useStore.js'
+
+const saved = () => JSON.parse(sessionStorage.getItem(REST_KEY) || 'null')
+const pushes = () => api.mock.calls.filter(([p]) => p === '/api/push/rest-timer')
+
+describe('the rest timer across a reload', () => {
+ let original
+ beforeEach(() => {
+ vi.useFakeTimers()
+ original = { S: useStore.getState().S, user: useStore.getState().user }
+ useStore.setState({ S: { ...original.S, sound: true, active: { id: 'a', entries: [] } }, user: { id: 'u1' } })
+ useUI.setState({ timer: null, work: null, toastMsg: '' })
+ sessionStorage.clear()
+ api.mockClear(); chime.mockClear()
+ })
+ afterEach(() => {
+ useUI.getState().stopRest()
+ useStore.setState(original)
+ vi.useRealTimers()
+ })
+
+ it('keeps the running rest, and forgets it once it is skipped', () => {
+ useUI.getState().startRest(90, 2)
+ expect(saved()).toMatchObject({ total: 90, forIdx: 2, paused: false, kind: null })
+ expect(saved().endsAt).toBe(useUI.getState().timer.endsAt)
+ useUI.getState().pauseRest()
+ expect(saved()).toMatchObject({ paused: true, left: 90 })
+ useUI.getState().stopRest()
+ expect(saved()).toBeNull()
+ })
+
+ it('comes back at boot with the time left, books nothing new, and chimes once at the end', () => {
+ const endsAt = Date.now() + 40_000
+ sessionStorage.setItem(REST_KEY, JSON.stringify({ endsAt, total: 90, forIdx: 1, kind: null, paused: false, left: 50 }))
+ expect(restoreRest()).toBe(true)
+ expect(useUI.getState().timer).toMatchObject({ left: 40, total: 90, endsAt, forIdx: 1 })
+ expect(pushes()).toEqual([])
+ vi.advanceTimersByTime(41_000)
+ expect(useUI.getState().timer).toMatchObject({ ready: true, left: 0 })
+ expect(chime).toHaveBeenCalledTimes(1)
+ expect(saved()).toBeNull()
+ })
+
+ it('a paused rest comes back held', () => {
+ sessionStorage.setItem(REST_KEY, JSON.stringify({ endsAt: Date.now() - 5000, total: 90, forIdx: 0, kind: null, paused: true, left: 33 }))
+ expect(restoreRest()).toBe(true)
+ expect(useUI.getState().timer).toMatchObject({ left: 33, total: 90, paused: true })
+ vi.advanceTimersByTime(60_000)
+ expect(useUI.getState().timer.left).toBe(33)
+ })
+
+ it('a rest that ended meanwhile, or one with no session running, is dropped', () => {
+ sessionStorage.setItem(REST_KEY, JSON.stringify({ endsAt: Date.now() - 1, total: 90, forIdx: 0, paused: false, left: 3 }))
+ expect(restoreRest()).toBe(false)
+ expect(useUI.getState().timer).toBeNull()
+ expect(saved()).toBeNull()
+ useStore.setState({ S: { ...useStore.getState().S, active: null } })
+ sessionStorage.setItem(REST_KEY, JSON.stringify({ endsAt: Date.now() + 30_000, total: 90, forIdx: 0, paused: false, left: 30 }))
+ expect(restoreRest()).toBe(false)
+ expect(useUI.getState().timer).toBeNull()
+ })
+})