Skip to content

fix: keep session stopwatches running while the screen is off - #707

Open
NeiltonGabriel wants to merge 1 commit into
developfrom
fix/session-stopwatch-screen-off
Open

fix: keep session stopwatches running while the screen is off#707
NeiltonGabriel wants to merge 1 commit into
developfrom
fix/session-stopwatch-screen-off

Conversation

@NeiltonGabriel

Copy link
Copy Markdown
Collaborator

Summary

  • Diagnosed the session stopwatch freezing whenever the phone slept: both clocks were plain counters advanced by one on each setInterval tick, and Android suspends JavaScript timers while the screen is off, so every second the device slept was lost for good.
  • Reworked both stopwatches in session-global-context.tsx to derive their elapsed time from wall-clock anchors (timerStartedAtMs/timerBaseSeconds for the exercise clock, totalStartedAtMs/totalBaseSeconds for the session total) instead of incrementing a counter, leaving the interval to only refresh what is displayed.
  • Re-anchored the three mutations that move time (registerSession, toggleTimer, updateTimeElapsed), so pausing still freezes the exercise clock while the session total keeps running, and restarting an exercise still zeroes it.
  • Added an AppState listener that recomputes both clocks on the transition back to active, so the screen catches up the moment the device wakes instead of waiting for the next tick.
  • Applied the same wall-clock derivation to the Stopwatch component's own uncontrolled timer, which carried the identical defect.
  • Bumped version in app.json to 1.1.4.

Validation:

  • npx tsc --noEmit — 0 errors.
  • npm run lint — 0 errors and 7 warnings, all pre-existing and none in the touched files.
  • Exercised the shipped tickSession against a simulated sleep timeline: five minutes with the screen off while running (310s elapsed, nothing lost), two minutes paused with the screen off (exercise clock frozen at 310s, session total advancing to 430s), resume plus another minute asleep, and the 3h cap on the session total.
  • Confirmed crisis/flight durations were already measured from Date.now() and are unaffected by the change; they now sit on an equally accurate session total.

@JoseWenderF
JoseWenderF changed the base branch from main to develop September 3, 2026 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant