Files
workouts/Workouts Watch App/WorkoutsApp.swift
T
rzen 04523c875a End the watch workout session from durable state, not a view
The HKWorkoutSession that keeps the watch app foregrounded was ended only
by ActiveWorkoutGateView's onChange(of: activeWorkouts) — a view-level side
effect. When a run ended from the phone while the watch app was
backgrounded (kept alive only by the session) or torn down and rebuilt with
an already-empty list, that onChange never fired: the session leaked (the
app kept re-foregrounding on every wrist raise) and finishAndSave() never
ran, so the HR/energy summary was neither saved to Health nor forwarded.

Move session-end off the view into a long-lived WorkoutSessionCoordinator
owned by WatchAppServices, driven by a new bridge.onWorkoutsChanged
callback fired after every authoritative cache mutation (phone push or the
watch's own optimistic edit). The decision is a pure SessionEndPlanner
seam (mirrors WatchCacheApplier): a running session ends only on a genuine
non-empty -> empty transition of the active set, so the launch race (session
running before the run doc syncs) resolves to .none and never discards a
run we haven't heard about yet. Same move as the live-mirror's
repairFromDurable, one layer down.

Watch-only; no schema or wire change. SessionEndPlannerTests pins the
decision table; the OS-initiated-end path (system ends the session itself)
stays a documented residual in PLAN-watch-session-end.md.
2026-07-09 08:12:06 -04:00

38 lines
903 B
Swift

//
// WorkoutsApp.swift
// Workouts Watch App
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import SwiftUI
import SwiftData
@main
struct WorkoutsWatchApp: App {
@State private var services = WatchAppServices()
@WKApplicationDelegateAdaptor(WatchAppDelegate.self) private var appDelegate
var body: some Scene {
WindowGroup {
#if DEBUG
if ScreenshotSeed.isActive {
WatchScreenshotRoot(services: services)
} else {
root
}
#else
root
#endif
}
}
private var root: some View {
ContentView()
.environment(services.bridge)
.environment(appDelegate.sessionManager)
.modelContainer(services.container)
.task { services.bootstrap(sessionManager: appDelegate.sessionManager) }
}
}