/* VitFit timer-display.js — used by timer-display.html */ 'use strict'; const TimerDisplay = (() => { let _socket = null; let _sessionId = null; let _localFallbackRaf = null; let _localEndTime = null; let _keepAliveEl = null; const containerEl = () => document.getElementById('timer-container'); const timerEl = () => document.getElementById('timer-countdown'); const totalEl = () => document.getElementById('timer-total'); const phaseEl = () => document.getElementById('timer-phase-label'); const lapEl = () => document.getElementById('timer-lap'); const exerciseEl = () => document.getElementById('timer-exercise'); const setEl = () => document.getElementById('timer-set'); const overlayEl = () => document.getElementById('timer-overlay'); const overlayTitleEl = () => document.getElementById('timer-overlay-title'); const offlineBanner = () => document.getElementById('display-offline-banner'); const localIndicator = () => document.getElementById('display-local-indicator'); function init(sessionId) { _sessionId = sessionId; _socket = new SocketClient(); _socket.onDisconnect(() => { offlineBanner()?.classList.add('visible'); if (_localEndTime) startLocalFallback(_localEndTime); }); _socket.onReconnect(async () => { offlineBanner()?.classList.remove('visible'); localIndicator()?.classList.remove('visible'); stopLocalFallback(); // Re-sync state from server const res = await Api.get(`/api/v1/timers/session/${_sessionId}`).catch(() => null); if (res?.ok) { const { data } = await res.json(); if (data) handleState(data); } }); _socket.on('timer:state', handleState); _socket.on('timer:tick', handleTick); _socket.on('timer:phase-change', handlePhaseChange); _socket.on('timer:complete', handleComplete); _socket.on('timer:stopped', handleStopped); _socket.connect(); _socket.joinRoom(sessionId); // AirPlay keep-alive _keepAliveEl = document.createElement('div'); _keepAliveEl.className = 'keep-alive'; document.body.appendChild(_keepAliveEl); // Cleanup on unload window.addEventListener('beforeunload', () => { _socket.leaveRoom(sessionId); _socket.destroy(); stopLocalFallback(); }); } function handleState(state) { _localEndTime = state.phaseEndTime; // ms epoch from server setPhaseColor(state.phase, state.phaseRemainingMs); updateCounters(state); if (timerEl()) timerEl().textContent = formatTime(state.phaseRemainingMs); if (totalEl()) totalEl().textContent = 'Total ' + formatTime(state.totalRemainingMs); } function handleTick(data) { _localEndTime = data.phaseEndTime || _localEndTime; setPhaseColor(data.phase, data.phaseRemainingMs); if (timerEl()) timerEl().textContent = formatTime(data.phaseRemainingMs); if (totalEl()) totalEl().textContent = 'Total ' + formatTime(data.totalRemainingMs); updateCounters(data); stopLocalFallback(); } function handlePhaseChange(data) { setPhaseColor(data.phase, data.phaseRemainingMs); updateCounters(data); _localEndTime = data.phaseEndTime; } function handleComplete() { stopLocalFallback(); if (timerEl()) timerEl().textContent = '00:00'; if (overlayTitleEl()) overlayTitleEl().textContent = 'DONE'; overlayEl()?.classList.add('visible'); containerEl()?.classList.add('phase-complete'); } function handleStopped() { stopLocalFallback(); if (overlayTitleEl()) overlayTitleEl().textContent = 'STOPPED'; overlayEl()?.classList.add('visible'); } function startLocalFallback(endTimeMs) { stopLocalFallback(); localIndicator()?.classList.add('visible'); function tick() { const remaining = Math.max(0, endTimeMs - Date.now()); if (timerEl()) timerEl().textContent = formatTime(remaining); if (remaining > 0) { _localFallbackRaf = requestAnimationFrame(tick); } else { localIndicator()?.classList.remove('visible'); } } _localFallbackRaf = requestAnimationFrame(tick); } function stopLocalFallback() { if (_localFallbackRaf) { cancelAnimationFrame(_localFallbackRaf); _localFallbackRaf = null; } localIndicator()?.classList.remove('visible'); } function updateCounters(state) { if (phaseEl()) phaseEl().textContent = (state.phase || '').toUpperCase(); if (lapEl()) lapEl().textContent = `LAP ${state.currentLap}/${state.lapCount}`; if (exerciseEl()) exerciseEl().textContent = `${state.currentExercise}/${state.exerciseCount} EX`; if (setEl()) setEl().textContent = `SET ${state.currentSet}/${state.setCount}`; } function setPhaseColor(phase, remainingMs) { const container = containerEl(); if (!container) return; container.className = 'display-container'; const isWarning = typeof remainingMs === 'number' && remainingMs <= 10000 && remainingMs > 0; if (isWarning) { container.classList.add('phase-warning'); } else { container.classList.add(`phase-${phase || 'idle'}`); } } function formatTime(ms) { if (typeof ms !== 'number' || isNaN(ms)) return '--:--'; const totalSec = Math.max(0, Math.ceil(ms / 1000)); const min = String(Math.floor(totalSec / 60)).padStart(2, '0'); const sec = String(totalSec % 60).padStart(2, '0'); return `${min}:${sec}`; } function formatHMS(ms) { const totalSec = Math.max(0, Math.ceil(ms / 1000)); const h = Math.floor(totalSec / 3600); const m = Math.floor((totalSec % 3600) / 60); const s = totalSec % 60; return `${h}:${String(m).padStart(2,'0')}:${String(s).padStart(2,'0')}`; } return { init, handleState, handleTick, handlePhaseChange, handleComplete, handleStopped, startLocalFallback, stopLocalFallback, formatTime, formatHMS, setPhaseColor }; })();