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.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
rest timer: the end tone with the app open plays natively, so music ducks for it
Duarte Santos99b2f0c4e0e7
diff --git a/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestAlert.java b/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestAlert.javaindex 208975fff..bc394344b 100644--- a/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestAlert.java+++ b/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestAlert.java@@ -514,6 +514,16 @@ public final class RestAlert {nm.createNotificationChannel(channel);}+ /**+ * The end tone with the app on screen. The page's Web Audio cannot turn other apps down, so+ * over music it was lost (Discord: "the bell doesn't ring while I have music on"); played+ * here it ducks the music like the screen-off alarm does. Off the main thread: playClip+ * waits until the clip has been heard.+ */+ static void playNow(Context ctx, String tone) {+ new Thread(() -> playSound(ctx, tone), "opengym-tone").start();+ }+private static void playSound(Context ctx, String tone) {playClip(ctx, RestTone.render(tone));}diff --git a/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestAlertPlugin.java b/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestAlertPlugin.javaindex d570032c1..cbe464067 100644--- a/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestAlertPlugin.java+++ b/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestAlertPlugin.java@@ -126,6 +126,18 @@ public class RestAlertPlugin extends Plugin {call.resolve();}+ /** The end tone now, over whatever plays, which ducks for it (see RestAlert.playNow). */+ @PluginMethod+ public void tone(PluginCall call) {+ Context ctx = getContext();+ if (ctx == null) {+ call.reject("no context");+ return;+ }+ RestAlert.playNow(ctx.getApplicationContext(), call.getString("kind", "chime"));+ call.resolve();+ }+@PluginMethodpublic void cancel(PluginCall call) {Context ctx = getContext();diff --git a/frontend/src/lib/rest-alert.js b/frontend/src/lib/rest-alert.jsindex c172cf150..3bee27d4a 100644--- a/frontend/src/lib/rest-alert.js+++ b/frontend/src/lib/rest-alert.js@@ -153,6 +153,16 @@ export function buzzAsAlarm(pattern) {.catch(() => false)}+// The end tone through the native side, which ducks music for it (a page cannot). Resolves true+// once it was handed over, false anywhere it cannot (the web, iOS, a failed call) for the caller+// to play the page's own chime instead. Never resolves with the plugin itself — see restPlugin.+export function toneNative(kind) {+ if (!MOBILE) return Promise.resolve(false)+ return restPlugin()+ .then(plugin => (plugin ? plugin.RestAlert.tone({ kind: String(kind || 'chime') }).then(() => true) : false))+ .catch(() => false)+}+// The running countdown repaints with this swatch. No-op when no rest is on screen.export function setRestAccent(key) {if (!MOBILE) returndiff --git a/frontend/src/store/useUI.js b/frontend/src/store/useUI.jsindex 59489f8c1..8b5cbe3b2 100644--- a/frontend/src/store/useUI.js+++ b/frontend/src/store/useUI.js@@ -6,7 +6,7 @@ import { api } from '../lib/api.js'import { t } from '../lib/i18n.js'import { deviceId } from '../lib/push.js'import { MOBILE } from '../lib/mobile.js'-import { armRestAlert, bindNativeRest, disarmRestAlert, holdRestAlert } from '../lib/rest-alert.js'+import { armRestAlert, bindNativeRest, disarmRestAlert, holdRestAlert, toneNative } from '../lib/rest-alert.js'import { useStore } from './useStore.js'import { REST_MAX } from '../lib/duration.js'import { accentValue } from '../lib/accent.js'@@ -110,7 +110,7 @@ const runWork = (set, get) => {const snd = st.sound, endSound = restSoundOf(st)if (left <= 0) {if (seenLive && !wk.alerted) {- chime(snd, endSound)+ endTone(snd, endSound)alertBuzz([200, 100, 200]); get().flashTimer()}if (wk.overtime && left > -MAX_WORK_OVERTIME_SEC) { set({ work: { ...wk, left, alerted: true } }); return }@@ -174,7 +174,7 @@ const runRest = (set, get) => {if (left <= 0 && tm.kind === 'switch') {// Over like a hold is: the chime and its buzz, then the bar goes. No "Ready", no toast:// the other side is the next thing, one tap away.- if (seenLive) { chime(snd, endSound); alertBuzz([200, 100, 200]); get().flashTimer() }+ if (seenLive) { endTone(snd, endSound); alertBuzz([200, 100, 200]); get().flashTimer() }stopRestTicking()set({ timer: null })return@@ -183,7 +183,7 @@ const runRest = (set, get) => {if (seenLive) {// The Android alarm for this end stays quiet while the app is on screen, so this chime is// the only one. Locked, this branch never runs and the alarm's tone does.- chime(snd, endSound)+ endTone(snd, endSound)alertBuzz([200, 100, 200]); get().flashTimer()}// The toast stays even when the rest ran out while the app was hidden: a guest, or anyone@@ -205,6 +205,14 @@ const runRest = (set, get) => {document.addEventListener('visibilitychange', timerTick)}+// The end of a rest or a hold, on screen. In the Android app the native side plays it, so music+// ducks for it; anywhere else, or if that fails, the page's own chime.+function endTone(enabled, kind) {+ if (!enabled) return+ if (!MOBILE) { chime(enabled, kind); return }+ toneNative(kind === true ? 'classic' : kind).then(ok => { if (!ok) chime(enabled, kind) })+}+export const useUI = create((set, get) => ({sheets: [], // { id, render:(close)=>JSX, kind:'sheet'|'center', locked }toastMsg: '',diff --git a/frontend/src/store/useUI.native-rest.test.js b/frontend/src/store/useUI.native-rest.test.jsindex e342bc131..b64f0ee0b 100644--- a/frontend/src/store/useUI.native-rest.test.js+++ b/frontend/src/store/useUI.native-rest.test.js@@ -13,6 +13,7 @@ vi.mock('../lib/rest-alert.js', () => ({holdRestAlert: vi.fn(),disarmRestAlert: vi.fn(),bindNativeRest: vi.fn(cb => { h.native = cb }),+ toneNative: vi.fn(() => Promise.resolve(true)),}))vi.mock('../lib/sound.js', () => ({ beep: vi.fn(), chime: vi.fn(), vibrate: vi.fn(), alertBuzz: vi.fn() }))vi.mock('../lib/api.js', () => ({ api: vi.fn(() => Promise.resolve({ ok: true })) }))@@ -22,6 +23,7 @@ import { useStore } from './useStore.js'import { api } from '../lib/api.js'import { armRestAlert, disarmRestAlert, holdRestAlert } from '../lib/rest-alert.js'import { beep, chime } from '../lib/sound.js'+import { toneNative } from '../lib/rest-alert.js'const hide = hidden => {Object.defineProperty(document, 'hidden', { configurable: true, get: () => hidden })@@ -39,7 +41,7 @@ beforeEach(() => {originalSettings = useStore.getState().SuseStore.setState({ S: { ...originalSettings, sound: true, timerFlash: false }, user: null })useUI.setState({ timer: null, timerFlashId: 0 })- for (const f of [armRestAlert, holdRestAlert, disarmRestAlert, beep, chime, api]) f.mockClear()+ for (const f of [armRestAlert, holdRestAlert, disarmRestAlert, beep, chime, api, toneNative]) f.mockClear()hide(false)})afterEach(() => {@@ -86,18 +88,28 @@ describe('the rest the app starts, pauses and ends', () => {expect(disarmRestAlert.mock.calls.length).toBe(armed)expect(useUI.getState().timer).toMatchObject({ left: 0, ready: true })expect(chime).not.toHaveBeenCalled()+ expect(toneNative).not.toHaveBeenCalled()})- it('still chimes in the app when the rest finishes on screen, and keeps the alarm', () => {+ // On screen the native side plays the end tone (it ducks music, the page cannot).+ it('still sounds in the app when the rest finishes on screen, and keeps the alarm', () => {useStore.setState({ S: { ...useStore.getState().S, timerFlash: true } })useUI.getState().startRest(1)const armed = disarmRestAlert.mock.calls.lengthvi.advanceTimersByTime(1000)- expect(chime).toHaveBeenCalledTimes(1)+ expect(toneNative).toHaveBeenCalledTimes(1)+ expect(chime).not.toHaveBeenCalled()expect(useUI.getState().timerFlashId).toBe(1)expect(disarmRestAlert.mock.calls.length).toBe(armed)})+ it('falls back to the page\'s chime when the native tone cannot play', async () => {+ toneNative.mockResolvedValueOnce(false)+ useUI.getState().startRest(1)+ await vi.advanceTimersByTimeAsync(1000)+ expect(chime).toHaveBeenCalledTimes(1)+ })+it('takes the notification down on Skip, and on Dismiss once it is Ready', () => {useUI.getState().startRest(90)const armed = disarmRestAlert.mock.calls.lengthdiff --git a/frontend/src/store/useUI.silent-buzz.test.js b/frontend/src/store/useUI.silent-buzz.test.jsindex defbae136..7894116f0 100644--- a/frontend/src/store/useUI.silent-buzz.test.js+++ b/frontend/src/store/useUI.silent-buzz.test.js@@ -11,6 +11,7 @@ vi.mock('../lib/rest-alert.js', () => ({holdRestAlert: vi.fn(),disarmRestAlert: vi.fn(),bindNativeRest: vi.fn(),+ toneNative: vi.fn(() => Promise.resolve(true)),}))vi.mock('../lib/sound.js', () => ({ beep: vi.fn(), chime: vi.fn(), vibrate: vi.fn(), alertBuzz: vi.fn() }))vi.mock('../lib/api.js', () => ({ api: vi.fn(() => Promise.resolve({ ok: true })) }))@@ -19,6 +20,7 @@ import { useUI } from './useUI.js'import { useStore } from './useStore.js'import { armRestAlert } from '../lib/rest-alert.js'import { alertBuzz, chime, vibrate } from '../lib/sound.js'+import { toneNative } from '../lib/rest-alert.js'let originalbeforeEach(() => {@@ -69,7 +71,7 @@ describe('vibrate on silent', () => {withSettings({ sound: true, restSound: 'soft' })useUI.getState().startRest(2)await vi.advanceTimersByTimeAsync(3000)- expect(chime).toHaveBeenCalledWith(true, 'soft')+ expect(toneNative).toHaveBeenCalledWith('soft')})it('the end of a rest seen on screen buzzes through alertBuzz', async () => {