Files
workouts/Shared/Connectivity/LiveProgress.swift
T
rzen 8cbe078ffd Make the live-run mirror survive lost phone-to-watch frames
Live frames ride sendMessage, which is reachable-only — and phone→watch
reachability drops exactly when the user is swiping on the phone (wrist
down). A frame that failed to send was staged but never retried until a
reachability edge, and a frame lost outright desynced the run until the
next human transition — sometimes forever, since a reconnect could even
re-send the stale staged frame and yank the peer backwards.

Four fixes, symmetric on both bridges and both run screens:

- A send that fails while nominally reachable now retries with a short
  backoff (a few times per staged message) instead of being swallowed.
- Receiving a frame that outranks the staged outbound one drops the
  staged frame, so a reconnect re-send can't move the run backwards;
  a delivery the staged frame outranks is ignored as stale.
- Every staleness comparison now tie-breaks the shared version sequence
  on the frame's wall-clock anchor (LiveProgress.isNewer) — after a
  lost frame both devices can mint the same version, and the later
  human action must win.
- Durable repair: when the absorbed workout doc shows sets completed
  beyond anything the open run screen recorded or followed, it jumps
  forward to the first unfinished set's work page — a lost frame now
  degrades to a briefly-stale page instead of a stuck one.
2026-07-08 18:52:24 -04:00

67 lines
2.9 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
}
extension LiveProgress {
/// Strict "newer than" over the shared per-run sequence, tie-broken by the phase anchor.
/// The tie-break matters because the sequence isn't collision-free: each side catches its
/// counter up only from frames it *receives*, so after a lost frame both devices can mint
/// the same version independently. On a collision the later wall-clock action wins —
/// paired-device clock skew is sub-second, so the anchor orders genuine collisions
/// correctly. Equal version *and* equal anchor is the same frame redelivered: not newer.
func isNewer(thanVersion version: Int, start: Date) -> Bool {
self.version != version ? self.version > version : phaseStart > start
}
func isNewer(than other: LiveProgress) -> Bool {
isNewer(thanVersion: other.version, start: other.phaseStart)
}
}