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:
@@ -34,6 +34,26 @@ final class LiveRunState {
|
||||
return c
|
||||
}
|
||||
|
||||
/// Latest live heart-rate sample forwarded by the watch (bpm), or `nil` when none is
|
||||
/// fresh. Device-level, not run-scoped — it's the wearer's pulse. Auto-clears after
|
||||
/// `hrStaleAfter` without a new sample, so the UI never shows a frozen number after
|
||||
/// the watch stops sending (session ended, unreachable).
|
||||
private(set) var heartRate: Double?
|
||||
|
||||
private var heartRateExpiry: Task<Void, Never>?
|
||||
private static let hrStaleAfter: Duration = .seconds(30)
|
||||
|
||||
/// Apply an incoming heart-rate sample and (re)arm its staleness expiry.
|
||||
func applyHeartRate(_ bpm: Double) {
|
||||
heartRate = bpm
|
||||
heartRateExpiry?.cancel()
|
||||
heartRateExpiry = Task { [weak self] in
|
||||
try? await Task.sleep(for: Self.hrStaleAfter)
|
||||
guard !Task.isCancelled else { return }
|
||||
self?.heartRate = nil
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply an incoming frame, dropping a stale (or redelivered) one for the same run.
|
||||
func apply(_ frame: LiveProgress) {
|
||||
if let c = current, c.logID == frame.logID, !frame.isNewer(than: c) { return }
|
||||
|
||||
@@ -267,6 +267,10 @@ final class PhoneConnectivityBridge: NSObject {
|
||||
if let logID = dict[WCPayload.lpLogIDKey] as? String {
|
||||
Task { @MainActor in self.liveRunState.end(logID: logID) }
|
||||
}
|
||||
case WCPayload.liveHeartRateType:
|
||||
if let sample = WCPayload.decodeLiveHeartRate(dict) {
|
||||
Task { @MainActor in self.liveRunState.applyHeartRate(sample.bpm) }
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user