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.

Follow-up to #296: with the app on screen the end of a rest sounds once, with the app's own chime, instead of racing the alarm's tone and adding a banner over the app

Duarte Santosd354e89acdb5
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.java
index 8a34cc39a..8fca2d3df 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
@@ -33,7 +33,6 @@ public final class RestAlert {
static final String ACTION_ACCENT = "ch.duartesantos.opengym.rest.ACCENT";
static final String ACTION_HOLD = "ch.duartesantos.opengym.rest.HOLD";
static final String CHANNEL_ID = "rest-over";
- private static volatile boolean toneSuppressed;
private static String lastAlertTitle = "Rest over";
private static boolean lastSound = true;
private static String lastChannel = CHANNEL_ID;
@@ -59,8 +58,6 @@ public final class RestAlert {
String channelId, String visibility, String importance, boolean localOnly,
String countdownTitle, long totalMs) {
if (channelId == null || channelId.isEmpty()) channelId = CHANNEL_ID;
- // A new rest may play its tone. The in-app beep sets this only for the one it covers.
- toneSuppressed = false;
Intent intent = alarmIntent(ctx);
intent.putExtra("id", id);
intent.putExtra("title", title == null ? "" : title);
@@ -90,10 +87,6 @@ public final class RestAlert {
startCountdown(ctx, at, totalMs, countdownTitle);
}
- public static void suppressTone() { toneSuppressed = true; }
-
- public static void allowTone() { toneSuppressed = false; }
-
public static void setAccentColor(int accent, int ink) {
lastAccent = accent;
lastInk = ink;
@@ -126,13 +119,19 @@ public final class RestAlert {
cpu.acquire(10_000);
} catch (Exception ignored) { /* tone still attempts without the lock */ }
try {
+ // The app is on screen: the page has chimed, buzzed and flashed the end itself, and
+ // its bar says Ready. A tone and a banner on top would say it twice. Asking the page
+ // to hush this alarm instead raced it: the page's last tick and this alarm land in
+ // the same few milliseconds, and whichever came first decided.
+ if (RestAlertPlugin.appInFront()) {
+ stopCountdown(ctx);
+ return;
+ }
pokeScreen(ctx);
boolean shown = showNotification(ctx, intent);
if (!shown) vibrateFallback(ctx);
- // One beep. The in-app tone already played if the page was visible, and it asked
- // us to skip this one. Locked, the page never gets there, so this is the beep.
- boolean play = intent.getBooleanExtra("sound", true) && !toneSuppressed;
- toneSuppressed = false;
+ // Locked or in the background, the page cannot play its chime, so this is the one.
+ boolean play = intent.getBooleanExtra("sound", true);
if (play) playSound(ctx);
// The countdown card is the foreground-service notification. Drop it once the
// "rest over" alert is up, including when the WebView is frozen.
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.java
index b1fa76619..20c7f6ad0 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
@@ -21,6 +21,9 @@ import com.getcapacitor.annotation.CapacitorPlugin;
@CapacitorPlugin(name = "RestAlert")
public class RestAlertPlugin extends Plugin {
private static RestAlertPlugin instance;
+ // Started to stopped is when the page is visible (document.hidden flips with it), so it is
+ // when the page's own countdown plays the end of a rest.
+ private static volatile boolean inFront;
@Override
public void load() {
@@ -28,6 +31,20 @@ public class RestAlertPlugin extends Plugin {
super.load();
}
+ @Override
+ protected void handleOnStart() {
+ inFront = true;
+ }
+
+ @Override
+ protected void handleOnStop() {
+ inFront = false;
+ }
+
+ static boolean appInFront() {
+ return inFront;
+ }
+
static void emit(String type, long endsAt, long totalMs, long leftMs, boolean paused) {
if (instance == null) return;
JSObject data = new JSObject();
@@ -124,12 +141,6 @@ public class RestAlertPlugin extends Plugin {
call.resolve();
}
- @PluginMethod
- public void suppressTone(PluginCall call) {
- RestAlert.suppressTone();
- call.resolve();
- }
-
// PluginCall.getLong only accepts a Long, and a JS timestamp often arrives as a Double
// (or a Long when it no longer fits in an int). Number covers all three.
private static long number(PluginCall call, String name, long fallback) {
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 08b583dbe..d7b378280 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
@@ -151,7 +151,6 @@ public class RestTimerService extends Service {
finishSkip();
return;
}
- RestAlert.allowTone();
if (paused) pausedLeft = left;
else {
endsAt = now + left;
diff --git a/frontend/src/lib/rest-alert.js b/frontend/src/lib/rest-alert.js
index 5b95f11ec..b93a49fa9 100644
--- a/frontend/src/lib/rest-alert.js
+++ b/frontend/src/lib/rest-alert.js
@@ -107,8 +107,7 @@ export function holdRestAlert(leftSec, totalSec) {
})
}
-// The page is visible and about to play the in-app beep. Skip the native tone so
-// the two don't both sound. The notification itself still posts.
+// What the notification's own buttons did (pause, ±15s, skip), for useUI to mirror in the bar.
let onNativeRest = null
export function bindNativeRest(cb) { onNativeRest = cb }
@@ -130,14 +129,6 @@ export function setRestAccent(key) {
})
}
-export function hushRestTone() {
- if (!MOBILE) return
- enqueue(async () => {
- const p = await restPlugin()
- if (p) await p.RestAlert.suppressTone()
- })
-}
-
async function ensureNotifPermission() {
try {
const { LocalNotifications } = await import('@capacitor/local-notifications')
diff --git a/frontend/src/store/useUI.js b/frontend/src/store/useUI.js
index 48b8cacb7..ccb328508 100644
--- a/frontend/src/store/useUI.js
+++ b/frontend/src/store/useUI.js
@@ -5,7 +5,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, hushRestTone } from '../lib/rest-alert.js'
+import { armRestAlert, bindNativeRest, disarmRestAlert, holdRestAlert } from '../lib/rest-alert.js'
import { useStore } from './useStore.js'
// Fire-and-forget: lets the server push a "rest over" alert if this tab gets suspended
@@ -96,9 +96,8 @@ const runRest = (set, get) => {
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()
+ // 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)
vibrate([200, 100, 200]); get().flashTimer()
}
diff --git a/frontend/src/store/useUI.native-rest.test.js b/frontend/src/store/useUI.native-rest.test.js
index e3c1a59bb..b6ce07f3b 100644
--- a/frontend/src/store/useUI.native-rest.test.js
+++ b/frontend/src/store/useUI.native-rest.test.js
@@ -12,7 +12,6 @@ vi.mock('../lib/rest-alert.js', () => ({
armRestAlert: vi.fn(() => Promise.resolve(true)),
holdRestAlert: vi.fn(),
disarmRestAlert: vi.fn(),
- hushRestTone: vi.fn(),
bindNativeRest: vi.fn(cb => { h.native = cb }),
}))
vi.mock('../lib/sound.js', () => ({ beep: vi.fn(), chime: vi.fn(), vibrate: vi.fn() }))