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.
54 lines
2.0 KiB
Swift
54 lines
2.0 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 run currently open in a non-cover driver on *this* device (its in-list/navigated
|
|
/// `ExerciseProgressView`). When an incoming frame is for that run, the device follows it
|
|
/// inline in that driver, so the mirror cover doesn't stack on top of a run already open.
|
|
var navigatedRunID: String?
|
|
|
|
/// The frame to actually present as a cover: the latest frame, unless the user dismissed it
|
|
/// or already has that run open inline.
|
|
var presentable: LiveProgress? {
|
|
guard let c = current, c.logID != mutedLogID, c.logID != navigatedRunID else { return nil }
|
|
return c
|
|
}
|
|
|
|
/// Apply an incoming frame, dropping a stale (or redelivered) one for the same run.
|
|
func apply(_ frame: LiveProgress) {
|
|
if let c = current, c.logID == frame.logID, !frame.isNewer(than: c) { 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
|
|
}
|
|
}
|