7400094eda
Watch-side follow-through for the End Workout flow: - The phone now pushes an authoritative set (in-progress, not-started, and completed within 24h) instead of the 25 most-recent workouts, and the watch prunes any workout absent from it. So a Discard/Delete (or a completed run aging out) drops off the watch, empties its active list, and ends the HKWorkoutSession — fixing the persistent wrist-raise re-foregrounding. The watch never originates a workout, so pruning can't lose local data; the 24h grace keeps a just-finished run on screen. The gate pops if the run you're viewing is pruned. UX tweaks: - The in-workout ⋯ is now a pull-down Menu (Add Exercise / End Workout) rather than an action sheet. - Starting a split while another workout is still active now prompts to end the current one(s) — keeping their progress — or run in parallel. Wired into both start paths (the split picker and "Start This Split"), via a shared WorkoutDocument.endKeepingProgress() helper. Claude-Session: https://claude.ai/code/session_01SCv7zvGFcKy47KSTnTLxRe
173 lines
6.9 KiB
Swift
173 lines
6.9 KiB
Swift
//
|
|
// ActiveWorkoutGateView.swift
|
|
// Workouts Watch App
|
|
//
|
|
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
|
|
//
|
|
|
|
import SwiftUI
|
|
import SwiftData
|
|
|
|
/// Root of the watch app. The watch only runs the workouts that are currently
|
|
/// active on the phone — there's no history browsing here. An "active" workout is a
|
|
/// cached one that isn't finished (`notStarted` or `inProgress`); a workout is
|
|
/// created on the phone as `notStarted` the moment a split is picked, and flips to
|
|
/// `completed` once every exercise is done, at which point it drops off this list.
|
|
///
|
|
/// The root shows every active workout (most-recent first); picking one opens its
|
|
/// exercise list.
|
|
struct ActiveWorkoutGateView: View {
|
|
@Environment(WatchConnectivityBridge.self) private var bridge
|
|
@Environment(WorkoutSessionManager.self) private var sessionManager
|
|
|
|
@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 }
|
|
}
|
|
|
|
private func split(for workout: Workout) -> Split? {
|
|
guard let id = workout.splitID else { return nil }
|
|
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(path: $path) {
|
|
Group {
|
|
if activeWorkouts.isEmpty {
|
|
emptyState
|
|
} else {
|
|
List {
|
|
ForEach(activeWorkouts) { workout in
|
|
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.
|
|
if activeWorkouts.isEmpty { bridge.requestSync() }
|
|
}
|
|
.onChange(of: activeWorkouts.isEmpty) { _, noActiveWorkouts in
|
|
// Everything finished (or was cleared) — release the HealthKit session that
|
|
// 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. Also pop if the run
|
|
// we're inside was pruned (discarded/deleted on the phone, or aged out of the push).
|
|
.onChange(of: bridge.editingWorkoutID) { _, _ in popIfNavigatedRunUnavailable() }
|
|
.onChange(of: bridge.editingSplitID) { _, _ in popIfNavigatedRunUnavailable() }
|
|
.onChange(of: workouts.map(\.id)) { _, _ in popIfNavigatedRunUnavailable() }
|
|
}
|
|
|
|
@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 is no longer available — pruned from the
|
|
/// cache (discarded/deleted on the phone, or aged out of the pushed set), or locked
|
|
/// because the phone took over editing it — pop back to the gate.
|
|
private func popIfNavigatedRunUnavailable() {
|
|
guard let route = path.last else { return }
|
|
guard let workout = workouts.first(where: { $0.id == route.workoutID }) else {
|
|
path.removeAll()
|
|
return
|
|
}
|
|
if isLockedForEditing(workout) { path.removeAll() }
|
|
}
|
|
|
|
private var emptyState: some View {
|
|
ContentUnavailableView {
|
|
Label("Start a Workout", systemImage: "iphone")
|
|
} description: {
|
|
Text("Begin a workout on your iPhone to run it here.")
|
|
} actions: {
|
|
Button {
|
|
bridge.requestSync()
|
|
} label: {
|
|
Label("Refresh", systemImage: "arrow.triangle.2.circlepath")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Active Workout Row
|
|
|
|
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: editingOnPhone ? "pencil" : (split?.systemImage ?? "figure.strengthtraining.traditional"))
|
|
.font(.title3)
|
|
.foregroundStyle(split.map { Color.color(from: $0.color) } ?? .workTint)
|
|
.frame(width: 26)
|
|
|
|
VStack(alignment: .leading, spacing: 2) {
|
|
Text(workout.splitName ?? Split.unnamed)
|
|
.font(.headline)
|
|
.lineLimit(1)
|
|
|
|
Text(subtitle)
|
|
.font(.caption2)
|
|
.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
|
|
}
|