Save workouts to Apple Health with live watch metrics and a post-workout summary
Watch sessions now run an HKLiveWorkoutBuilder: heart rate and active energy are collected live (shown in an in-workout HUD), saved to Health as a real HKWorkout on completion, and carried back to the phone as WorkoutMetrics on the workout document. Phone-only workouts get an estimated Health workout (MET x bodyweight x duration) after a 10s debounce that a late-arriving watch session cancels; a launch sweep backfills workouts completed within the last day. Dedupe is keyed on metrics.healthKitWorkoutUUID plus the metrics source. Splits gain an activity type (strength, functional, HIIT, core, cardio, cycling) that categorizes the Health workout and picks the MET value. A post-workout summary sheet (duration, calories, avg/max HR, HR zones, total volume) fills in live and is also shown on completed workouts. Weights can now display in lb or kg (display-only relabel), synced to the watch over the existing application context. WorkoutDocument schema 1->2 (metrics are irreplaceable, so older apps must quarantine rather than strip them); cache schema 2 rebuilds the SwiftData store with the new metric columns. Deleting a workout in the app intentionally leaves its Health record in place.
This commit is contained in:
@@ -10,6 +10,7 @@ import SwiftData
|
||||
|
||||
struct WorkoutLogListView: View {
|
||||
@Environment(WatchConnectivityBridge.self) private var bridge
|
||||
@Environment(WorkoutSessionManager.self) private var sessionManager
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
|
||||
/// Working copy of the workout. We drive the UI from this and mutate it on
|
||||
@@ -48,6 +49,10 @@ struct WorkoutLogListView: View {
|
||||
|
||||
var body: some View {
|
||||
List {
|
||||
if sessionManager.isRunning {
|
||||
Section { LiveMetricsHUD() }
|
||||
}
|
||||
|
||||
Section(header: Text(label)) {
|
||||
ForEach(sortedLogs) { log in
|
||||
Button {
|
||||
@@ -141,10 +146,41 @@ struct WorkoutLogListView: View {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Live Metrics HUD
|
||||
|
||||
/// Compact live readout (heart rate + active calories) shown while a watch session is
|
||||
/// running. Reads the session manager from the environment itself so its per-second
|
||||
/// updates re-render only this row, not the editable log list above it. Renders
|
||||
/// nothing until the first sample arrives, and dims under Always-On like the timers.
|
||||
private struct LiveMetricsHUD: View {
|
||||
@Environment(WorkoutSessionManager.self) private var sessionManager
|
||||
@Environment(\.isLuminanceReduced) private var dimmed
|
||||
|
||||
var body: some View {
|
||||
let heartRate = sessionManager.currentHeartRate
|
||||
let energy = sessionManager.currentActiveEnergyKcal
|
||||
if heartRate != nil || energy != nil {
|
||||
HStack(spacing: 16) {
|
||||
if let heartRate {
|
||||
Label("\(Int(heartRate.rounded()))", systemImage: "heart.fill")
|
||||
.foregroundStyle(dimmed ? Color.secondary : Color.red)
|
||||
}
|
||||
if let energy {
|
||||
Label("\(Int(energy.rounded())) cal", systemImage: "flame.fill")
|
||||
.foregroundStyle(dimmed ? Color.secondary : Color.orange)
|
||||
}
|
||||
}
|
||||
.font(.caption.monospacedDigit())
|
||||
.frame(maxWidth: .infinity)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Workout Log Row Label
|
||||
|
||||
struct WorkoutLogRowLabel: View {
|
||||
let log: WorkoutLogDocument
|
||||
@AppStorage("weightUnit") private var weightUnit: WeightUnit = .lb
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
@@ -207,7 +243,7 @@ struct WorkoutLogRowLabel: View {
|
||||
return "\(log.sets) × \(secs) sec"
|
||||
}
|
||||
} else {
|
||||
return "\(log.sets) × \(log.reps) × \(log.weight) lbs"
|
||||
return "\(log.sets) × \(log.reps) × \(weightUnit.format(log.weight))"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -219,6 +255,7 @@ struct ExercisePickerView: View {
|
||||
|
||||
let exercises: [Exercise]
|
||||
let onSelect: (Exercise) -> Void
|
||||
@AppStorage("weightUnit") private var weightUnit: WeightUnit = .lb
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
@@ -266,7 +303,7 @@ struct ExercisePickerView: View {
|
||||
return "\(exercise.sets) × \(secs) sec"
|
||||
}
|
||||
} else {
|
||||
return "\(exercise.sets) × \(exercise.reps) × \(exercise.weight) lbs"
|
||||
return "\(exercise.sets) × \(exercise.reps) × \(weightUnit.format(exercise.weight))"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user