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:
@@ -23,6 +23,11 @@ struct SplitDocument: Codable, Sendable, Equatable, Identifiable {
|
||||
var createdAt: Date
|
||||
var updatedAt: Date
|
||||
var exercises: [ExerciseDocument]
|
||||
/// Raw `WorkoutActivityType`; nil → traditional strength training. Optional and
|
||||
/// deliberately NOT schema-bumped: it's a minor categorization preference, so an
|
||||
/// older app dropping it on rewrite (reverting to the default) is preferable to
|
||||
/// quarantining the user's whole routine.
|
||||
var activityType: Int?
|
||||
|
||||
static let currentSchema = 1
|
||||
|
||||
@@ -55,8 +60,16 @@ struct WorkoutDocument: Codable, Sendable, Equatable, Identifiable {
|
||||
var createdAt: Date
|
||||
var updatedAt: Date
|
||||
var logs: [WorkoutLogDocument]
|
||||
/// Health metrics captured for a finished workout (watch sensors) or estimated
|
||||
/// by the phone. Nil until the workout completes and a writer fills it in. The
|
||||
/// presence of `metrics.healthKitWorkoutUUID` is the dedupe signal that keeps the
|
||||
/// watch and the phone from both writing the same workout to Health.
|
||||
var metrics: WorkoutMetrics?
|
||||
|
||||
static let currentSchema = 1
|
||||
// Bumped 1→2 when `metrics` was added: the captured HR/calorie data is
|
||||
// irreplaceable, so the forward-gate must quarantine these files on older apps
|
||||
// rather than let them strip `metrics` on rewrite.
|
||||
static let currentSchema = 2
|
||||
|
||||
var relativePath: String { Self.relativePath(id: id, start: start) }
|
||||
|
||||
@@ -125,6 +138,24 @@ struct WorkoutLogDocument: Codable, Sendable, Equatable, Identifiable {
|
||||
var date: Date
|
||||
}
|
||||
|
||||
// MARK: - Workout health metrics
|
||||
|
||||
/// Health metrics for one finished workout. Embedded in `WorkoutDocument` so they
|
||||
/// ride the existing iCloud + Watch-bridge paths with the rest of the workout.
|
||||
/// Every field except `source`/`recordedAt` is optional — a phone estimate carries
|
||||
/// only calories + volume, while a watch session fills in heart rate too.
|
||||
struct WorkoutMetrics: Codable, Sendable, Equatable {
|
||||
var activeEnergyKcal: Double?
|
||||
var avgHeartRate: Double? // bpm
|
||||
var maxHeartRate: Double? // bpm
|
||||
var minHeartRate: Double? // bpm
|
||||
var totalVolume: Double? // Σ sets×reps×weight across weighted logs
|
||||
var hrZoneSeconds: [Double]? // seconds spent in each of 5 HR zones (low→high)
|
||||
var healthKitWorkoutUUID: String?
|
||||
var source: MetricSource
|
||||
var recordedAt: Date
|
||||
}
|
||||
|
||||
// MARK: - Forward-compatibility gate
|
||||
|
||||
/// A file whose `schemaVersion` exceeds the reader's `currentSchema` was written
|
||||
|
||||
Reference in New Issue
Block a user