Park the Watch run while iPhone edits an exercise or split
Publish an exclusive-edit lock (editingWorkoutID / editingSplitID) in the phone→watch application context. While the phone has a workout's exercise (ExerciseView) or a split (SplitDetailView) open in an editor, the watch pops out of that run, blocks re-entry, and shows it as "Editing on iPhone" — so the two devices never drive the same run at once and the watch can't clobber the phone's edit with a stale optimistic write. The lock clears when the editor closes; absent keys in the latest-wins context mean "not editing". Claude-Session: https://claude.ai/code/session_01SCv7zvGFcKy47KSTnTLxRe
This commit is contained in:
@@ -14,6 +14,14 @@ final class PhoneConnectivityBridge: NSObject {
|
||||
private let syncEngine: SyncEngine
|
||||
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?
|
||||
|
||||
private var context: ModelContext { container.mainContext }
|
||||
|
||||
init(container: ModelContainer, syncEngine: SyncEngine) {
|
||||
@@ -48,11 +56,30 @@ final class PhoneConnectivityBridge: NSObject {
|
||||
splits: splits.map(SplitDocument.init(from:)),
|
||||
workouts: workouts.map(WorkoutDocument.init(from:)),
|
||||
restSeconds: restSeconds,
|
||||
doneCountdownSeconds: doneCountdownSeconds
|
||||
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()
|
||||
}
|
||||
|
||||
/// 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]) {
|
||||
|
||||
@@ -12,6 +12,7 @@ import SwiftData
|
||||
|
||||
struct SplitDetailView: View {
|
||||
@Environment(SyncEngine.self) private var sync
|
||||
@Environment(AppServices.self) private var services
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
var split: Split
|
||||
@@ -109,6 +110,11 @@ struct SplitDetailView: View {
|
||||
} message: { item in
|
||||
Text("Remove \"\(item.name)\" from this split?")
|
||||
}
|
||||
// Editing this split (or any of its exercises, all reached from here) parks any
|
||||
// active watch run sourced from it — matched by splitID — so the watch can't keep
|
||||
// performing an exercise whose plan we're reconfiguring.
|
||||
.onAppear { services.watchBridge.setEditingSplit(split.id) }
|
||||
.onDisappear { services.watchBridge.setEditingSplit(nil) }
|
||||
}
|
||||
|
||||
private func moveExercises(from source: IndexSet, to destination: Int) {
|
||||
|
||||
@@ -14,6 +14,7 @@ import UIKit
|
||||
|
||||
struct ExerciseView: View {
|
||||
@Environment(SyncEngine.self) private var sync
|
||||
@Environment(AppServices.self) private var services
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
let workout: Workout
|
||||
@@ -69,9 +70,12 @@ struct ExerciseView: View {
|
||||
progress = log?.currentStateIndex ?? 0
|
||||
// Keep the screen lit while logging sets — a mid-workout sleep is annoying.
|
||||
UIApplication.shared.isIdleTimerDisabled = true
|
||||
// Take over this run: the watch parks and locks it while we're editing here.
|
||||
services.watchBridge.setEditingWorkout(workout.id)
|
||||
}
|
||||
.onDisappear {
|
||||
UIApplication.shared.isIdleTimerDisabled = false
|
||||
services.watchBridge.setEditingWorkout(nil)
|
||||
}
|
||||
// Reflect external changes (e.g. a set completed on the watch) live. Each edit
|
||||
// rewrites the whole workout file, so the cache always holds the latest — pulling
|
||||
|
||||
Reference in New Issue
Block a user