Files
workouts/Workouts/Views/WorkoutLogs/RunFlowView.swift
T
rzen c4fe412308 Close the live-mirror flow hand-off gaps that skipped a timed phase
Field incident (2026-07-17): a Cardio flow run driven from a locked phone
durably completed the 30-min Main Circuit ~5 minutes in, then fought the
watch until manual swipes forced agreement. Second bulletproofing pass
(BULLETPROOFING.md, "2026-07-18 — Live-run mirror & flow hand-off"):

- H4: RunFlowView (both platforms) follows the driver's cross-log
  hand-off frame (forward-only by plan order, runnable target only), and
  a remote completion mid-flow hands off via onAdvance instead of
  dismissing the run (which tore down the Live Activity and stranded the
  flow in the mirror cover).
- H5: wall-clock plausibility gate — a duration exercise never
  auto-completes before startedAt + sets × duration; the flow-terminal
  rest self-repairs to computeResume() and the Finish auto-Done holds
  for a human tap. Human Done taps stay ungated.
- M6: both bridges persist liveVersion as a UserDefaults high-water mark
  so an app relaunch can't get every subsequent frame dropped as stale.
- M7: phone list + mirror cover absorbs gain the watch's strictly-newer
  guard, so a stale cache echo can't regress the working doc.
- Diagnostics: run-flow os_log breadcrumbs (page transitions, applied
  frames, repairs, hand-offs, gate trips) on both drivers.

Open, documented: M5 (no reconciliation signal across a single-set
duration phase), M8 (watch context apply lacks per-log staleness guard),
L5 (spurious pager write-backs read as human swipes).
2026-07-18 10:49:45 -04:00

116 lines
5.5 KiB
Swift

//
// RunFlowView.swift
// Workouts
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//
import SwiftUI
/// Thin coordinator that lets a single pushed run walk across exercises in a flow-mode
/// routine (`autoAdvance`). It owns which log is on screen and swaps it when the running
/// `ExerciseProgressView` finishes an exercise; `.id(currentLogID)` rebuilds the child
/// fresh for each exercise, so the delicate per-exercise run/mirror machinery stays
/// exactly as it is — this only automates the "open the next exercise" step.
///
/// For a non-flow routine nothing ever calls `advance`, so this is transparent: it presents
/// one exercise and behaves identically to hosting `ExerciseProgressView` directly.
struct RunFlowView: View {
@Environment(LiveRunState.self) private var liveRun
@Binding var doc: WorkoutDocument
let startLogID: String
let onChange: () -> Void
let onLive: (LiveProgress) -> Void
/// Send the "stop mirroring this run" signal for a given log. Parameterized by log id
/// because the run's current log advances; the leaving exercise's id is what ends.
let sendLiveEnded: (String) -> Void
let onActivity: (LiveProgress) -> Void
let onActivityEnded: () -> Void
var speechAnnouncer: SpeechAnnouncer? = nil
@State private var currentLogID: String
/// True once we've auto-advanced at least once — the current exercise then skips its
/// Ready page and begins on appear. The first exercise (manual entry) keeps Ready.
@State private var enteredViaFlow = false
/// Set across an exercise hand-off so the leaving exercise's teardown doesn't tear the
/// mirror / Live Activity down — the run is continuing, not ending.
@State private var advancing = false
init(doc: Binding<WorkoutDocument>, startLogID: String, onChange: @escaping () -> Void,
onLive: @escaping (LiveProgress) -> Void, sendLiveEnded: @escaping (String) -> Void,
onActivity: @escaping (LiveProgress) -> Void, onActivityEnded: @escaping () -> Void,
speechAnnouncer: SpeechAnnouncer? = nil) {
self._doc = doc
self.startLogID = startLogID
self.onChange = onChange
self.onLive = onLive
self.sendLiveEnded = sendLiveEnded
self.onActivity = onActivity
self.onActivityEnded = onActivityEnded
self.speechAnnouncer = speechAnnouncer
_currentLogID = State(initialValue: startLogID)
}
var body: some View {
// The ZStack is a stable identity (its lifecycle fires once for the whole run),
// while the child re-identifies per exercise via `.id(currentLogID)`.
ZStack {
ExerciseProgressView(
doc: $doc,
logID: currentLogID,
onChange: onChange,
onLive: onLive,
onLiveEnded: { if !advancing { sendLiveEnded(currentLogID) } },
onActivity: onActivity,
onActivityEnded: { if !advancing { onActivityEnded() } },
incomingFrame: liveRun.current.flatMap { $0.logID == currentLogID ? $0 : nil },
speechAnnouncer: speechAnnouncer,
enteredViaFlow: enteredViaFlow,
onAdvance: advance(to:)
)
.id(currentLogID)
}
.onAppear { liveRun.navigatedRunID = currentLogID }
.onDisappear {
if liveRun.navigatedRunID == currentLogID { liveRun.navigatedRunID = nil }
// Silence narration only when the *whole run* leaves. This host stays mounted
// across per-exercise hand-offs (the child re-ids), so stopping here — rather
// than in the child's onDisappear — no longer cuts off the next exercise's cue.
speechAnnouncer?.stop()
}
.onChange(of: liveRun.current) { _, frame in
followRemoteHandoff(frame)
}
}
/// Follow the driver across an exercise boundary. Auto-advances are never broadcast, so
/// a frame for a *different* log of this flow workout is always the peer's discrete
/// hand-off into the next exercise — which the per-exercise child can't follow (its
/// incoming-frame filter is scoped to `currentLogID`). Forward-only by plan order and
/// only into a still-runnable log, so a late-delivered frame for an earlier exercise
/// can never yank the flow backwards.
private func followRemoteHandoff(_ frame: LiveProgress?) {
guard doc.autoAdvance == true,
let frame, frame.workoutID == doc.id, frame.logID != currentLogID else { return }
let sorted = doc.logs.sorted { $0.order != $1.order ? $0.order < $1.order : $0.id < $1.id }
guard let from = sorted.firstIndex(where: { $0.id == currentLogID }),
let to = sorted.firstIndex(where: { $0.id == frame.logID }), to > from else { return }
let status = WorkoutStatus(rawValue: sorted[to].status) ?? .notStarted
guard status == .notStarted || status == .inProgress else { return }
advance(to: frame.logID)
}
/// Hand off to the next exercise. Suppresses the leaving exercise's live-ended/activity
/// teardown so the mirror follows across the boundary, moves `navigatedRunID` forward so
/// this device doesn't mirror its own next exercise, then swaps the presented log.
private func advance(to next: String) {
advancing = true
liveRun.navigatedRunID = next
enteredViaFlow = true
currentLogID = next
// Release the guard after the swap settles (old view torn down, new one begun).
Task { @MainActor in advancing = false }
}
}