Files
workouts/Shared/Connectivity/LiveProgress.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

51 lines
2.0 KiB
Swift

//
// LiveProgress.swift
// Workouts (Shared)
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import Foundation
/// Which page of the Ready Work Rest Finish run flow the driving device is on.
enum LiveRunPhase: String, Sendable, Equatable {
case ready, work, rest, finish
}
/// An *ephemeral* "live run" frame broadcast by the device actively driving an exercise so a
/// propped-up second device can mirror the run flow in real time.
///
/// This is deliberately **not** the durable `WorkoutDocument` sync. That path writes a file to
/// iCloud Drive at set-completion granularity; this one is a throwaway snapshot of *where in
/// the flow* the driver is, sent on every phase transition and never persisted (no SwiftData,
/// no iCloud).
///
/// Timers ride as wall-clock anchors, not a streamed countdown: the mirror renders the same
/// SwiftUI timer text off `phaseStart` / `phaseEnd`, so both devices count independently and
/// stay in lockstep without ticking over the wire. Paired-device clock skew is sub-second, so
/// the two displays agree in practice. A count-down phase (rest, timed work, the finish
/// auto-Done) carries `phaseEnd`; a rep-based work set counts *up* and leaves it `nil`.
struct LiveProgress: Sendable, Equatable {
var workoutID: String
var logID: String
var exerciseName: String
var phase: LiveRunPhase
/// 0-based set this frame pertains to: the set being worked (`work`/`finish`), or the set
/// just completed that this rest follows (`rest`). Drives the header and the progress dots.
var setIndex: Int
var setCount: Int
/// Footer line under the timer, e.g. "8 reps" or "30 sec".
var detail: String
/// Wall-clock anchor for the phase's timer. Work counts up from here; count-down phases
/// run from here to `phaseEnd`.
var phaseStart: Date
/// End anchor for count-down phases; `nil` for a rep-based work set (which counts up).
var phaseEnd: Date?
/// Monotonic per-run sequence, so the mirror can drop a stale / out-of-order delivery.
var version: Int
}