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.

Live Activity Done: the rest counts from the tap, and eight sets ahead

Duarte Santos3664ccfcb785
diff --git a/frontend/src/lib/live-activity.js b/frontend/src/lib/live-activity.js
index c7c24bd59..510dd7d68 100644
--- a/frontend/src/lib/live-activity.js
+++ b/frontend/src/lib/live-activity.js
@@ -30,7 +30,7 @@ const APPLIED_KEY = 'gym_live_applied'
const APPLIED_MAX = 300
const SEND_DEBOUNCE_MS = 300
// The sets after the current one the activity carries, so its Done can move on without the page.
-export const LIVE_AHEAD = 3
+export const LIVE_AHEAD = 8
const num = v => (typeof v === 'number' && Number.isFinite(v) ? v : null)
diff --git a/frontend/src/views/Workout.jsx b/frontend/src/views/Workout.jsx
index cadcca262..18fd3e38a 100644
--- a/frontend/src/views/Workout.jsx
+++ b/frontend/src/views/Workout.jsx
@@ -1756,15 +1756,23 @@ function ActiveWorkout() {
// The Lock Screen's Done (lib/live-activity.js) ticks its set through this same toggle, as if
// tapped here (rest timer, high-water mark, the end-of-workout sheet). The time is the tap's: the
- // page may hear of it much later (lib/finish-workout.js sessionEnd reads the sets' times).
+ // page may hear of it much later (lib/finish-workout.js sessionEnd reads the sets' times), and so
+ // is the rest's start.
const liveToggleRef = useRef(toggle)
liveToggleRef.current = toggle
useEffect(() => bindLiveActions({
setDone: (idx, i, { at }) => {
const A = useStore.getState().S.active
if (!A || A.editingWorkoutId || A.entries?.[idx]?.sets?.[i]?.done) return
+ const restBefore = useUI.getState().timer
liveToggleRef.current(idx, i)
- if (at) mutEntry(idx, en => { const set = en.sets[i]; if (set?.done) set.at = at })
+ if (!at) return
+ mutEntry(idx, en => { const set = en.sets[i]; if (set?.done) set.at = at })
+ // The rest this tick started began at the tap, which the Lock Screen has been counting
+ // down since: heard of late, it is shortened by that much, or over already.
+ const rest = useUI.getState().timer
+ const late = Math.floor((Date.now() - at) / 1000)
+ if (late > 1 && rest && rest !== restBefore && !rest.ready && !rest.paused) useUI.getState().addRest(-late)
},
}), [])
diff --git a/frontend/src/views/Workout.test.jsx b/frontend/src/views/Workout.test.jsx
index b26bf94fa..fe60815b2 100644
--- a/frontend/src/views/Workout.test.jsx
+++ b/frontend/src/views/Workout.test.jsx
@@ -40,6 +40,8 @@ const mocks = vi.hoisted(() => {
workoutSettingsSheet: vi.fn(),
nav: vi.fn(),
setNoteSheet: vi.fn(),
+ addRest: vi.fn(),
+ live: null,
}
state.stopRest = vi.fn(() => { state.timer = null })
state.stopWork = vi.fn(() => { state.work = null })
@@ -59,6 +61,7 @@ const mocks = vi.hoisted(() => {
stopRest: state.stopRest,
stopWork: state.stopWork,
shiftRestOwner: vi.fn(),
+ addRest: state.addRest,
startWork: state.startWork,
toast: state.toast,
})
@@ -82,6 +85,8 @@ vi.mock('../store/useUI.js', () => {
return { useUI }
})
vi.mock('react-router-dom', () => ({ useNavigate: () => mocks.nav }))
+// The Live Activity's Done (lib/live-activity.js): the handler the workout screen binds.
+vi.mock('../lib/live-activity.js', () => ({ bindLiveActions: h => { mocks.live = h; return () => { if (mocks.live === h) mocks.live = null } } }))
vi.mock('../sheets.jsx', () => ({
startFlow: vi.fn(),
exercisePicker: mocks.exercisePicker,
@@ -3443,3 +3448,32 @@ describe('the workout screen chrome (v1.3.11)', () => {
expect(container.querySelector('.wthumb')).toBeNull()
})
})
+
+describe('the Lock Screen\'s Done (Live Activity)', () => {
+ it('ticks the set through the same toggle, with the tap\'s time, and the rest counts from the tap', async () => {
+ mocks.startRest.mockImplementation(sec => { mocks.timer = { left: sec, total: sec, endsAt: Date.now() + sec * 1000 } })
+ await mount([exercise('live-bench', [false, false])])
+ expect(mocks.live).toBeTruthy()
+ const at = Date.now() - 30000
+ await act(async () => { mocks.live.setDone(0, 0, { at }) })
+ const set = mocks.S.active.entries[0].sets[0]
+ expect(set.done).toBe(true)
+ expect(set.at).toBe(at)
+ expect(mocks.startRest).toHaveBeenCalledTimes(1)
+ // 30 s of it already went by on the Lock Screen
+ expect(mocks.addRest).toHaveBeenCalledTimes(1)
+ expect(mocks.addRest.mock.calls[0][0]).toBeLessThanOrEqual(-29)
+ mocks.startRest.mockReset()
+ })
+
+ it('leaves a set already ticked alone, and a saved workout being edited', async () => {
+ await mount([exercise('live-bench', [true, false])])
+ await act(async () => { mocks.live.setDone(0, 0, { at: Date.now() }) })
+ expect(mocks.S.active.entries[0].sets[0].done).toBe(true)
+ expect(mocks.startRest).not.toHaveBeenCalled()
+ await unmount()
+ await mount([exercise('live-bench', [false])], 0, { active: { editingWorkoutId: 'w1' } })
+ await act(async () => { mocks.live.setDone(0, 0, { at: Date.now() }) })
+ expect(mocks.S.active.entries[0].sets[0].done).toBe(false)
+ })
+})