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:
@@ -23,6 +23,11 @@ struct ActiveWorkoutGateView: View {
|
||||
@Query(sort: \Workout.start, order: .reverse) private var workouts: [Workout]
|
||||
@Query private var splits: [Split]
|
||||
|
||||
/// Navigated workouts (depth 1). Held here — rather than relying on implicit
|
||||
/// `NavigationLink` destinations — so we can pop back to the gate the moment the phone
|
||||
/// takes over editing the run we're inside.
|
||||
@State private var path: [ActiveWorkoutRoute] = []
|
||||
|
||||
private var activeWorkouts: [Workout] {
|
||||
workouts.filter { $0.status == .inProgress || $0.status == .notStarted }
|
||||
}
|
||||
@@ -32,24 +37,36 @@ struct ActiveWorkoutGateView: View {
|
||||
return splits.first { $0.id == id }
|
||||
}
|
||||
|
||||
/// True while the phone has this workout's exercise — or the split it came from — open
|
||||
/// in an editor. The watch parks such a run and blocks re-entry so the phone owns the
|
||||
/// edit and the watch can't forward a stale optimistic write over it.
|
||||
private func isLockedForEditing(_ workout: Workout) -> Bool {
|
||||
if let id = bridge.editingWorkoutID, id == workout.id { return true }
|
||||
if let splitID = bridge.editingSplitID, splitID == workout.splitID { return true }
|
||||
return false
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
NavigationStack(path: $path) {
|
||||
Group {
|
||||
if activeWorkouts.isEmpty {
|
||||
emptyState
|
||||
} else {
|
||||
List {
|
||||
ForEach(activeWorkouts) { workout in
|
||||
NavigationLink {
|
||||
WorkoutLogListView(workout: workout)
|
||||
} label: {
|
||||
ActiveWorkoutRow(workout: workout, split: split(for: workout))
|
||||
}
|
||||
row(for: workout)
|
||||
}
|
||||
}
|
||||
.navigationTitle("In Progress")
|
||||
}
|
||||
}
|
||||
.navigationDestination(for: ActiveWorkoutRoute.self) { route in
|
||||
// Resolve from the full set (not just active) so a run that finishes while
|
||||
// you're inside it still renders rather than blanking.
|
||||
if let workout = workouts.first(where: { $0.id == route.workoutID }) {
|
||||
WorkoutLogListView(workout: workout)
|
||||
}
|
||||
}
|
||||
}
|
||||
.task {
|
||||
// Nothing to run yet — pull fresh state in case the phone just started one.
|
||||
@@ -60,6 +77,28 @@ struct ActiveWorkoutGateView: View {
|
||||
// was keeping the launched app alive.
|
||||
if noActiveWorkouts { sessionManager.end() }
|
||||
}
|
||||
// The phone just entered (or left) an editor — if we're inside the now-locked run,
|
||||
// pop back to the gate so re-entry rebuilds a fresh working copy.
|
||||
.onChange(of: bridge.editingWorkoutID) { _, _ in popIfNavigatedRunLocked() }
|
||||
.onChange(of: bridge.editingSplitID) { _, _ in popIfNavigatedRunLocked() }
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private func row(for workout: Workout) -> some View {
|
||||
if isLockedForEditing(workout) {
|
||||
ActiveWorkoutRow(workout: workout, split: split(for: workout), editingOnPhone: true)
|
||||
} else {
|
||||
NavigationLink(value: ActiveWorkoutRoute(workoutID: workout.id)) {
|
||||
ActiveWorkoutRow(workout: workout, split: split(for: workout))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// If the run we're currently navigated into has become locked, pop to the gate.
|
||||
private func popIfNavigatedRunLocked() {
|
||||
guard let route = path.last,
|
||||
let workout = workouts.first(where: { $0.id == route.workoutID }) else { return }
|
||||
if isLockedForEditing(workout) { path.removeAll() }
|
||||
}
|
||||
|
||||
private var emptyState: some View {
|
||||
@@ -82,13 +121,15 @@ struct ActiveWorkoutGateView: View {
|
||||
private struct ActiveWorkoutRow: View {
|
||||
let workout: Workout
|
||||
let split: Split?
|
||||
/// When true, the phone is editing this run — render it dimmed and non-tappable.
|
||||
var editingOnPhone: Bool = false
|
||||
|
||||
private var logs: [WorkoutLog] { workout.logsArray }
|
||||
private var doneCount: Int { logs.filter { $0.status == .completed }.count }
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 10) {
|
||||
Image(systemName: split?.systemImage ?? "figure.strengthtraining.traditional")
|
||||
Image(systemName: editingOnPhone ? "pencil" : (split?.systemImage ?? "figure.strengthtraining.traditional"))
|
||||
.font(.title3)
|
||||
.foregroundStyle(split.map { Color.color(from: $0.color) } ?? .workTint)
|
||||
.frame(width: 26)
|
||||
@@ -103,11 +144,22 @@ private struct ActiveWorkoutRow: View {
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
.opacity(editingOnPhone ? 0.5 : 1)
|
||||
}
|
||||
|
||||
private var subtitle: String {
|
||||
if editingOnPhone { return "Editing on iPhone…" }
|
||||
guard !logs.isEmpty else { return workout.start.formattedDate() }
|
||||
if doneCount == 0 { return "Not started · \(logs.count) exercises" }
|
||||
return "\(doneCount) of \(logs.count) done"
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Navigation
|
||||
|
||||
/// Stable, hashable handle for a navigated run. Keyed by id (not the `Workout` object) so
|
||||
/// the path survives cache rebuilds, and a distinct type so it never collides with the
|
||||
/// log-id destination inside `WorkoutLogListView`.
|
||||
private struct ActiveWorkoutRoute: Hashable {
|
||||
let workoutID: String
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user