Record per-set actuals and drive the chart and volume from them

Every completed set now writes a SetEntry (reps/weight or seconds),
pre-filled from the plan by transition(to:) so the list checkbox, both
run flows, and One More all capture for free; reset clears, skip keeps
partials. The rest and finish pages show the just-done set as a pill
that opens a stepper sheet for correcting reps and weight (2.5 lb /
1.25 kg steps). The Weight Progression chart plots the top-set actual
weight and workout volume sums recorded sets, both falling back to the
plan for legacy logs via effectiveSetEntries.

Storage side of UX #3 rides along: plan weights are Double now.
Schema bumps: SplitDocument 2→3, WorkoutDocument 3→4 (a fractional
weight fails an older Int decode, and a rewrite would strip the
irreplaceable actuals), SwiftData cache 4→5. A per-log updatedAt is
reserved for the future cross-device log merge.

Claude-Session: https://claude.ai/code/session_01HJDQQDA9QdP8zByg43H5v3
This commit is contained in:
2026-07-08 12:48:37 -04:00
parent c05e83cff7
commit 394ec0989e
24 changed files with 1193 additions and 77 deletions
+70 -4
View File
@@ -35,7 +35,10 @@ struct SplitDocument: Codable, Sendable, Equatable, Identifiable {
// added. Removing required fields means older apps can no longer decode new
// files, so the forward-gate must quarantine them rather than let an older app
// silently rewrite them.
static let currentSchemaVersion = 2
// Bumped 23 when `ExerciseDocument.weight` went Int Double: an older app
// decoding a fractional weight (e.g. 45.5) into `Int` fails, so it must
// quarantine rather than rewrite.
static let currentSchemaVersion = 3
var relativePath: String { "Splits/\(id).json" }
}
@@ -46,7 +49,7 @@ struct ExerciseDocument: Codable, Sendable, Equatable, Identifiable {
var order: Int
var sets: Int
var reps: Int
var weight: Int
var weight: Double // unit-less; fractional since schema v3 (Int weights decode natively)
var loadType: Int
var durationSeconds: Int // total seconds (0 when not a timed exercise)
/// Ordered, user-defined comfort settings for machine-based exercises (seat
@@ -64,6 +67,15 @@ struct MachineSetting: Codable, Sendable, Equatable {
var value: String // "4", "3rd hole", "45°" free-form; machine dials aren't uniformly numeric
}
/// One performed set. Which fields are set follows the log's `LoadType`:
/// weight reps + weight; none (bodyweight) reps; duration seconds.
struct SetEntry: Codable, Sendable, Equatable {
var reps: Int? = nil
var weight: Double? = nil
var seconds: Int? = nil
var completedAt: Date
}
// MARK: - Workout
struct WorkoutDocument: Codable, Sendable, Equatable, Identifiable {
@@ -90,7 +102,12 @@ struct WorkoutDocument: Codable, Sendable, Equatable, Identifiable {
// `WorkoutLogDocument` and `machineSettings` was added to it. Dropping a
// required field means older apps can no longer decode new files, so the
// forward-gate must quarantine them.
static let currentSchemaVersion = 3
// Bumped 34 when `WorkoutLogDocument.weight` went Int Double and per-set
// actuals (`setEntries`) were added: an older app decoding a fractional weight
// into `Int` fails, and one rewriting the file would strip `setEntries`
// the recorded actuals are irreplaceable (same rationale as the v2 `metrics`
// bump), so older apps must quarantine.
static let currentSchemaVersion = 4
var relativePath: String { Self.relativePath(id: id, start: start) }
@@ -153,7 +170,7 @@ struct WorkoutLogDocument: Codable, Sendable, Equatable, Identifiable {
var order: Int
var sets: Int
var reps: Int
var weight: Int
var weight: Double // unit-less plan snapshot; fractional since schema v4
var loadType: Int
var durationSeconds: Int // total seconds (0 when not a timed exercise)
var currentStateIndex: Int
@@ -168,6 +185,14 @@ struct WorkoutLogDocument: Codable, Sendable, Equatable, Identifiable {
/// Optional additions absent in files written before they existed.
var startedAt: Date? = nil
var completedAt: Date? = nil
/// Per-set actuals, in set order. `nil` legacy file / nothing recorded;
/// entries are appended as sets complete (pre-filled from the plan) and only
/// diverge from it when the user adjusts one. Cleared on reset to `.notStarted`.
var setEntries: [SetEntry]? = nil
/// Reserved for the per-log merge H1's conflict resolution needs (a log edited
/// on two devices merges by the newer `updatedAt`). Nothing writes it yet;
/// absent in older files.
var updatedAt: Date? = nil
}
extension WorkoutLogDocument {
@@ -204,17 +229,58 @@ extension WorkoutLogDocument {
status = newStatus.rawValue
switch newStatus {
case .notStarted:
// A full reset the run never happened, so recorded actuals go too.
startedAt = nil
completedAt = nil
setEntries = nil
case .inProgress:
if startedAt == nil { startedAt = Date() }
completedAt = nil
case .completed:
if completedAt == nil { completedAt = Date() }
// Sets that completed without an explicit record (list checkbox,
// completeExercise) get plan-default entries; recorded ones are kept.
fillSetEntries(upTo: sets, at: completedAt ?? Date())
case .skipped:
// A skipped log keeps its partial entries "End Workout, keeping
// progress" must not discard the sets that were done.
completedAt = nil
}
}
/// Append plan-default entries for sets that completed without an explicit
/// record, up to `count` in total. Only the missing tail is filled entries
/// already recorded are never overwritten and a zero `count` on a log with
/// no entries leaves `setEntries` nil (nothing was performed).
mutating func fillSetEntries(upTo count: Int, at date: Date) {
var entries = setEntries ?? []
guard entries.count < count else { return }
while entries.count < count {
entries.append(planSetEntry(completedAt: date))
}
setEntries = entries
}
/// One plan-default entry, shaped by the log's `LoadType`:
/// weight reps + weight; none (bodyweight) reps; duration seconds.
func planSetEntry(completedAt date: Date) -> SetEntry {
switch LoadType(rawValue: loadType) ?? .weight {
case .weight: SetEntry(reps: reps, weight: weight, completedAt: date)
case .none: SetEntry(reps: reps, completedAt: date)
case .duration: SetEntry(seconds: durationSeconds, completedAt: date)
}
}
/// The entries read paths consume the single fallback rule for legacy files:
/// recorded actuals when any exist, else plan-synthesized entries for a
/// *completed* log (what `fillSetEntries` would have recorded), else empty
/// (an unfinished log with nothing recorded performed nothing).
var effectiveSetEntries: [SetEntry] {
if let setEntries, !setEntries.isEmpty { return setEntries }
guard status == WorkoutStatus.completed.rawValue else { return [] }
let date = completedAt ?? date
return (0..<max(0, sets)).map { _ in planSetEntry(completedAt: date) }
}
}
// MARK: - Workout health metrics