Watch sessions now run an HKLiveWorkoutBuilder: heart rate and active energy are collected live (shown in an in-workout HUD), saved to Health as a real HKWorkout on completion, and carried back to the phone as WorkoutMetrics on the workout document. Phone-only workouts get an estimated Health workout (MET x bodyweight x duration) after a 10s debounce that a late-arriving watch session cancels; a launch sweep backfills workouts completed within the last day. Dedupe is keyed on metrics.healthKitWorkoutUUID plus the metrics source. Splits gain an activity type (strength, functional, HIIT, core, cardio, cycling) that categorizes the Health workout and picks the MET value. A post-workout summary sheet (duration, calories, avg/max HR, HR zones, total volume) fills in live and is also shown on completed workouts. Weights can now display in lb or kg (display-only relabel), synced to the watch over the existing application context. WorkoutDocument schema 1->2 (metrics are irreplaceable, so older apps must quarantine rather than strip them); cache schema 2 rebuilds the SwiftData store with the new metric columns. Deleting a workout in the app intentionally leaves its Health record in place.
310 lines
11 KiB
Swift
310 lines
11 KiB
Swift
//
|
||
// WorkoutLogListView.swift
|
||
// Workouts Watch App
|
||
//
|
||
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
|
||
//
|
||
|
||
import SwiftUI
|
||
import SwiftData
|
||
|
||
struct WorkoutLogListView: View {
|
||
@Environment(WatchConnectivityBridge.self) private var bridge
|
||
@Environment(WorkoutSessionManager.self) private var sessionManager
|
||
@Environment(\.modelContext) private var modelContext
|
||
|
||
/// Working copy of the workout. We drive the UI from this and mutate it on
|
||
/// every edit (then forward through the bridge) to avoid the read-after-write
|
||
/// race against the cache, which lags local writes by a beat.
|
||
@State private var doc: WorkoutDocument
|
||
|
||
@State private var showingExercisePicker = false
|
||
@State private var selectedLogID: String?
|
||
|
||
init(workout: Workout) {
|
||
_doc = State(initialValue: WorkoutDocument(from: workout))
|
||
}
|
||
|
||
/// The split this workout came from (read-only on the watch), used to offer
|
||
/// additional exercises that aren't logged yet. Fetched imperatively — *not* via
|
||
/// `@Query` — so the list body never observes the live `Split` or traverses its
|
||
/// `exercises` relationship during a render. Doing so (a `@Query`-observed model
|
||
/// whose to-many relationship is read in `body`) drove a SwiftData re-render loop
|
||
/// that hung the watch. `availableExercises` is therefore only ever evaluated from
|
||
/// the picker sheet's closure, not from `body`.
|
||
private var split: Split? {
|
||
guard let splitID = doc.splitID else { return nil }
|
||
return CacheMapper.fetchSplit(id: splitID, in: modelContext)
|
||
}
|
||
|
||
private var sortedLogs: [WorkoutLogDocument] {
|
||
doc.logs.sorted { $0.order < $1.order }
|
||
}
|
||
|
||
private var availableExercises: [Exercise] {
|
||
guard let split else { return [] }
|
||
let existingNames = Set(doc.logs.map { $0.exerciseName })
|
||
return split.exercisesArray.filter { !existingNames.contains($0.name) }
|
||
}
|
||
|
||
var body: some View {
|
||
List {
|
||
if sessionManager.isRunning {
|
||
Section { LiveMetricsHUD() }
|
||
}
|
||
|
||
Section(header: Text(label)) {
|
||
ForEach(sortedLogs) { log in
|
||
Button {
|
||
selectedLogID = log.id
|
||
} label: {
|
||
WorkoutLogRowLabel(log: log)
|
||
}
|
||
.buttonStyle(.plain)
|
||
}
|
||
}
|
||
|
||
if doc.splitID != nil {
|
||
Section {
|
||
Button {
|
||
showingExercisePicker = true
|
||
} label: {
|
||
HStack {
|
||
Image(systemName: "plus.circle.fill")
|
||
.foregroundColor(.green)
|
||
Text("Add Exercise")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
.overlay {
|
||
if sortedLogs.isEmpty {
|
||
ContentUnavailableView(
|
||
"No Exercises",
|
||
systemImage: "figure.strengthtraining.traditional",
|
||
description: Text(doc.splitID == nil
|
||
? "No exercises in this workout."
|
||
: "Tap + to add exercises.")
|
||
)
|
||
}
|
||
}
|
||
.navigationTitle(doc.splitName ?? Split.unnamed)
|
||
.navigationDestination(item: $selectedLogID) { logID in
|
||
ExerciseProgressView(
|
||
doc: $doc,
|
||
logID: logID,
|
||
onChange: { bridge.update(workout: doc) },
|
||
onLive: { bridge.sendLiveProgress($0) },
|
||
onLiveEnded: { bridge.sendLiveEnded(workoutID: doc.id, logID: logID) },
|
||
// Follow the phone when it drives this same run from its mirror.
|
||
incomingFrame: bridge.liveIncoming.flatMap { $0.logID == logID ? $0 : nil }
|
||
)
|
||
// We're driving this run inline — suppress the follower cover for it.
|
||
.onAppear { bridge.navigatedRunID = logID }
|
||
.onDisappear { if bridge.navigatedRunID == logID { bridge.navigatedRunID = nil } }
|
||
}
|
||
.sheet(isPresented: $showingExercisePicker) {
|
||
ExercisePickerView(exercises: availableExercises) { exercise in
|
||
addExercise(exercise)
|
||
}
|
||
}
|
||
}
|
||
|
||
private var label: String {
|
||
let start = doc.start
|
||
if doc.status == WorkoutStatus.completed.rawValue, let end = doc.end {
|
||
if start.isSameDay(as: end) {
|
||
return "\(start.formattedDate())—\(end.formattedTime())"
|
||
} else {
|
||
return "\(start.formattedDate())—\(end.formattedDate())"
|
||
}
|
||
}
|
||
return start.formattedDate()
|
||
}
|
||
|
||
private func addExercise(_ exercise: Exercise) {
|
||
let newLog = WorkoutLogDocument(
|
||
id: ULID.make(),
|
||
exerciseName: exercise.name,
|
||
order: doc.logs.count,
|
||
sets: exercise.sets,
|
||
reps: exercise.reps,
|
||
weight: exercise.weight,
|
||
loadType: exercise.loadType,
|
||
durationSeconds: exercise.durationTotalSeconds,
|
||
currentStateIndex: 0,
|
||
completed: false,
|
||
status: WorkoutStatus.notStarted.rawValue,
|
||
notes: nil,
|
||
date: doc.start
|
||
)
|
||
doc.logs.append(newLog)
|
||
doc.updatedAt = Date()
|
||
bridge.update(workout: doc)
|
||
showingExercisePicker = false
|
||
}
|
||
}
|
||
|
||
// MARK: - Live Metrics HUD
|
||
|
||
/// Compact live readout (heart rate + active calories) shown while a watch session is
|
||
/// running. Reads the session manager from the environment itself so its per-second
|
||
/// updates re-render only this row, not the editable log list above it. Renders
|
||
/// nothing until the first sample arrives, and dims under Always-On like the timers.
|
||
private struct LiveMetricsHUD: View {
|
||
@Environment(WorkoutSessionManager.self) private var sessionManager
|
||
@Environment(\.isLuminanceReduced) private var dimmed
|
||
|
||
var body: some View {
|
||
let heartRate = sessionManager.currentHeartRate
|
||
let energy = sessionManager.currentActiveEnergyKcal
|
||
if heartRate != nil || energy != nil {
|
||
HStack(spacing: 16) {
|
||
if let heartRate {
|
||
Label("\(Int(heartRate.rounded()))", systemImage: "heart.fill")
|
||
.foregroundStyle(dimmed ? Color.secondary : Color.red)
|
||
}
|
||
if let energy {
|
||
Label("\(Int(energy.rounded())) cal", systemImage: "flame.fill")
|
||
.foregroundStyle(dimmed ? Color.secondary : Color.orange)
|
||
}
|
||
}
|
||
.font(.caption.monospacedDigit())
|
||
.frame(maxWidth: .infinity)
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - Workout Log Row Label
|
||
|
||
struct WorkoutLogRowLabel: View {
|
||
let log: WorkoutLogDocument
|
||
@AppStorage("weightUnit") private var weightUnit: WeightUnit = .lb
|
||
|
||
var body: some View {
|
||
HStack {
|
||
statusIcon
|
||
.foregroundColor(statusColor)
|
||
|
||
VStack(alignment: .leading, spacing: 2) {
|
||
Text(log.exerciseName)
|
||
.font(.headline)
|
||
.lineLimit(1)
|
||
|
||
Text(subtitle)
|
||
.font(.caption2)
|
||
.foregroundColor(.secondary)
|
||
}
|
||
|
||
Spacer()
|
||
}
|
||
}
|
||
|
||
private var status: WorkoutStatus {
|
||
WorkoutStatus(rawValue: log.status) ?? .notStarted
|
||
}
|
||
|
||
private var statusIcon: Image {
|
||
switch status {
|
||
case .completed:
|
||
Image(systemName: "checkmark.circle.fill")
|
||
case .inProgress:
|
||
Image(systemName: "circle.dotted")
|
||
case .notStarted:
|
||
Image(systemName: "circle")
|
||
case .skipped:
|
||
Image(systemName: "xmark.circle")
|
||
}
|
||
}
|
||
|
||
private var statusColor: Color {
|
||
switch status {
|
||
case .completed:
|
||
.accentColor
|
||
case .inProgress:
|
||
.gray
|
||
case .notStarted:
|
||
.secondary
|
||
case .skipped:
|
||
.secondary
|
||
}
|
||
}
|
||
|
||
private var subtitle: String {
|
||
if LoadType(rawValue: log.loadType) == .duration {
|
||
let mins = log.durationSeconds / 60
|
||
let secs = log.durationSeconds % 60
|
||
if mins > 0 && secs > 0 {
|
||
return "\(log.sets) × \(mins)m \(secs)s"
|
||
} else if mins > 0 {
|
||
return "\(log.sets) × \(mins) min"
|
||
} else {
|
||
return "\(log.sets) × \(secs) sec"
|
||
}
|
||
} else {
|
||
return "\(log.sets) × \(log.reps) × \(weightUnit.format(log.weight))"
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - Exercise Picker View
|
||
|
||
struct ExercisePickerView: View {
|
||
@Environment(\.dismiss) private var dismiss
|
||
|
||
let exercises: [Exercise]
|
||
let onSelect: (Exercise) -> Void
|
||
@AppStorage("weightUnit") private var weightUnit: WeightUnit = .lb
|
||
|
||
var body: some View {
|
||
NavigationStack {
|
||
List {
|
||
if exercises.isEmpty {
|
||
Text("All exercises added")
|
||
.foregroundColor(.secondary)
|
||
} else {
|
||
ForEach(exercises) { exercise in
|
||
Button {
|
||
onSelect(exercise)
|
||
dismiss()
|
||
} label: {
|
||
VStack(alignment: .leading) {
|
||
Text(exercise.name)
|
||
.font(.headline)
|
||
Text(exerciseSubtitle(exercise))
|
||
.font(.caption2)
|
||
.foregroundColor(.secondary)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
.navigationTitle("Add Exercise")
|
||
.toolbar {
|
||
ToolbarItem(placement: .cancellationAction) {
|
||
Button("Cancel") {
|
||
dismiss()
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private func exerciseSubtitle(_ exercise: Exercise) -> String {
|
||
if exercise.loadTypeEnum == .duration {
|
||
let mins = exercise.durationMinutes
|
||
let secs = exercise.durationSeconds
|
||
if mins > 0 && secs > 0 {
|
||
return "\(exercise.sets) × \(mins)m \(secs)s"
|
||
} else if mins > 0 {
|
||
return "\(exercise.sets) × \(mins) min"
|
||
} else {
|
||
return "\(exercise.sets) × \(secs) sec"
|
||
}
|
||
} else {
|
||
return "\(exercise.sets) × \(exercise.reps) × \(weightUnit.format(exercise.weight))"
|
||
}
|
||
}
|
||
}
|