Add per-routine target heart rate with live in-run indicator

Endurance routines (HIIT, cardio, cycling) can carry an optional target bpm
(RoutineDocument.targetHeartRate, not schema-bumped — same preference-field
rationale as restSeconds/autoAdvance), snapshotted onto the WorkoutDocument at
plan time like the other pacing fields.

During a run, the watch streams its live HR sample to the phone over a new
best-effort liveHeartRate message — deliberately outside the LiveProgress
machinery (no version bump, no staging/retry; a gauge, not a record), throttled
to changed-bpm-or-10s in the watch bridge. LiveRunState holds the sample with a
30s staleness auto-clear so a dead stream never shows a frozen number.

Both run screens show the reading only when the run carries a target: the
phone's ExerciseProgressView as a top pill, the watch's in the top-trailing
toolbar slot, each tinted by a shared ±5 bpm HeartRateBand with an arrow cue to
push harder (low) or ease off (high) — e.g. dialing in a treadmill incline to
hold a steady effort.

Claude-Session: https://claude.ai/code/session_01Y7ZhkCYWNiTSAFhFCGnJ8n
This commit is contained in:
2026-07-11 19:51:44 -04:00
parent b06a44eb40
commit 8854ad59d5
17 changed files with 283 additions and 10 deletions
@@ -32,6 +32,8 @@ struct RoutineAddEditView: View {
@State private var autoAdvance: Bool = false
@State private var restOverrideEnabled: Bool = false
@State private var restSecondsValue: Int = 45
@State private var targetHREnabled: Bool = false
@State private var targetHRValue: Int = 135
@State private var showingIconPicker: Bool = false
@State private var showingDeleteConfirmation: Bool = false
@@ -48,6 +50,8 @@ struct RoutineAddEditView: View {
_autoAdvance = State(initialValue: routine.autoAdvance ?? false)
_restOverrideEnabled = State(initialValue: routine.restSeconds != nil)
_restSecondsValue = State(initialValue: routine.restSeconds ?? 45)
_targetHREnabled = State(initialValue: routine.targetHeartRate != nil)
_targetHRValue = State(initialValue: routine.targetHeartRate ?? 135)
}
}
@@ -136,6 +140,26 @@ struct RoutineAddEditView: View {
Text("Auto-Advance flows from one exercise to the next with a rest between, so you can run the whole routine hands-free. Custom Rest Time sets this routine's rest — used between sets, and between exercises when auto-advancing; otherwise the Settings default applies.")
}
// Only endurance types get a target a strength set's HR swings by design.
if activityType.supportsHeartRateTarget {
Section {
Toggle("Target Heart Rate", isOn: $targetHREnabled)
if targetHREnabled {
Stepper(value: $targetHRValue, in: 80...200, step: 5) {
HStack {
Text("Target")
Spacer()
Text("\(targetHRValue) bpm").foregroundColor(.secondary)
}
}
}
} header: {
Text("Heart Rate")
} footer: {
Text("During a run, the exercise screen shows your live heart rate from the watch with a cue to ease off or push harder — useful for holding a steady effort, like dialing in a treadmill incline.")
}
}
if let routine = routine {
Section(header: Text("Exercises")) {
NavigationLink {
@@ -205,6 +229,9 @@ struct RoutineAddEditView: View {
doc.activityType = activityType.rawValue
doc.restSeconds = restOverrideEnabled ? restSecondsValue : nil
doc.autoAdvance = autoAdvance ? true : nil
// Cleared when the type no longer supports a target (the section hides).
doc.targetHeartRate = activityType.supportsHeartRateTarget && targetHREnabled
? targetHRValue : nil
doc.updatedAt = Date()
Task { await sync.save(routine: doc) }
} else {
@@ -222,7 +249,9 @@ struct RoutineAddEditView: View {
exercises: [],
activityType: activityType.rawValue,
restSeconds: restOverrideEnabled ? restSecondsValue : nil,
autoAdvance: autoAdvance ? true : nil
autoAdvance: autoAdvance ? true : nil,
targetHeartRate: activityType.supportsHeartRateTarget && targetHREnabled
? targetHRValue : nil
)
Task { await sync.save(routine: doc) }
}