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.

Timed hold survives a reload or the app being killed

Duarte Santosb9d505f7997b
diff --git a/frontend/src/store/useUI.js b/frontend/src/store/useUI.js
index db5446975..0f9e5d2db 100644
--- a/frontend/src/store/useUI.js
+++ b/frontend/src/store/useUI.js
@@ -82,6 +82,63 @@ let workTick = null
let workDone = null
const MAX_WORK_OVERTIME_SEC = 15 * 60
+// The hold's countdown tick, from a start or from a restore after a reload.
+const runWork = (set, get) => {
+ stopWorkTicking()
+ workTick = () => {
+ const wk = get().work
+ if (!wk) return
+ const left = Math.max(wk.overtime ? -MAX_WORK_OVERTIME_SEC : 0, Math.round((wk.endsAt - Date.now()) / 1000))
+ const seenLive = !document.hidden && pageHiddenAt === null
+ if (!document.hidden) pageHiddenAt = null
+ if (left === wk.left) return
+ const { sound: snd, classicChime } = useStore.getState().S
+ if (left <= 0) {
+ if (seenLive && !wk.alerted) {
+ chime(snd, classicChime)
+ alertBuzz([200, 100, 200]); get().flashTimer()
+ }
+ if (wk.overtime && left > -MAX_WORK_OVERTIME_SEC) { set({ work: { ...wk, left, alerted: true } }); return }
+ const done = workDone || ownerDone(wk)
+ get().stopWork()
+ // `chimed` tells the set's own tick that this end has already sounded and buzzed — not
+ // so when overtime ran out, whose end chime played when the target was reached.
+ if (done) done(wk.total - left, { chimed: seenLive && !wk.alerted })
+ return
+ }
+ if (left <= 3) beep(snd, 660, 0.1)
+ set({ work: { ...wk, left } })
+ }
+ workInt = setInterval(workTick, 1000)
+ document.addEventListener('visibilitychange', workTick)
+}
+const stopWorkTicking = () => {
+ if (workInt) clearInterval(workInt); workInt = null
+ if (workTick) document.removeEventListener('visibilitychange', workTick); workTick = null
+}
+// The set a hold belongs to, while it still is that one: the same exercise at that place in the
+// running workout, not ticked off yet.
+const ownerSet = o => {
+ const e = useStore.getState().S?.active?.entries?.[o?.idx]
+ const st = e && e.id === o.id ? e.sets?.[o.i] : null
+ return st && !st.done ? st : null
+}
+// A restored hold that ends before the workout screen is back to bind its handler: the time still
+// goes to its set, and a hold that ran out (or got its Done) ticks the set off. No rest follows:
+// that is the workout screen's to start, and the hold mostly ended while the app was away.
+const ownerDone = wk => !wk?.owner ? null : (elapsed, { abandoned = false } = {}) => {
+ if (!ownerSet(wk.owner)) return
+ const { idx, i } = wk.owner
+ useStore.getState().update(s => {
+ const st = s.active.entries[idx].sets[i]
+ st.sec = elapsed
+ if (abandoned) { if (st.planSec == null) st.planSec = wk.total; return }
+ delete st.planSec
+ if (st.sides) return // a per-side row is ticked side by side, on the workout screen
+ st.done = true
+ st.at = Date.now()
+ }, true)
+}
const stopRestTicking = () => {
if (timerInt) clearInterval(timerInt); timerInt = null
if (timerTick) document.removeEventListener('visibilitychange', timerTick); timerTick = null
@@ -277,41 +334,26 @@ export const useUI = create((set, get) => ({
on an early finish; the elapsed time is what actually gets logged, so stopping at 0:38 of a
0:45 hold records 0:38 rather than crediting the full target. `chimed` is true when the
countdown ran out in front of you and the end chime and buzz have just played; `abandoned`
- when a rest displaced the hold (abandonWork). */
- startWork(sec, label, onDone) {
+ when a rest displaced the hold (abandonWork).
+ `owner` ({ idx, i, id }: the active entry and set being held) lets a hold outlive a reload or
+ the app being killed (restoreWork below). onDone is a closure and cannot be kept, so the owner
+ says where the time goes, and the workout screen binds its handler again (bindWork). */
+ startWork(sec, label, onDone, owner = null) {
get().abandonWork() // a hold this one replaces keeps what it held, same as a rest replacing one
get().stopRest()
const total = Math.max(1, Math.round(sec) || 1)
const endsAt = Date.now() + total * 1000
workDone = onDone
pageHiddenAt = document.hidden ? Date.now() : null // see startRest: a stale hide is not a catch-up
- set({ work: { left: total, total, endsAt, label, overtime: useStore.getState().S.timedSetOvertime === true } })
- workTick = () => {
- const wk = get().work
- if (!wk) return
- const left = Math.max(wk.overtime ? -MAX_WORK_OVERTIME_SEC : 0, Math.round((wk.endsAt - Date.now()) / 1000))
- const seenLive = !document.hidden && pageHiddenAt === null
- if (!document.hidden) pageHiddenAt = null
- if (left === wk.left) return
- const { sound: snd, classicChime } = useStore.getState().S
- if (left <= 0) {
- if (seenLive && !wk.alerted) {
- chime(snd, classicChime)
- alertBuzz([200, 100, 200]); get().flashTimer()
- }
- if (wk.overtime && left > -MAX_WORK_OVERTIME_SEC) { set({ work: { ...wk, left, alerted: true } }); return }
- const done = workDone
- get().stopWork()
- // `chimed` tells the set's own tick that this end has already sounded and buzzed — not
- // so when overtime ran out, whose end chime played when the target was reached.
- if (done) done(wk.total - left, { chimed: seenLive && !wk.alerted })
- return
- }
- if (left <= 3) beep(snd, 660, 0.1)
- set({ work: { ...wk, left } })
- }
- workInt = setInterval(workTick, 1000)
- document.addEventListener('visibilitychange', workTick)
+ set({ work: { left: total, total, endsAt, label, overtime: useStore.getState().S.timedSetOvertime === true, ...(owner ? { owner } : {}) } })
+ runWork(set, get)
+ },
+ // The workout screen, back on a hold restored after a reload: `make(work)` gives it the handler
+ // its own start would have. Nothing to do for a hold that has one, or whose set is gone.
+ bindWork(make) {
+ const wk = get().work
+ if (!wk?.owner || workDone || !ownerSet(wk.owner)) return
+ workDone = make(wk)
},
// Ended the hold early — log what was actually held.
finishWorkEarly() {
@@ -319,7 +361,7 @@ export const useUI = create((set, get) => ({
if (!wk) return
const startedAt = wk.endsAt - wk.total * 1000
const elapsed = Math.max(1, Math.min(wk.total + (wk.overtime ? MAX_WORK_OVERTIME_SEC : 0), Math.round((Date.now() - startedAt) / 1000)))
- const done = workDone
+ const done = workDone || ownerDone(wk)
vibrate(30)
get().stopWork()
if (done) done(elapsed)
@@ -334,7 +376,7 @@ export const useUI = create((set, get) => ({
const wk = get().work
if (!wk) { get().stopWork(); return }
const elapsed = wk.total - wk.left
- const done = workDone
+ const done = workDone || ownerDone(wk)
get().stopWork()
// Under two seconds there is nothing to keep: that is a play button tapped by accident, or
// tapped and thought better of, and rounding it up to one second the way an early finish does
@@ -344,8 +386,7 @@ export const useUI = create((set, get) => ({
},
// Abandon without logging anything.
stopWork() {
- if (workInt) clearInterval(workInt); workInt = null
- if (workTick) document.removeEventListener('visibilitychange', workTick); workTick = null
+ stopWorkTicking()
workDone = null
set({ work: null })
}
@@ -394,6 +435,46 @@ export function restoreRest(now = Date.now()) {
useUI.subscribe((s, prev) => { if (s.timer !== prev.timer) saveRest(s.timer) })
restoreRest()
+// The hold (a timed set) the same way: a reload or the app being killed mid-plank used to drop
+// the bar and the seconds with it. Kept beside the rest, with the set it belongs to (`owner`);
+// at boot it carries on while its end is ahead, and one that ended meanwhile is written to its set
+// as held to the end. The workout screen binds its own handler again (bindWork), and until it
+// does the time still lands on the set (ownerDone). Only the start, total and owner are kept:
+// the tick reads `endsAt`, so what it shows after the reload is what the clock says.
+export const WORK_KEY = 'gym_work'
+const saveWork = wk => {
+ const ls = restStore()
+ if (!ls) return
+ try {
+ if (!wk?.owner) ls.removeItem(WORK_KEY)
+ else ls.setItem(WORK_KEY, JSON.stringify({ endsAt: wk.endsAt, total: wk.total, label: wk.label || '', overtime: !!wk.overtime, alerted: !!wk.alerted, owner: wk.owner }))
+ } catch { /* the hold just does not outlive a reload */ }
+}
+export function restoreWork(now = Date.now()) {
+ const ls = restStore()
+ let saved = null
+ try { saved = JSON.parse(ls?.getItem(WORK_KEY) || 'null') } catch { saved = null }
+ if (!saved || useUI.getState().work) return false
+ const total = Math.round(Number(saved.total))
+ const drop = () => { try { ls.removeItem(WORK_KEY) } catch { /* nothing to drop */ } return false }
+ if (!(total > 0) || !(saved.endsAt > 0) || !ownerSet(saved.owner)) return drop()
+ const wk = { left: total, total, endsAt: saved.endsAt, label: saved.label || '', overtime: !!saved.overtime, ...(saved.alerted ? { alerted: true } : {}), owner: saved.owner }
+ const left = Math.round((saved.endsAt - now) / 1000)
+ // Over while the app was away: held to the end (overtime past the target is not known, and
+ // the target is what was planned), with no chime for an end nobody was there for.
+ if (left <= 0 && !(wk.overtime && left > -MAX_WORK_OVERTIME_SEC)) {
+ drop()
+ ownerDone(wk)(total)
+ return true
+ }
+ pageHiddenAt = typeof document !== 'undefined' && document.hidden ? now : null
+ useUI.setState({ work: { ...wk, left, ...(left <= 0 ? { alerted: true } : {}) } })
+ runWork(useUI.setState, useUI.getState)
+ return true
+}
+useUI.subscribe((s, prev) => { if (s.work !== prev.work && (!s.work || !prev.work || s.work.owner !== prev.work.owner || s.work.endsAt !== prev.work.endsAt || s.work.alerted !== prev.work.alerted)) saveWork(s.work) })
+restoreWork()
+
// 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.work-reload.test.js b/frontend/src/store/useUI.work-reload.test.js
new file mode 100644
index 000000000..24b5cd120
--- /dev/null
+++ b/frontend/src/store/useUI.work-reload.test.js
@@ -0,0 +1,89 @@
+// @vitest-environment happy-dom
+// A reload or the app being killed mid-hold (a timed set, a plank) used to drop the bar and the
+// seconds with it. The hold is kept with the set it belongs to, carries on after the reload, and
+// one that ended while the app was away is written to its set as held to the end.
+import { describe, expect, it, beforeEach, afterEach, vi } from 'vitest'
+
+vi.mock('../lib/api.js', () => ({ api: vi.fn(() => Promise.resolve({ ok: true })) }))
+vi.mock('../lib/sound.js', () => ({ beep: vi.fn(), chime: vi.fn(), vibrate: vi.fn(), alertBuzz: vi.fn() }))
+
+import { useUI, restoreWork, WORK_KEY } from './useUI.js'
+import { useStore } from './useStore.js'
+
+const saved = () => JSON.parse(localStorage.getItem(WORK_KEY) || 'null')
+const plankSet = () => useStore.getState().S.active.entries[1].sets[0]
+const owner = { idx: 1, i: 0, id: 'plank' }
+
+let original
+beforeEach(() => {
+ vi.useFakeTimers()
+ original = useStore.getState().S
+ useStore.setState({ S: { ...original, sound: true, active: { id: 'a', entries: [
+ { id: 'squat', sets: [{ w: 100, r: 5 }] },
+ { id: 'plank', sets: [{ sec: 45 }, { sec: 45 }] },
+ ] } } })
+ useUI.setState({ timer: null, work: null })
+ localStorage.clear()
+})
+afterEach(() => {
+ useUI.getState().stopWork()
+ useUI.getState().stopRest()
+ useStore.setState({ S: original })
+ vi.useRealTimers()
+})
+
+describe('a hold across a reload', () => {
+ it('is kept with its set while it runs, and forgotten once it ends', () => {
+ const done = vi.fn()
+ useUI.getState().startWork(45, 'Plank', done, owner)
+ expect(saved()).toMatchObject({ total: 45, label: 'Plank', owner })
+ useUI.getState().finishWorkEarly()
+ expect(done).toHaveBeenCalled()
+ expect(saved()).toBeNull()
+ })
+
+ it('a hold without an owner is not kept', () => {
+ useUI.getState().startWork(45, 'Plank', vi.fn())
+ expect(saved()).toBeNull()
+ })
+
+ it('carries on after the reload, and the time lands on its set even before the screen is back', () => {
+ const endsAt = Date.now() + 42_000
+ localStorage.setItem(WORK_KEY, JSON.stringify({ endsAt, total: 45, label: 'Plank', overtime: false, owner }))
+ expect(restoreWork()).toBe(true)
+ expect(useUI.getState().work).toMatchObject({ left: 42, total: 45, label: 'Plank', owner })
+ vi.advanceTimersByTime(43_000)
+ expect(useUI.getState().work).toBeNull()
+ expect(plankSet()).toMatchObject({ sec: 45, done: true })
+ expect(saved()).toBeNull()
+ })
+
+ it('the workout screen binds its own handler again, and Done goes to it', () => {
+ localStorage.setItem(WORK_KEY, JSON.stringify({ endsAt: Date.now() + 40_000, total: 45, label: 'Plank', overtime: false, owner }))
+ restoreWork()
+ const handler = vi.fn()
+ useUI.getState().bindWork(wk => { expect(wk.owner).toEqual(owner); return handler })
+ vi.advanceTimersByTime(10_000)
+ useUI.getState().finishWorkEarly()
+ expect(handler).toHaveBeenCalledWith(15)
+ expect(plankSet().done).toBeUndefined() // the screen's handler ticks it, not the fallback
+ })
+
+ it('one that ended while the app was away is written as held to the end', () => {
+ localStorage.setItem(WORK_KEY, JSON.stringify({ endsAt: Date.now() - 5_000, total: 45, label: 'Plank', overtime: false, owner }))
+ expect(restoreWork()).toBe(true)
+ expect(useUI.getState().work).toBeNull()
+ expect(plankSet()).toMatchObject({ sec: 45, done: true })
+ expect(saved()).toBeNull()
+ })
+
+ it('is dropped when its set is gone or already ticked', () => {
+ localStorage.setItem(WORK_KEY, JSON.stringify({ endsAt: Date.now() + 30_000, total: 45, label: 'Plank', overtime: false, owner: { idx: 0, i: 0, id: 'plank' } }))
+ expect(restoreWork()).toBe(false)
+ expect(useUI.getState().work).toBeNull()
+ expect(saved()).toBeNull()
+ useStore.getState().update(s => { s.active.entries[1].sets[0].done = true }, false)
+ localStorage.setItem(WORK_KEY, JSON.stringify({ endsAt: Date.now() + 30_000, total: 45, label: 'Plank', overtime: false, owner }))
+ expect(restoreWork()).toBe(false)
+ })
+})
diff --git a/frontend/src/views/Workout.jsx b/frontend/src/views/Workout.jsx
index b2ad0a2db..dc6c387f6 100644
--- a/frontend/src/views/Workout.jsx
+++ b/frontend/src/views/Workout.jsx
@@ -1311,8 +1311,12 @@ function ActiveWorkout() {
// the row is held to the end, ticked, or given a duration you typed yourself, and it never
// reaches S.workouts (lib/finish-workout.js).
const plan = (!e.sets[i].done && e.sets[i].planSec) || e.sets[i].sec || 45
- useUI.getState().startWork(plan, exerciseNameText(exOr(e.id)), (elapsed, { abandoned = false, chimed = false } = {}) => {
- // A hold a rest displaced (useUI.abandonWork: a set ticked on another row, or another
+ useUI.getState().startWork(plan, exerciseNameText(exOr(e.id)), holdDone(idx, i, plan), { idx, i, id: e.id })
+ }
+ // What a hold hands back to its row: on its end, its Done, or a rest displacing it. Also bound
+ // again to a hold restored after a reload (useUI.bindWork, below).
+ const holdDone = (idx, i, plan) => (elapsed, { abandoned = false, chimed = false } = {}) => {
+ // A hold a rest displaced (useUI.abandonWork: a set ticked on another row, or another
// exercise) keeps its seconds and nothing else. It is not a finish: the row stays unticked
// and starts no rest, because the rest that displaced the hold is already counting down —
// and the plan it was held against is put aside so the row still knows what it is asking for.
@@ -1326,8 +1330,13 @@ function ActiveWorkout() {
}
mutEntry(idx, en => { en.sets[i].sec = elapsed; delete en.sets[i].planSec })
if (!useStore.getState().S.active.entries[idx].sets[i].done) toggle(idx, i, undefined, { quiet: chimed })
- })
}
+ // A hold that came back from a reload has no handler yet: this screen gives it its own.
+ const holdDoneRef = useRef(holdDone)
+ holdDoneRef.current = holdDone
+ useEffect(() => {
+ useUI.getState().bindWork?.(wk => (...a) => holdDoneRef.current(wk.owner.idx, wk.owner.i, wk.total)(...a))
+ }, [])
// `quiet`: the hold that ticks this set has just ended with the chime and its buzz pattern
// (store/useUI.js). The tick's own beep would sound over the chime's first note and clip it,