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.

fix(timer): preserve progress bar scale on rest adjustments

Aitor Díaza9d4fa09e06c
diff --git a/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestTimerService.java b/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestTimerService.java
index c12901447..c96daa6a1 100644
--- a/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestTimerService.java
+++ b/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestTimerService.java
@@ -159,7 +159,7 @@ public class RestTimerService extends Service {
// Adding the delta to that old deadline would leave the clock stuck.
long left = paused ? pausedLeft : Math.max(0, endsAt - now);
left += deltaMs;
- totalMs = Math.max(1000, totalMs + deltaMs);
+ if (left > totalMs) totalMs = left;
if (left <= 0) {
finishSkip();
return;
diff --git a/frontend/src/store/useUI.js b/frontend/src/store/useUI.js
index 74f803eb4..862f2d951 100644
--- a/frontend/src/store/useUI.js
+++ b/frontend/src/store/useUI.js
@@ -209,12 +209,13 @@ export const useUI = create((set, get) => ({
// taking off more than is left means "I'm ready now" — same as skipping, and it keeps a
// negative duration out of both the progress bar and the server-side push schedule
if (left <= 0) { get().stopRest(); return }
+ const total = Math.max(tm.total || left, left)
// Paused, there is no end to move and nothing booked on the server: the time is simply held,
// and the notification holds the new figure.
- if (tm.paused) { set({ timer: { ...tm, left, total: tm.total + sec } }); holdRestAlert(left, tm.total + sec); return }
+ if (tm.paused) { set({ timer: { ...tm, left, total } }); holdRestAlert(left, total); return }
const endsAt = tm.endsAt + sec * 1000
- set({ timer: { ...tm, left, total: tm.total + sec, endsAt } })
- bookRestEnd(endsAt, tm.total + sec)
+ set({ timer: { ...tm, left, total, endsAt } })
+ bookRestEnd(endsAt, total)
},
// 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.
diff --git a/frontend/src/store/useUI.native-rest.test.js b/frontend/src/store/useUI.native-rest.test.js
index e006f388e..925dc5a8c 100644
--- a/frontend/src/store/useUI.native-rest.test.js
+++ b/frontend/src/store/useUI.native-rest.test.js
@@ -68,13 +68,13 @@ describe('the rest the app starts, pauses and ends', () => {
expect(disarmRestAlert.mock.calls.length).toBe(disarms) // the notification stays, paused
useUI.getState().addRest(15) // time added while paused: held again at the new figure
- expect(holdRestAlert).toHaveBeenLastCalledWith(75, 105)
+ expect(holdRestAlert).toHaveBeenLastCalledWith(75, 90)
vi.advanceTimersByTime(5 * 60_000)
const arms = armRestAlert.mock.calls.length
useUI.getState().resumeRest()
expect(armRestAlert.mock.calls.length).toBe(arms + 1)
- expect(armRestAlert).toHaveBeenLastCalledWith(Date.now() + 75_000, expect.objectContaining({ totalSec: 105 }))
+ expect(armRestAlert).toHaveBeenLastCalledWith(Date.now() + 75_000, expect.objectContaining({ totalSec: 90 }))
})
it('keeps the alarm when the countdown ends, including while the app is hidden', () => {
diff --git a/frontend/src/store/useUI.test.js b/frontend/src/store/useUI.test.js
index cf9faee01..1a1dde043 100644
--- a/frontend/src/store/useUI.test.js
+++ b/frontend/src/store/useUI.test.js
@@ -266,3 +266,40 @@ describe('rest readiness and optional timed-set overtime', () => {
expect(replacement).not.toHaveBeenCalled()
})
})
+
+describe('addRest adjustments', () => {
+ beforeEach(() => { vi.useFakeTimers(); useUI.setState({ timer: null }) })
+ afterEach(() => { useUI.getState().stopRest(); vi.useRealTimers() })
+
+ it('reduces left without shrinking total so progress bar reflects the decrement', () => {
+ useUI.getState().startRest(60)
+ expect(useUI.getState().timer.left).toBe(60)
+ expect(useUI.getState().timer.total).toBe(60)
+
+ useUI.getState().addRest(-15)
+ expect(useUI.getState().timer.left).toBe(45)
+ expect(useUI.getState().timer.total).toBe(60)
+ })
+
+ it('stops timer if subtraction reaches zero or below', () => {
+ useUI.getState().startRest(15)
+ useUI.getState().addRest(-15)
+ expect(useUI.getState().timer).toBe(null)
+ })
+
+ it('increases left and keeps total if still within initial total', () => {
+ useUI.getState().startRest(60)
+ useUI.getState().addRest(-15) // left: 45, total: 60
+ useUI.getState().addRest(10) // left: 55, total: 60
+ expect(useUI.getState().timer.left).toBe(55)
+ expect(useUI.getState().timer.total).toBe(60)
+ })
+
+ it('expands total if time added exceeds the initial total', () => {
+ useUI.getState().startRest(60)
+ useUI.getState().addRest(15)
+ expect(useUI.getState().timer.left).toBe(75)
+ expect(useUI.getState().timer.total).toBe(75)
+ })
+})
+