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.

Merge #296: Android: rest countdown in a notification (#122) (vitox013)

Duarte Santos224e823f31cb
diff --cc frontend/android/app/src/main/AndroidManifest.xml
index bf03f6942,1fe604391..09d3687dd
--- a/frontend/android/app/src/main/AndroidManifest.xml
+++ b/frontend/android/app/src/main/AndroidManifest.xml
@@@ -43,10 -61,13 +61,16 @@@
on Android 12+. Where it isn't granted, scheduling silently falls back to inexact
alarms — the reminder still arrives, just not to the minute. -->
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
+ <!-- Same exact-alarm permission as the day reminder. Where it isn't granted, the rest
+ alert falls back to an inexact alarm and, if this phone is paired, to Web Push. -->
+ <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
+ <!-- The buzz at the end of a rest or a hold and on a set tick (navigator.vibrate). The
+ WebView drops every vibrate call from an app without this permission, so the phone
+ stayed still. A normal permission: granted at install, no prompt. -->
<uses-permission android:name="android.permission.VIBRATE" />
+ <uses-permission android:name="android.permission.WAKE_LOCK" />
+ <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
+ <uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<!-- Gym check-in: scanning a membership QR/barcode with the camera. -->
<uses-permission android:name="android.permission.CAMERA" />
</manifest>
diff --cc frontend/src/store/useUI.js
index 28b885b94,1c1a4f901..a63747680
--- a/frontend/src/store/useUI.js
+++ b/frontend/src/store/useUI.js
@@@ -13,6 -15,6 +15,20 @@@ import { useStore } from './useStore.js
const pushRestTimer = sec => { if (useStore.getState().user) api('/api/push/rest-timer', { method: 'POST', body: JSON.stringify({ seconds: sec, deviceId: deviceId() }) }).catch(() => {}) }
const cancelPushRestTimer = () => { if (useStore.getState().user) api('/api/push/rest-timer/cancel', { method: 'POST', body: JSON.stringify({ deviceId: deviceId() }) }).catch(() => {}) }
++// Books the end of a rest with whatever can announce it while the app is not looking: in the
++// Android app a native alarm and the countdown notification, everywhere else (and wherever that
++// alarm could not be set) the server's push. The web build books the push at once, as before.
++const bookRestEnd = (endsAt, totalSec) => {
++ if (!MOBILE) { pushRestTimer(Math.max(1, Math.round((endsAt - Date.now()) / 1000))); return }
++ const { S } = useStore.getState()
++ armRestAlert(endsAt, { title: t('Rest over'), countdownTitle: t('Rest'), totalSec, accent: S.accent, sound: !!S.sound })
++ .then(ok => {
++ // Only for the rest that asked: one skipped or moved since then has booked its own end.
++ const tm = useUI.getState().timer
++ if (!ok && tm && !tm.paused && !tm.ready && tm.endsAt === endsAt) pushRestTimer(Math.max(1, Math.round((endsAt - Date.now()) / 1000)))
++ })
++}
++
const notificationsSupported = () => typeof window !== 'undefined' && 'Notification' in window
// Set the moment the tab goes hidden, never cleared here — timerTick/workTick read and
@@@ -58,45 -62,6 +76,50 @@@ let timerTick = nul
let workInt = null
let workTick = null
let workDone = null
+const MAX_WORK_OVERTIME_SEC = 15 * 60
+
+const stopRestTicking = () => {
+ if (timerInt) clearInterval(timerInt); timerInt = null
+ if (timerTick) document.removeEventListener('visibilitychange', timerTick); timerTick = null
+}
+// The rest countdown's tick, from a start or from a resume: the same endsAt-based count either
+// way, so a paused rest carries on exactly as a started one runs.
+const runRest = (set, get) => {
+ stopRestTicking()
+ timerTick = () => {
+ const tm = get().timer
+ if (!tm || tm.ready || tm.paused) return
+ const left = Math.max(0, Math.round((tm.endsAt - Date.now()) / 1000))
+ const seenLive = !document.hidden && pageHiddenAt === null
+ if (!document.hidden) pageHiddenAt = null
+ if (left === tm.left) return
+ const snd = useStore.getState().S.sound
+ if (left <= 0) {
+ if (seenLive) {
++ // The alarm is about to post the same moment. Tell it not to play, so this
++ // chime is the only one. Locked, this branch never runs and the alarm tone does.
++ hushRestTone()
+ chime(snd)
+ vibrate([200, 100, 200]); get().flashTimer()
+ }
+ // The toast stays even when the rest ran out while the app was hidden: a guest, or anyone
+ // without push permission, gets no notification, and a countdown that silently vanishes
+ // on reopen reads like a bug. Only the loud parts (beep, vibration, flash) are gated.
++ // The native alarm is left armed: this tick can come a little early, and with the screen
++ // locked it never runs at all.
+ get().toast(t('Rest over — next set!'))
- maybeRestNotification()
++ if (!MOBILE) maybeRestNotification()
+ cancelPushRestTimer()
+ stopRestTicking()
+ set({ timer: { ...tm, left: 0, ready: true } })
+ return
+ }
+ if (left <= 3) beep(snd, 660, 0.1)
+ set({ timer: { ...tm, left } })
+ }
+ timerInt = setInterval(timerTick, 1000)
+ document.addEventListener('visibilitychange', timerTick)
+}
export const useUI = create((set, get) => ({
sheets: [], // { id, render:(close)=>JSX, kind:'sheet'|'center', locked }
@@@ -132,49 -96,46 +155,51 @@@
// Rest timer set to Off. Stopping and returning rather than starting a zero-length timer
// keeps every caller honest: the four places that start a rest do not each need to know.
if (!(sec > 0)) return
+ // And the hold, the other way round from startWork: the two must never run together (see the
+ // work timer below). A set ticked by hand while its hold ran used to leave both going — the
+ // rest bar with its Skip and ±15 s hidden behind the hold bar, and then the hold reaching
+ // zero under a rest that was still counting down, beeping its own end and logging the full
+ // target for a set nobody was holding any more. Below the guard, not above it: a rest that
+ // does not start has nothing to run alongside the hold, and taking the hold down for it
+ // would throw away a plank in progress for nothing. What it held is kept either way —
+ // abandonWork, not stopWork.
+ get().abandonWork()
+ // Nothing clears pageHiddenAt but a tick, so an app switch with no timer running left it set
+ // for good. The next timer's first tick then read it as "this countdown ran out while the app
+ // was away" and finished in silence — a one-second rest, started on screen, over on screen,
+ // with no beep, no vibration and no flash. Each timer starts from where the page is now.
+ pageHiddenAt = document.hidden ? Date.now() : null
const endsAt = Date.now() + sec * 1000
set({ timer: { left: sec, total: sec, endsAt, forIdx } })
- pushRestTimer(sec)
- // Mobile: the alarm is armed now, because the WebView will not be around to notice
- // zero if the screen locks. Web, iOS, and a failed Android schedule keep the server push.
- armRestAlert(endsAt, { title: t('Rest over'), countdownTitle: t('Rest'), totalSec: sec, accent: useStore.getState().S.accent, sound: !!useStore.getState().S.sound })
- .then(ok => { if (!ok) pushRestTimer(sec) })
- timerTick = () => {
- const tm = get().timer
- if (!tm || tm.paused) return
- const left = Math.max(0, Math.round((tm.endsAt - Date.now()) / 1000))
- const seenLive = !document.hidden && pageHiddenAt === null
- if (!document.hidden) pageHiddenAt = null
- if (left === tm.left) return
- const snd = useStore.getState().S.sound
- if (left <= 0) {
- // +15 on the notification can move the deadline in the same instant this tick
- // decided the rest was over. If it did, keep counting instead of closing.
- const extended = get().timer
- if (extended && !extended.paused && extended.endsAt - Date.now() > 500) return
- if (seenLive) {
- // The alarm is about to post the same moment. Tell it not to play, so this
- // beep is the only one. Locked, this branch never runs and the alarm tone does.
- hushRestTone()
- beep(snd, 880, 0.15); beep(snd, 880, 0.15, 0.25); beep(snd, 1320, 0.4, 0.5)
- vibrate([200, 100, 200]); get().flashTimer()
- }
- // The toast stays even when the rest ran out while the app was hidden: a guest, or anyone
- // without push permission, gets no notification, and a countdown that silently vanishes
- // on reopen reads like a bug. Only the loud parts (beep, vibration, flash) are gated.
- // keepAlert: this tick is often a little early, and with the screen locked it never
- // runs at all. Cancelling here would drop the alarm before it fires.
- get().toast(t('Rest over — next set!'))
- if (!MOBILE) maybeRestNotification()
- get().stopRest({ keepAlert: true }); return
- }
- if (left <= 3) beep(snd, 660, 0.1)
- set({ timer: { ...tm, left } })
- }
- timerInt = setInterval(timerTick, 1000)
- document.addEventListener('visibilitychange', timerTick)
++ bookRestEnd(endsAt, sec)
+ runRest(set, get)
+ },
+ // Holding the rest where it is — a longer break than planned, a machine to wait for, a phone
+ // call (#193). Paused time does not count: the countdown stops, and so does everything that
+ // would announce its end — the server's push and the alert this tab shows are cancelled, not
+ // left to fire at the old time. The timed hold is a different timer and is not touched.
+ pauseRest() {
+ const tm = get().timer
+ if (!tm || tm.ready || tm.paused) return
+ stopRestTicking()
+ cancelPushRestTimer()
++ disarmRestAlert()
+ const left = Math.max(1, Math.round((tm.endsAt - Date.now()) / 1000))
+ set({ timer: { ...tm, left, paused: true } })
+ },
+ // Carrying on from where the pause held it: the end moves out by however long the pause was,
+ // and the push that announces it is booked again for the new time.
+ resumeRest() {
+ const tm = get().timer
+ if (!tm?.paused) return
+ const { paused, ...rest } = tm
+ // As in startRest: the page may have been hidden and shown while paused, and that is no
+ // catch-up of a countdown that was not running.
+ pageHiddenAt = document.hidden ? Date.now() : null
- set({ timer: { ...rest, endsAt: Date.now() + tm.left * 1000 } })
- pushRestTimer(tm.left)
++ const endsAt = Date.now() + tm.left * 1000
++ set({ timer: { ...rest, endsAt } })
++ bookRestEnd(endsAt, tm.total)
+ runRest(set, get)
},
addRest(sec) {
const tm = get().timer
@@@ -184,10 -144,10 +209,11 @@@
// 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 }
+ // Paused, there is no end to move and nothing booked on the server: the time is simply held.
+ if (tm.paused) { set({ timer: { ...tm, left, total: tm.total + sec } }); return }
- set({ timer: { ...tm, left, total: tm.total + sec, endsAt: tm.endsAt + sec * 1000 } })
- pushRestTimer(left)
+ const endsAt = tm.endsAt + sec * 1000
+ set({ timer: { ...tm, left, total: tm.total + sec, endsAt } })
- armRestAlert(endsAt, { title: t('Rest over'), countdownTitle: t('Rest'), totalSec: tm.total + sec, accent: useStore.getState().S.accent, sound: !!useStore.getState().S.sound })
- .then(ok => { if (!ok) pushRestTimer(left) })
++ bookRestEnd(endsAt, tm.total + sec)
},
// 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.
@@@ -196,8 -156,43 +222,19 @@@
if (!tm || !(tm.forIdx >= at)) return
set({ timer: { ...tm, forIdx: tm.forIdx + delta } })
},
+ // The notification added time after this screen had already closed the rest.
+ // Put the countdown back without tearing down the native timer that is still running.
+ reviveRest(endsAt, total, left, paused) {
- set({ timer: { left, total, endsAt, paused: !!paused, forIdx: get().timer?.forIdx } })
- if (paused || timerInt) return
- timerTick = () => {
- const tm = get().timer
- if (!tm || tm.paused) return
- const leftNow = Math.max(0, Math.round((tm.endsAt - Date.now()) / 1000))
- const seenLive = !document.hidden && pageHiddenAt === null
- if (!document.hidden) pageHiddenAt = null
- if (leftNow === tm.left) return
- const snd = useStore.getState().S.sound
- if (leftNow <= 0) {
- const extended = get().timer
- if (extended && !extended.paused && extended.endsAt - Date.now() > 500) return
- if (seenLive) {
- hushRestTone()
- beep(snd, 880, 0.15); beep(snd, 880, 0.15, 0.25); beep(snd, 1320, 0.4, 0.5)
- vibrate([200, 100, 200]); get().flashTimer()
- }
- get().toast(t('Rest over — next set!'))
- if (!MOBILE) maybeRestNotification()
- get().stopRest({ keepAlert: true }); return
- }
- if (leftNow <= 3) beep(snd, 660, 0.1)
- set({ timer: { ...tm, left: leftNow } })
- }
- timerInt = setInterval(timerTick, 1000)
- document.addEventListener('visibilitychange', timerTick)
++ pageHiddenAt = document.hidden ? Date.now() : null
++ set({ timer: { left, total, endsAt, forIdx: get().timer?.forIdx, ...(paused ? { paused: true } : {}) } })
++ if (paused) stopRestTicking()
++ else runRest(set, get)
+ },
- stopRest({ keepAlert = false } = {}) {
- if (timerInt) clearInterval(timerInt); timerInt = null
- if (timerTick) document.removeEventListener('visibilitychange', timerTick); timerTick = null
- // A natural finish leaves the scheduled alarm in place. Skip, replace, and "rest off"
- // cancel it — otherwise the alert fires after the user already moved on.
- if (!keepAlert) disarmRestAlert()
+ stopRest() {
+ stopRestTicking()
++ // Skip, Dismiss, a rest replacing this one and "rest off" all take the native alarm and
++ // its notifications down with it, or the alert fires after the user already moved on.
++ disarmRestAlert()
if (get().timer) cancelPushRestTimer()
set({ timer: null })
},
@@@ -284,3 -254,18 +321,18 @@@
set({ work: null })
}
}))
+
+ // 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.
+ bindNativeRest(ev => {
+ if (!ev) return
+ if (ev.type === 'skip') { useUI.getState().stopRest(); return }
+ const tm = useUI.getState().timer
+ const left = Math.max(0, Math.round((ev.leftMs || 0) / 1000))
+ const total = Math.max(1, Math.round((ev.totalMs || 0) / 1000))
- if (!tm) {
++ if (!tm || tm.ready) {
+ if (ev.type === 'adjust' && left > 0) useUI.getState().reviveRest(ev.endsAt, total, left, ev.paused)
+ return
+ }
+ useUI.setState({ timer: { ...tm, left, total, endsAt: ev.endsAt || tm.endsAt, paused: !!ev.paused } })
+ })
diff --cc frontend/src/views/Settings.jsx
index aa8d16bd9,9931b7e83..5b0181702
--- a/frontend/src/views/Settings.jsx
+++ b/frontend/src/views/Settings.jsx
@@@ -3,25 -3,22 +3,26 @@@ import { useNavigate } from 'react-rout
import { useStore, DEF, hasData } from '../store/useStore.js'
import { workoutControls } from '../lib/workout-controls.js'
import { convertStateUnit } from '../lib/units.js'
+import { speedUnitOf } from '../lib/speed.js'
+import { copyText } from '../lib/clipboard.js'
import { useUI } from '../store/useUI.js'
-import { ACCENTS, todayISO, localTZ, weekStartOf, MONDAY, SUNDAY } from '../lib/format.js'
+import { ACCENTS, todayISO, localTZ, weekStartOf, MONDAY, SUNDAY, fmtPlate } from '../lib/format.js'
+import { inventoryFor, ownsPlates } from '../lib/plates.js'
import { effortOf } from '../lib/history.js'
-import { unlock, playOnSilentSupported } from '../lib/sound.js'
-import { api, webauthnOK, passkeyLogin, passkeyRegister, IS_ANDROID } from '../lib/api.js'
+import { unlock, playOnSilentSupported, vibrateSupported } from '../lib/sound.js'
+import { api, webauthnOK, passkeyRegister, IS_ANDROID } from '../lib/api.js'
import { pushSupported, enablePush, disablePush, sendTestPush, syncPushSubscription } from '../lib/push.js'
import { wakeLockSupported } from '../lib/wakelock.js'
-import { t, LANGS, INSTR_LANGS } from '../lib/i18n.js'
+import { t, LANGS, INSTR_LANGS, EXERCISE_NAME_LANGS, baseLang } from '../lib/i18n.js'
import { DEMO, REPO } from '../lib/demo.js'
import { MOBILE, isAndroid, shareExport, syncReminder } from '../lib/mobile.js'
+ import { setRestAccent } from '../lib/rest-alert.js'
import { checkForUpdate, downloadAndInstall } from '../lib/update.js'
import { forgetCoach } from '../lib/coach-api.js'
-import { ConnectSheet } from './MobileOnboarding.jsx'
-import { starterPlanSheet, confirmSheet, importFromApp, importFromHevy, equipmentProfileSheet, menuSheet, askAddDeviceData } from '../sheets.jsx'
+import { starterPlanSheet, confirmSheet, importFromApp, importFromHevy, equipmentProfileSheet, plateInventorySheet, menuSheet } from '../sheets.jsx'
import Icon from '../components/Icon.jsx'
+import { ServerSyncSection, KeptChangesRows, leaveServer, connectServer, passkeySignIn } from '../components/ServerSync.jsx'
+import { passwordOn, PasswordRow, openPasswordSignIn, openPasswordRegister } from '../components/PasswordAuth.jsx'
import { Section, Row, SelectRow, Switch, Segmented, Button, TextField } from '../components/ui.jsx'
export default function Settings() {