Files
workouts/Workouts/Connectivity/PhoneConnectivityBridge.swift
T
rzen 8f69497b24 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
2026-06-20 22:11:05 -04:00

167 lines
7.3 KiB
Swift

import Foundation
import SwiftData
import WatchConnectivity
/// Phone side of the iPhoneWatch bridge. The phone owns iCloud Drive; the watch
/// is a thin remote that round-trips through it:
/// Phone Watch: pushes all splits + recent workouts as the latest
/// application context whenever the cache changes (local or remote).
/// Watch Phone: receives an updated `WorkoutDocument` and applies it via the
/// SyncEngine write path (file observer cache push back).
@MainActor
final class PhoneConnectivityBridge: NSObject {
private let container: ModelContainer
private let syncEngine: SyncEngine
private let liveRunState: LiveRunState
private var session: WCSession?
/// Exclusive-edit lock published to the watch. While the phone has a workout's
/// exercise (or a split) open in an editor, the watch parks any matching run and
/// blocks re-entry so the two devices never drive the same run at once. Included in
/// every `pushAll` (the latest-wins context replaces wholesale, so a push that omitted
/// them would clear the lock prematurely).
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) {
self.container = container
self.syncEngine = syncEngine
self.liveRunState = liveRunState
super.init()
}
func activate() {
guard WCSession.isSupported() else { return }
let session = WCSession.default
session.delegate = self
session.activate()
self.session = session
// Push fresh state to the watch whenever the cache changes.
syncEngine.onCacheChanged = { [weak self] in self?.pushAll() }
}
/// Sends the current splits + most-recent workouts to the watch.
func pushAll() {
guard let session, session.activationState == .activated, session.isPaired,
session.isWatchAppInstalled else { return }
let splits = (try? context.fetch(FetchDescriptor<Split>(sortBy: [SortDescriptor(\.order)]))) ?? []
var wDesc = FetchDescriptor<Workout>(sortBy: [SortDescriptor(\.start, order: .reverse)])
wDesc.fetchLimit = 25
let workouts = (try? context.fetch(wDesc)) ?? []
let restSeconds = UserDefaults.standard.object(forKey: WCPayload.restSecondsKey) as? Int ?? 45
let doneCountdownSeconds = UserDefaults.standard.object(forKey: WCPayload.doneCountdownSecondsKey) as? Int ?? 5
let payload = WCPayload.encodeState(
splits: splits.map(SplitDocument.init(from:)),
workouts: workouts.map(WorkoutDocument.init(from:)),
restSeconds: restSeconds,
doneCountdownSeconds: doneCountdownSeconds,
editingWorkoutID: editingWorkoutID,
editingSplitID: editingSplitID
)
try? session.updateApplicationContext(payload)
}
/// Mark (or clear, with `nil`) the workout currently open in a phone exercise editor.
/// The watch parks that run and blocks re-entry until it clears. Pushes immediately so
/// the lock takes effect without waiting on a cache change.
func setEditingWorkout(_ id: String?) {
guard editingWorkoutID != id else { return }
editingWorkoutID = id
pushAll()
}
/// Mark (or clear, with `nil`) the split currently open in a phone editor. The watch
/// parks any active run sourced from that split (matched by `splitID`).
func setEditingSplit(_ id: String?) {
guard editingSplitID != id else { return }
editingSplitID = id
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]) {
switch dict[WCPayload.typeKey] as? String {
case WCPayload.requestSyncType:
Task { @MainActor in self.pushAll() }
case WCPayload.workoutUpdateType:
if let doc = WCPayload.decodeWorkoutUpdate(dict) {
Task { @MainActor in await self.syncEngine.ingestFromWatch(doc) }
}
case WCPayload.liveProgressType:
if let frame = WCPayload.decodeLiveProgress(dict) {
Task { @MainActor in self.applyIncomingLive(frame) }
}
case WCPayload.liveEndedType:
if let logID = dict[WCPayload.lpLogIDKey] as? String {
Task { @MainActor in self.liveRunState.end(logID: logID) }
}
default:
break
}
}
}
// MARK: - WCSessionDelegate
extension PhoneConnectivityBridge: WCSessionDelegate {
nonisolated func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
Task { @MainActor in self.pushAll() }
}
nonisolated func sessionDidBecomeInactive(_ session: WCSession) {}
nonisolated func sessionDidDeactivate(_ session: WCSession) {
session.activate() // reactivate for a switched watch
}
nonisolated func sessionReachabilityDidChange(_ session: WCSession) {
if session.isReachable { Task { @MainActor in self.pushAll() } }
}
nonisolated func session(_ session: WCSession, didReceiveMessage message: [String: Any]) {
route(message)
}
nonisolated func session(_ session: WCSession, didReceiveUserInfo userInfo: [String: Any] = [:]) {
route(userInfo)
}
}