Merge !89: Timer flash: blink the real theme instead of a black/white overlay, sk
This commit is contained in:
commit
dca8393a7a
4 files changed
+72
-21
No files matched your search
@@ -1,8 +1,22 @@
|
||||
import { useEffect } from 'react'
|
||||
import { useUI } from '../store/useUI.js'
|
||||
|
||||
// A new key restarts the CSS sequence even when two timers finish close together.
|
||||
// The layer never catches taps and ends transparent, so it can safely stay mounted.
|
||||
// Flips the app's real light/dark theme on and off twice (~2.4s) instead of laying an
|
||||
// opaque black/white rectangle over the screen — the alert reads as the app itself
|
||||
// blinking, and it always settles back on whatever theme the user actually had.
|
||||
export default function TimerFlash() {
|
||||
const id = useUI(s => s.timerFlashId)
|
||||
return id ? <div key={id} className="timer-flash" aria-hidden="true" /> : null
|
||||
useEffect(() => {
|
||||
if (!id) return
|
||||
const de = document.documentElement
|
||||
const original = de.dataset.theme
|
||||
const opposite = original === 'light' ? 'dark' : 'light'
|
||||
const steps = [opposite, original, opposite, original]
|
||||
const timers = steps.map((theme, i) => setTimeout(() => { de.dataset.theme = theme }, i * 600))
|
||||
return () => {
|
||||
timers.forEach(clearTimeout)
|
||||
de.dataset.theme = original
|
||||
}
|
||||
}, [id])
|
||||
return null
|
||||
}
|
||||
@@ -802,19 +802,6 @@ h4.sec{
|
||||
.glyph-cell:active{transform:scale(.92)}
|
||||
.glyph-cell.on{background:var(--acc);color:var(--on-acc)}
|
||||
|
||||
/* --- opt-in timer completion flash ---
|
||||
Four deliberate pulses over 2.4s stay below three flashes per second. Transparent
|
||||
gaps keep the black and white pulses visually distinct. */
|
||||
.timer-flash{
|
||||
position:fixed;inset:0;z-index:1000;pointer-events:none;opacity:0;
|
||||
animation:timer-flash-four 2.4s linear both;
|
||||
}
|
||||
@keyframes timer-flash-four{
|
||||
0%,20%,28%,48%,56%,76%,84%,100%{opacity:0}
|
||||
4%,16%,60%,72%{background:#000;opacity:1}
|
||||
32%,44%,88%,96%{background:#fff;opacity:1}
|
||||
}
|
||||
|
||||
/* --- big weight readout --- */
|
||||
.bwstep{display:flex;align-items:center;justify-content:center;gap:18px;margin:14px 0 6px}
|
||||
.bw-pm{
|
||||
|
||||
@@ -13,6 +13,16 @@ const cancelPushRestTimer = () => { if (useStore.getState().user) api('/api/push
|
||||
const notificationsSupported = () => typeof window !== 'undefined' && 'Notification' in window
|
||||
let requestRestNotificationPermissionP = null
|
||||
|
||||
// Set the moment the tab goes hidden, never cleared here — timerTick/workTick read and
|
||||
// clear it themselves once they're running visible again. Lets a completion tick tell
|
||||
// "the countdown hit zero while the app was actually open" from "it hit zero while
|
||||
// backgrounded/closed and we're only just catching up now that it's open again" — the
|
||||
// latter must skip beep/vibrate/flash/toast and rely solely on the push notification.
|
||||
let pageHiddenAt = null
|
||||
if (typeof document !== 'undefined') {
|
||||
document.addEventListener('visibilitychange', () => { if (document.hidden) pageHiddenAt = Date.now() })
|
||||
}
|
||||
|
||||
const requestRestNotificationPermission = async () => {
|
||||
if (!notificationsSupported()) return false
|
||||
if (Notification.permission === 'granted') return true
|
||||
@@ -59,7 +69,7 @@ export const useUI = create((set, get) => ({
|
||||
timer: null, // rest countdown between sets — { left, total, endsAt, forIdx }
|
||||
// forIdx: index of the active entry whose set started the rest (undefined when unknown)
|
||||
work: null, // work countdown DURING a timed set (issue #16) — { left, total, endsAt, label }
|
||||
timerFlashId: 0, // changing the id remounts the four-pulse visual alert
|
||||
timerFlashId: 0, // changing the id retriggers the theme-blink visual alert
|
||||
|
||||
flashTimer() {
|
||||
if (!useStore.getState().S.timerFlash) return
|
||||
@@ -94,11 +104,16 @@ export const useUI = create((set, get) => ({
|
||||
const tm = get().timer
|
||||
if (!tm) 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) {
|
||||
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(); maybeRestNotification(); get().toast(t('Rest over — next set!')); get().stopRest(); return
|
||||
if (seenLive) {
|
||||
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!'))
|
||||
}
|
||||
maybeRestNotification(); get().stopRest(); return
|
||||
}
|
||||
if (left <= 3) beep(snd, 660, 0.1)
|
||||
set({ timer: { ...tm, left } })
|
||||
@@ -149,11 +164,15 @@ export const useUI = create((set, get) => ({
|
||||
const wk = get().work
|
||||
if (!wk) return
|
||||
const left = Math.max(0, Math.round((wk.endsAt - Date.now()) / 1000))
|
||||
const seenLive = !document.hidden && pageHiddenAt === null
|
||||
if (!document.hidden) pageHiddenAt = null
|
||||
if (left === wk.left) return
|
||||
const snd = useStore.getState().S.sound
|
||||
if (left <= 0) {
|
||||
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()
|
||||
if (seenLive) {
|
||||
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()
|
||||
}
|
||||
const done = workDone
|
||||
get().stopWork()
|
||||
if (done) done(wk.total)
|
||||
|
||||
@@ -64,4 +64,35 @@ describe('opt-in timer screen flash', () => {
|
||||
vi.advanceTimersByTime(1000)
|
||||
expect(useUI.getState().timerFlashId).toBe(1)
|
||||
})
|
||||
|
||||
const goHidden = () => { Object.defineProperty(document, 'hidden', { value: true, configurable: true }); document.dispatchEvent(new Event('visibilitychange')) }
|
||||
const goVisible = () => { Object.defineProperty(document, 'hidden', { value: false, configurable: true }); document.dispatchEvent(new Event('visibilitychange')) }
|
||||
afterEach(() => goVisible()) // leave document.hidden the way every other test expects it
|
||||
|
||||
it('does not flash a rest that expires while the app is hidden, even once reopened', () => {
|
||||
useStore.setState({ S: { ...useStore.getState().S, timerFlash: true } })
|
||||
useUI.getState().startRest(90)
|
||||
goHidden()
|
||||
vi.setSystemTime(Date.now() + 91_000) // deadline passes with no ticks — the app was actually closed/suspended
|
||||
goVisible() // reopening re-fires visibilitychange, which is how the bug used to trigger
|
||||
expect(useUI.getState().timerFlashId).toBe(0)
|
||||
expect(useUI.getState().timer).toBe(null)
|
||||
})
|
||||
|
||||
it('does not flash a timed exercise that finishes while the app is hidden', () => {
|
||||
useStore.setState({ S: { ...useStore.getState().S, timerFlash: true } })
|
||||
useUI.getState().startWork(90, 'Plank', vi.fn())
|
||||
goHidden()
|
||||
vi.setSystemTime(Date.now() + 91_000)
|
||||
goVisible()
|
||||
expect(useUI.getState().timerFlashId).toBe(0)
|
||||
expect(useUI.getState().work).toBe(null)
|
||||
})
|
||||
|
||||
it('still flashes a rest that expires normally while the app stays visible', () => {
|
||||
useStore.setState({ S: { ...useStore.getState().S, timerFlash: true } })
|
||||
useUI.getState().startRest(1)
|
||||
vi.advanceTimersByTime(1000)
|
||||
expect(useUI.getState().timerFlashId).toBe(1)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user