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:
@@ -25,6 +25,12 @@ import WatchKit
|
||||
struct ExerciseProgressView: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
/// Live HR straight off the session's builder — drives the target-HR readout when
|
||||
/// this run carries a `targetHeartRate`. Optional so hosts without a session
|
||||
/// manager in the environment (the screenshot root) render without it.
|
||||
@Environment(WorkoutSessionManager.self) private var sessionManager: WorkoutSessionManager?
|
||||
@Environment(\.isLuminanceReduced) private var dimmed
|
||||
|
||||
/// The shared working workout document owned by the parent. We mutate the matching
|
||||
/// log in place and ask the parent to forward each change through the bridge —
|
||||
/// driving the UI from this doc (not the cache) avoids losing rapid edits to the
|
||||
@@ -285,6 +291,9 @@ struct ExerciseProgressView: View {
|
||||
Image(systemName: "xmark")
|
||||
}
|
||||
}
|
||||
ToolbarItem(placement: .topBarTrailing) {
|
||||
heartRateReadout
|
||||
}
|
||||
}
|
||||
.confirmationDialog("Cancel Exercise?", isPresented: $showingCancelConfirm, titleVisibility: .visible) {
|
||||
Button("Cancel Exercise", role: .destructive) { dismiss() }
|
||||
@@ -364,6 +373,26 @@ struct ExerciseProgressView: View {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Live heart rate vs. target
|
||||
|
||||
/// Compact live HR against this run's target (cardio routines only): bpm tinted by
|
||||
/// band with a cue arrow — ease off (down) or push harder (up). Renders nothing
|
||||
/// unless the run carries a target and a sample exists; dims under Always-On like
|
||||
/// the timers.
|
||||
@ViewBuilder
|
||||
private var heartRateReadout: some View {
|
||||
if let target = doc.targetHeartRate, let bpm = sessionManager?.currentHeartRate {
|
||||
let band = HeartRateBand(bpm: bpm, target: target)
|
||||
HStack(spacing: 2) {
|
||||
Text("\(Int(bpm.rounded()))")
|
||||
.monospacedDigit()
|
||||
Image(systemName: band.cueSymbol)
|
||||
}
|
||||
.font(.caption2.weight(.semibold))
|
||||
.foregroundStyle(dimmed ? Color.secondary : band.tint)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Live mirror
|
||||
|
||||
/// Finish the initial-page restore, then either follow an in-progress remote driver or, if
|
||||
@@ -749,6 +778,27 @@ private extension Color {
|
||||
static let restTimer = Color(white: 0.74)
|
||||
}
|
||||
|
||||
// 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 Timer Layout
|
||||
|
||||
/// The live timer a phase shows, described by its wall-clock anchors so `PhaseTimerLayout` can
|
||||
|
||||
Reference in New Issue
Block a user