Make the live run two-way: drive from either device
The propped-up iPhone now runs the real ExerciseProgressView for a live watch workout instead of a read-only mirror, and the live-run channel is symmetric — either device can drive the flow and the other follows. Each page transition is classified human / auto / remote: only human transitions (swipe, Start, One More, swipe-back reset) are broadcast and recorded by the actor; auto-advances (rest / timed-work countdown) record locally but aren't sent, since both devices reach them independently off the shared wall-clock anchors; an applied remote frame jumps the page without re-recording or re-broadcasting. That rule is also what stops an echo loop. - PhoneConnectivityBridge gains sendLiveProgress/sendLiveEnded (the missing phone->watch direction); WatchConnectivityBridge receives frames into an observable liveIncoming via a new didReceiveMessage route. Both share one increasing per-run version sequence so the stale-frame guard works across the two devices' counters. - Both ExerciseProgressViews gain an incomingFrame input + applyIncoming (syncing setCount for a remote One More); the iPhone one gains the liveSnapshot/broadcast machinery the watch already had. - New LiveRunCoverView wraps the real driver for the cover (resolves the workout, persists via SyncEngine, wires the live channel + close); ContentView presents it; LiveProgressMirrorView is removed. Claude-Session: https://claude.ai/code/session_01SCv7zvGFcKy47KSTnTLxRe
This commit is contained in:
@@ -23,6 +23,11 @@ final class PhoneConnectivityBridge: NSObject {
|
||||
private(set) var editingWorkoutID: String?
|
||||
private(set) var editingSplitID: String?
|
||||
|
||||
/// Monotonic sequence stamped on each live-run frame we send. Bumped to stay ahead of
|
||||
/// any frame we *receive*, so the two devices share one increasing sequence per run and
|
||||
/// either side can drop a stale / out-of-order delivery (see `LiveProgress.version`).
|
||||
private var liveVersion = 0
|
||||
|
||||
private var context: ModelContext { container.mainContext }
|
||||
|
||||
init(container: ModelContainer, syncEngine: SyncEngine, liveRunState: LiveRunState) {
|
||||
@@ -82,6 +87,34 @@ final class PhoneConnectivityBridge: NSObject {
|
||||
pushAll()
|
||||
}
|
||||
|
||||
// MARK: - Live run mirror (ephemeral; reachable-only)
|
||||
|
||||
/// Broadcast where the run flow currently is, so the watch (if it has this run open) can
|
||||
/// follow it live. Sent over `sendMessage` only when reachable — this is throwaway
|
||||
/// presence, so there's no guaranteed-delivery fallback (a queued frame would be stale on
|
||||
/// arrival). Mirrors the watch's `sendLiveProgress`; only *human* transitions are sent.
|
||||
func sendLiveProgress(_ frame: LiveProgress) {
|
||||
guard let session, session.activationState == .activated, session.isReachable else { return }
|
||||
liveVersion += 1
|
||||
var stamped = frame
|
||||
stamped.version = liveVersion
|
||||
session.sendMessage(WCPayload.encodeLiveProgress(stamped), replyHandler: nil, errorHandler: { _ in })
|
||||
}
|
||||
|
||||
/// Tell the watch we left the run flow (the cover closed / the run finished).
|
||||
func sendLiveEnded(workoutID: String, logID: String) {
|
||||
guard let session, session.activationState == .activated, session.isReachable else { return }
|
||||
session.sendMessage(WCPayload.encodeLiveEnded(workoutID: workoutID, logID: logID),
|
||||
replyHandler: nil, errorHandler: { _ in })
|
||||
}
|
||||
|
||||
/// Apply a frame the watch sent. Catch our send counter up to it first, so the next frame
|
||||
/// we send outranks it and the shared per-run sequence keeps increasing across devices.
|
||||
private func applyIncomingLive(_ frame: LiveProgress) {
|
||||
liveVersion = max(liveVersion, frame.version)
|
||||
liveRunState.apply(frame)
|
||||
}
|
||||
|
||||
/// Parse the (non-Sendable) WC dictionary in the nonisolated delegate context,
|
||||
/// then hop to the MainActor with only Sendable values.
|
||||
nonisolated private func route(_ dict: [String: Any]) {
|
||||
@@ -94,7 +127,7 @@ final class PhoneConnectivityBridge: NSObject {
|
||||
}
|
||||
case WCPayload.liveProgressType:
|
||||
if let frame = WCPayload.decodeLiveProgress(dict) {
|
||||
Task { @MainActor in self.liveRunState.apply(frame) }
|
||||
Task { @MainActor in self.applyIncomingLive(frame) }
|
||||
}
|
||||
case WCPayload.liveEndedType:
|
||||
if let logID = dict[WCPayload.lpLogIDKey] as? String {
|
||||
|
||||
Reference in New Issue
Block a user