Surface machine settings in the exercise library and refine machine rigs

Library detail screens now lead with the user's recorded machine settings
(per-split when they disagree, empty-state card for machine-based entries)
and append the weight progression chart. Starter seeds mark machine
exercises with an empty machineSettings list so the settings UI lights up
before first use. The figure rig gains a frontal body profile for face-on
machines, props that can ride mid joints (knees/elbows), and an
alternating four-frame Bird Dog loop.

Claude-Session: https://claude.ai/code/session_01LEoff8bXGBS83tK1c55Mf7
This commit is contained in:
2026-07-06 18:35:15 -04:00
parent ff1ffbb1f6
commit 669ecf1259
76 changed files with 733 additions and 268 deletions
@@ -6,6 +6,7 @@
//
import SwiftUI
import SwiftData
/// The Exercises library screen, pushed from Settings Library: every exercise with
/// a bundled motion rig (the same catalog the picker offers), each opening its
@@ -21,22 +22,73 @@ struct ExerciseLibraryView: View {
}
}
/// One library exercise, in the standard half-and-half exercise layout: the authored
/// reference content (summary, targets, setup/execution/cues) scrolls in the top
/// half, the looping form-guide figure holds the bottom. Falls back to a full-screen
/// figure when no `info.md` is bundled.
/// One library exercise, in the standard half-and-half exercise layout: the top half
/// scrolls the user's machine settings (their setup cheat-sheet), the authored
/// reference content (summary, targets, setup/execution/cues), and the weight
/// progression chart; the looping form-guide figure holds the bottom.
struct ExerciseLibraryDetailView: View {
let exerciseName: String
/// This exercise wherever it appears in the user's splits the source of the
/// recorded machine settings.
@Query private var exercises: [Exercise]
/// One completed weighted log is enough to justify the progression chart;
/// bodyweight and timed exercises never accrue one, so they skip the chart
/// instead of plotting a flat zero line.
@Query private var weightedLogs: [WorkoutLog]
init(exerciseName: String) {
self.exerciseName = exerciseName
let name = exerciseName
_exercises = Query(filter: #Predicate<Exercise> { $0.name == name })
let completedRaw = WorkoutStatus.completed.rawValue
_weightedLogs = Query(filter: #Predicate<WorkoutLog> {
$0.exerciseName == name && $0.statusRaw == completedRaw && $0.weight > 0
})
}
private var info: ExerciseInfo? {
ExerciseInfoLibrary.info(for: exerciseName)
}
/// Distinct recorded machine-settings lists for this exercise. Usually one; when
/// splits disagree, each distinct list keeps its split's name as a label.
private var settingsGroups: [(label: String?, settings: [MachineSetting])] {
var groups: [(label: String?, settings: [MachineSetting])] = []
for exercise in exercises {
guard let settings = exercise.machineSettings, !settings.isEmpty else { continue }
guard !groups.contains(where: { $0.settings == settings }) else { continue }
groups.append((exercise.split?.name, settings))
}
if groups.count == 1 { groups[0].label = nil }
return groups
}
var body: some View {
VStack(spacing: 0) {
if let info {
ExerciseInfoContent(info: info)
ScrollView {
VStack(alignment: .leading, spacing: 16) {
// Show the card whenever settings are recorded or, for an
// exercise the authored info identifies as machine-based, as an
// empty state, so the feature is discoverable before first use.
if !settingsGroups.isEmpty {
MachineSettingsCard(groups: settingsGroups)
} else if info?.isMachineBased == true {
MachineSettingsCard(groups: [])
}
if let info {
ExerciseInfoContent(info: info)
}
if !weightedLogs.isEmpty {
WeightProgressionChartView(exerciseName: exerciseName)
}
}
.padding()
.frame(maxWidth: .infinity, alignment: .leading)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
ExerciseFigureSlot(exerciseName: exerciseName)
}
.navigationTitle(exerciseName)
@@ -44,38 +96,76 @@ struct ExerciseLibraryDetailView: View {
}
}
/// The user's recorded machine comfort settings, set apart from the reference text
/// as a card: namevalue rows, grouped per split when the recorded lists differ.
/// Empty `groups` renders the not-yet-recorded state (shown for machine-based
/// exercises so the feature is visible before first use).
private struct MachineSettingsCard: View {
let groups: [(label: String?, settings: [MachineSetting])]
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text("Your Machine Settings")
.font(.headline)
if groups.isEmpty {
Text("None recorded yet. Mark the exercise as machine-based in its editor, or use the settings button on the workout screen.")
.font(.callout)
.foregroundStyle(.secondary)
}
ForEach(Array(groups.enumerated()), id: \.offset) { _, group in
VStack(alignment: .leading, spacing: 4) {
if let label = group.label {
Text(label)
.font(.caption)
.foregroundStyle(.secondary)
}
ForEach(Array(group.settings.enumerated()), id: \.offset) { _, setting in
HStack(alignment: .firstTextBaseline) {
Text(setting.name)
.foregroundStyle(.secondary)
Spacer()
Text(setting.value)
.fontWeight(.semibold)
.monospacedDigit()
}
.font(.callout)
}
}
}
}
.padding(12)
.frame(maxWidth: .infinity, alignment: .leading)
.background(Color(.secondarySystemBackground), in: RoundedRectangle(cornerRadius: 12))
}
}
/// Renders a parsed `ExerciseInfo`: summary, target chips, then each authored
/// section with its numbered steps or bullets.
private struct ExerciseInfoContent: View {
let info: ExerciseInfo
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 16) {
if !info.summary.isEmpty {
Text(info.summary)
.font(.callout)
.foregroundStyle(.secondary)
}
VStack(alignment: .leading, spacing: 16) {
if !info.summary.isEmpty {
Text(info.summary)
.font(.callout)
.foregroundStyle(.secondary)
}
if !info.targets.isEmpty {
TargetChips(targets: info.targets)
}
if !info.targets.isEmpty {
TargetChips(targets: info.targets)
}
ForEach(info.sections, id: \.title) { section in
VStack(alignment: .leading, spacing: 6) {
Text(section.title)
.font(.headline)
ForEach(Array(section.items.enumerated()), id: \.offset) { index, item in
itemRow(item, ordinal: ordinal(for: item, at: index, in: section))
}
ForEach(info.sections, id: \.title) { section in
VStack(alignment: .leading, spacing: 6) {
Text(section.title)
.font(.headline)
ForEach(Array(section.items.enumerated()), id: \.offset) { index, item in
itemRow(item, ordinal: ordinal(for: item, at: index, in: section))
}
}
}
.padding()
.frame(maxWidth: .infinity, alignment: .leading)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
/// Steps number themselves by their position among the section's steps, so an
+74 -3
View File
@@ -12,6 +12,7 @@ import SwiftData
import Charts
struct ExerciseView: View {
@Environment(SyncEngine.self) private var sync
@Environment(AppServices.self) private var services
let workout: Workout
@@ -23,6 +24,7 @@ struct ExerciseView: View {
@State private var doc: WorkoutDocument
@State private var showingPlanEdit = false
@State private var showingNotesEdit = false
@State private var showingMachineSettings = false
/// `seedDoc` lets the caller hand over an in-memory document (e.g. the parent's
/// working copy right after adding an exercise) so the screen doesn't wait on
@@ -41,6 +43,9 @@ struct ExerciseView: View {
var body: some View {
Group {
if let log {
// Deliberately NOT the half-and-half figure layout: this screen is
// dense with the plan, notes, settings, and the progression chart
// the form guide lives in the run flow and the exercise library.
content(for: log)
} else {
// The just-added log hasn't reached the cache yet; refresh shortly.
@@ -54,6 +59,16 @@ struct ExerciseView: View {
.sheet(isPresented: $showingNotesEdit) {
NotesEditView(workout: workout, logID: logID)
}
.sheet(isPresented: $showingMachineSettings) {
if let log {
MachineSettingsSheet(
exerciseName: log.exerciseName,
initialSettings: log.machineSettings ?? []
) { updated in
applyMachineSettings(updated)
}
}
}
.onAppear {
refreshDocIfNeeded()
// Take over this run: the watch parks and locks it while we're editing here.
@@ -94,6 +109,42 @@ struct ExerciseView: View {
}
}
// MARK: - Machine Settings shown when the log carries settings, or for
// any exercise the authored library identifies as machine-based, so the
// section is discoverable before settings are first recorded.
if log.machineSettings != nil
|| ExerciseInfoLibrary.info(for: log.exerciseName)?.isMachineBased == true {
let settings = log.machineSettings ?? []
Section {
if settings.isEmpty {
Text("No settings recorded")
.foregroundColor(.secondary)
.italic()
} else {
ForEach(Array(settings.enumerated()), id: \.offset) { _, setting in
HStack(alignment: .firstTextBaseline) {
Text(setting.name)
.foregroundStyle(.secondary)
Spacer()
Text(setting.value)
.fontWeight(.semibold)
.monospacedDigit()
}
}
}
} header: {
HStack {
Text("Machine Settings")
Spacer()
Button("Edit") {
showingMachineSettings = true
}
.font(.subheadline)
.textCase(.none)
}
}
}
// MARK: - Notes Section (Read-only with Edit button)
Section {
if let notes = log.notes, !notes.isEmpty {
@@ -116,9 +167,12 @@ struct ExerciseView: View {
}
}
// MARK: - Progress Tracking Chart
Section(header: Text("Progress Tracking")) {
WeightProgressionChartView(exerciseName: log.exerciseName)
// MARK: - Progress Tracking Chart (weighted exercises only a weight
// chart is meaningless for bodyweight/timed logs)
if LoadType(rawValue: log.loadType) == .weight {
Section(header: Text("Progress Tracking")) {
WeightProgressionChartView(exerciseName: log.exerciseName)
}
}
}
// Pull plan/notes edits made in the sheets back into the live doc.
@@ -130,6 +184,23 @@ struct ExerciseView: View {
}
}
/// Apply edited machine settings: update the log's plan-time snapshot (persisted
/// with the workout), then write the same settings back to the originating
/// split's exercise as the durable default same sequencing as the workout
/// list's machine sheet.
private func applyMachineSettings(_ settings: [MachineSetting]) {
guard let i = doc.logs.firstIndex(where: { $0.id == logID }) else { return }
doc.logs[i].machineSettings = settings
doc.updatedAt = Date()
let snapshot = doc
let exerciseName = doc.logs[i].exerciseName
let splitID = doc.splitID
Task {
await sync.save(workout: snapshot)
await sync.writeBackMachineSettings(settings, exerciseName: exerciseName, splitID: splitID)
}
}
/// If the requested log isn't in the working doc yet (just-added race), pull a
/// fresh copy from the cache entity once it catches up.
private func refreshDocIfNeeded() {
@@ -334,28 +334,10 @@ struct WorkoutLogListView: View {
let splitID = doc.splitID
Task {
await sync.save(workout: workoutSnapshot)
await writeBackMachineSettings(settings, exerciseName: exerciseName, splitID: splitID)
await sync.writeBackMachineSettings(settings, exerciseName: exerciseName, splitID: splitID)
}
}
/// Push the settings onto the originating split's exercise (matched by name
/// logs reference exercises by name only), following the seed clone-on-edit
/// redirect so the live clone is updated. Skips silently when the split is gone
/// or no exercise matches, and when nothing changed (so a pristine seed isn't
/// needlessly forked). Never caches the split's id across the save, since saving
/// a seed mints a new one.
private func writeBackMachineSettings(_ settings: [MachineSetting], exerciseName: String, splitID: String?) async {
guard let splitID else { return }
let liveID = sync.currentSplitID(for: splitID)
guard let split = CacheMapper.fetchSplit(id: liveID, in: modelContext) else { return }
var splitDoc = SplitDocument(from: split)
guard let idx = splitDoc.exercises.firstIndex(where: { $0.name == exerciseName }) else { return }
guard splitDoc.exercises[idx].machineSettings != settings else { return }
splitDoc.exercises[idx].machineSettings = settings
splitDoc.updatedAt = Date()
await sync.save(split: splitDoc)
}
/// Recompute the workout's status/end from its logs, then persist.
private func save() {
doc.recomputeStatusFromLogs()
@@ -549,8 +531,9 @@ struct SplitExercisePickerSheet: View {
/// machine-based log (non-nil `machineSettings`), so there's no machine toggle
/// just the ordered rows (reusing `MachineSettingsEditor`) plus Save/Cancel. Save
/// hands the edited array back to the caller, which persists it to the log and
/// writes it back to the split's exercise as the new default.
private struct MachineSettingsSheet: View {
/// writes it back to the split's exercise as the new default. Shared with
/// `ExerciseView`'s Machine Settings section.
struct MachineSettingsSheet: View {
@Environment(\.dismiss) private var dismiss
let exerciseName: String