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
@@ -30,6 +30,10 @@ struct ExerciseProgressView: View {
@Environment(\.dismiss) private var dismiss
@Environment(\.verticalSizeClass) private var verticalSizeClass
/// Live watch-forwarded heart rate (see `LiveRunState.heartRate`) drives the
/// target-HR pill when this run carries a `targetHeartRate`.
@Environment(LiveRunState.self) private var liveRun
/// The shared working workout document owned by the parent list. We mutate the
/// matching log in place and ask the parent to persist each change driving the UI
@@ -391,6 +395,9 @@ struct ExerciseProgressView: View {
.padding(.bottom, 8)
}
}
.overlay(alignment: .top) {
heartRatePill
}
// Bottom half: the looping form-guide figure. Shows the *next* exercise while
// resting between exercises (a live preview of what's coming up), otherwise this
@@ -731,6 +738,33 @@ struct ExerciseProgressView: View {
}
}
// MARK: - Live heart rate vs. target
/// Live heart rate against this run's target (cardio routines only): the bpm the
/// watch is streaming, tinted by band, with a cue to ease off (arrow down) or push
/// harder (arrow up) how you dial in a treadmill incline from a propped phone.
/// Renders nothing unless the run carries a target *and* a fresh sample exists, so
/// it has zero footprint on strength runs or when the watch isn't active.
@ViewBuilder
private var heartRatePill: some View {
if let target = doc.targetHeartRate, let bpm = liveRun.heartRate {
let band = HeartRateBand(bpm: bpm, target: target)
HStack(spacing: 6) {
Image(systemName: "heart.fill")
Text("\(Int(bpm.rounded()))")
.monospacedDigit()
Image(systemName: band.cueSymbol)
}
.font(.subheadline.weight(.semibold))
.foregroundStyle(band.tint)
.padding(.horizontal, 14)
.padding(.vertical, 7)
.background(.quaternary.opacity(0.5), in: Capsule())
.padding(.top, 6)
.animation(.easeInOut(duration: 0.3), value: band)
}
}
// MARK: - Adjust the last set's entry
/// The just-completed set's recorded entry, shown on the rest and finish pages
@@ -1119,6 +1153,27 @@ private enum WorkoutHaptic {
}
}
// MARK: - Heart-rate band presentation
private extension HeartRateBand {
/// The correction cue: below target push harder (up), above ease off (down).
var cueSymbol: String {
switch self {
case .low: "arrow.up"
case .inRange: "checkmark"
case .high: "arrow.down"
}
}
var tint: Color {
switch self {
case .low: .blue
case .inRange: .green
case .high: .orange
}
}
}
// MARK: - Phase Colors
private extension Color {