Files
workouts/Workouts/Connectivity/LiveRunState.swift
T
rzen a16e8ec270 Mirror a live Apple Watch run on a propped-up iPhone
Add an ephemeral live-run presence channel (separate from the durable
iCloud progress sync) so a propped-up iPhone can mirror the Watch's
Ready → work/rest → Finish flow in real time as the user swipes.

Watch drives, phone mirrors (read-only), so there's no echo loop:
- Watch's ExerciseProgressView broadcasts a LiveProgress frame on every
  phase transition (and an ended signal on leave) via sendMessage,
  reachable-only — throwaway presence, never written to iCloud.
- Timers ride as wall-clock anchors (Date kept native in the WC dict to
  preserve sub-second precision), so both devices count independently
  off shared start times and stay in lockstep without streaming ticks.
- Phone holds a transient LiveRunState; ContentView auto-presents a
  read-only LiveProgressMirrorView full-screen cover while a run is live.

Claude-Session: https://claude.ai/code/session_01SCv7zvGFcKy47KSTnTLxRe
2026-06-20 21:08:32 -04:00

48 lines
1.6 KiB
Swift

//
// LiveRunState.swift
// Workouts
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import Foundation
import Observation
/// Phone-side holder for the *ephemeral* live-run frames the watch broadcasts while it drives
/// an exercise (see `LiveProgress`). The mirror UI observes this; nothing here is persisted.
///
/// Phase 1 is watch-drives / phone-mirrors, so this is read-only state fed by the connectivity
/// bridge the phone never sends back, which is why there's no echo loop to guard against yet.
@Observable
@MainActor
final class LiveRunState {
/// The latest frame from the driving device, or `nil` when no run is being mirrored.
private(set) var current: LiveProgress?
/// A log the user manually closed the mirror for; suppressed until that run ends.
private var mutedLogID: String?
/// The frame to actually present, honoring a manual dismiss.
var presentable: LiveProgress? {
guard let c = current, c.logID != mutedLogID else { return nil }
return c
}
/// Apply an incoming frame, dropping a stale one for the same run.
func apply(_ frame: LiveProgress) {
if let c = current, c.logID == frame.logID, frame.version < c.version { return }
current = frame
}
/// The driver left the run (cancel / done / navigated away) stop mirroring it.
func end(logID: String) {
if current?.logID == logID { current = nil }
if mutedLogID == logID { mutedLogID = nil }
}
/// The user dismissed the mirror; don't re-present this run until it ends.
func mute() {
mutedLogID = current?.logID
}
}