Restructure into a three-tab app with Progress, goals, and Meditation

The UX redesign's first landing (spec in UX-REDESIGN.md): ContentView
becomes a Today / Progress / Settings TabView, "Routine" replaces
"Split" in every user-facing string and view name (code-level types
keep their names), and workout starting moves to shared
WorkoutStarter / StartedWorkoutNavigator plumbing.

- New Progress tab: weekly goal streaks, workout trends, per-exercise
  weight progression, achievements, and the full history list
  (WorkoutLogsView -> WorkoutHistoryView).
- Goals: stable categories workouts roll up to, managed from Settings.
- New Meditation exercise + starter routine; timed sits record to
  Apple Health as Mind & Body sessions.

Claude-Session: https://claude.ai/code/session_012qw2itfzKyEJ1HpsFt8Ex4
This commit is contained in:
2026-07-11 07:53:01 -04:00
parent 5c201289fb
commit 6e440317c4
97 changed files with 5354 additions and 1291 deletions
@@ -19,7 +19,7 @@ struct SessionEndPlannerTests {
private func workout(_ id: String, _ status: WorkoutStatus, start: Date = ts) -> WorkoutDocument {
WorkoutDocument(
schemaVersion: WorkoutDocument.currentSchemaVersion,
id: id, splitID: nil, splitName: nil, start: start, end: nil,
id: id, routineID: nil, routineName: nil, start: start, end: nil,
status: status.rawValue, createdAt: start, updatedAt: start,
logs: [], metrics: nil)
}
@@ -6,9 +6,9 @@ import Testing
/// Locks the phonewatch state apply/prune contract the pure `WatchCacheApplier` seam the
/// `WatchConnectivityBridge` delegates its cache mutation to. The bridge wraps `WCSession`, so
/// this session-free seam is the testable surface for the three behaviors that matter:
/// 1. An authoritative push upserts the splits/workouts the phone sent.
/// 1. An authoritative push upserts the routines/workouts the phone sent.
/// 2. An authoritative **empty** push prunes stale rows the phone no longer has (the
/// recently-fixed case otherwise a deleted split / aged-out run is orphaned forever).
/// recently-fixed case otherwise a deleted routine / aged-out run is orphaned forever).
/// 3. A `nil` set (the phone's payload failed to decode a build/schema mismatch) is a
/// decode failure, not an empty set: it must skip entirely and NOT prune real rows.
///
@@ -20,15 +20,15 @@ struct WatchCacheApplierTests {
private static let ts = Date(timeIntervalSince1970: 1_700_000_000)
private func makeContext() throws -> ModelContext {
let schema = Schema([Split.self, Exercise.self, Workout.self, WorkoutLog.self])
let schema = Schema([Routine.self, Exercise.self, Workout.self, WorkoutLog.self])
// cloudKitDatabase: .none matches WorkoutsModelContainer (the watch's read-through cache).
let config = ModelConfiguration(schema: schema, isStoredInMemoryOnly: true, cloudKitDatabase: .none)
return ModelContext(try ModelContainer(for: schema, configurations: [config]))
}
private func split(id: String, name: String) -> SplitDocument {
SplitDocument(
schemaVersion: SplitDocument.currentSchemaVersion, id: id, name: name, color: "blue",
private func routine(id: String, name: String) -> RoutineDocument {
RoutineDocument(
schemaVersion: RoutineDocument.currentSchemaVersion, id: id, name: name, color: "blue",
systemImage: "dumbbell.fill", order: 0, createdAt: Self.ts, updatedAt: Self.ts,
exercises: [ExerciseDocument(id: "EX-\(id)", name: "Bench Press", order: 0, sets: 4,
reps: 10, weight: 135, loadType: LoadType.weight.rawValue,
@@ -39,7 +39,7 @@ struct WatchCacheApplierTests {
private func workout(id: String, name: String = "Push") -> WorkoutDocument {
WorkoutDocument(
schemaVersion: WorkoutDocument.currentSchemaVersion, id: id, splitID: "S", splitName: name,
schemaVersion: WorkoutDocument.currentSchemaVersion, id: id, routineID: "S", routineName: name,
start: Self.ts, end: nil, status: WorkoutStatus.inProgress.rawValue,
createdAt: Self.ts, updatedAt: Self.ts,
logs: [WorkoutLogDocument(id: "L-\(id)", exerciseName: "Bench Press", order: 0, sets: 4,
@@ -50,8 +50,8 @@ struct WatchCacheApplierTests {
)
}
private func splitIDs(in ctx: ModelContext) -> Set<String> {
Set((try? ctx.fetch(FetchDescriptor<Split>()))?.map(\.id) ?? [])
private func routineIDs(in ctx: ModelContext) -> Set<String> {
Set((try? ctx.fetch(FetchDescriptor<Routine>()))?.map(\.id) ?? [])
}
private func workoutIDs(in ctx: ModelContext) -> Set<String> {
@@ -60,25 +60,25 @@ struct WatchCacheApplierTests {
// MARK: - Authoritative upsert
@Test func authoritativePushUpsertsSplitsAndWorkouts() throws {
@Test func authoritativePushUpsertsRoutinesAndWorkouts() throws {
let ctx = try makeContext()
WatchCacheApplier.apply(
splits: [split(id: "SP1", name: "Upper"), split(id: "SP2", name: "Lower")],
routines: [routine(id: "SP1", name: "Upper"), routine(id: "SP2", name: "Lower")],
workouts: [workout(id: "01WKA"), workout(id: "01WKB")],
into: ctx
)
#expect(splitIDs(in: ctx) == ["SP1", "SP2"])
#expect(routineIDs(in: ctx) == ["SP1", "SP2"])
#expect(workoutIDs(in: ctx) == ["01WKA", "01WKB"])
}
@Test func reUpsertSameIDUpdatesInPlaceWithoutDuplicating() throws {
let ctx = try makeContext()
WatchCacheApplier.apply(splits: [split(id: "SP1", name: "Original")], workouts: [], into: ctx)
WatchCacheApplier.apply(splits: [split(id: "SP1", name: "Renamed")], workouts: [], into: ctx)
WatchCacheApplier.apply(routines: [routine(id: "SP1", name: "Original")], workouts: [], into: ctx)
WatchCacheApplier.apply(routines: [routine(id: "SP1", name: "Renamed")], workouts: [], into: ctx)
let splits = try ctx.fetch(FetchDescriptor<Split>())
#expect(splits.count == 1)
#expect(splits.first?.name == "Renamed")
let routines = try ctx.fetch(FetchDescriptor<Routine>())
#expect(routines.count == 1)
#expect(routines.first?.name == "Renamed")
}
// MARK: - Authoritative empty prunes stale rows (the recently-fixed case)
@@ -87,67 +87,67 @@ struct WatchCacheApplierTests {
let ctx = try makeContext()
// Seed the cache as if the phone had previously pushed real state.
WatchCacheApplier.apply(
splits: [split(id: "SP1", name: "Upper")],
routines: [routine(id: "SP1", name: "Upper")],
workouts: [workout(id: "01WKA")],
into: ctx
)
#expect(splitIDs(in: ctx) == ["SP1"])
#expect(routineIDs(in: ctx) == ["SP1"])
#expect(workoutIDs(in: ctx) == ["01WKA"])
// The user deleted everything on the phone (or all runs aged out): an authoritative
// EMPTY push must clear the now-stale rows rather than orphan them forever.
WatchCacheApplier.apply(splits: [], workouts: [], into: ctx)
#expect(splitIDs(in: ctx).isEmpty)
WatchCacheApplier.apply(routines: [], workouts: [], into: ctx)
#expect(routineIDs(in: ctx).isEmpty)
#expect(workoutIDs(in: ctx).isEmpty)
}
@Test func partialPushPrunesOnlyRowsThePhoneNoLongerSends() throws {
let ctx = try makeContext()
WatchCacheApplier.apply(
splits: [split(id: "SP1", name: "Keep"), split(id: "SP2", name: "Drop")],
routines: [routine(id: "SP1", name: "Keep"), routine(id: "SP2", name: "Drop")],
workouts: [workout(id: "01WKKEEP"), workout(id: "01WKDROP")],
into: ctx
)
// The phone now sends only the survivors the omitted ones are pruned.
WatchCacheApplier.apply(
splits: [split(id: "SP1", name: "Keep")],
routines: [routine(id: "SP1", name: "Keep")],
workouts: [workout(id: "01WKKEEP")],
into: ctx
)
#expect(splitIDs(in: ctx) == ["SP1"])
#expect(routineIDs(in: ctx) == ["SP1"])
#expect(workoutIDs(in: ctx) == ["01WKKEEP"])
}
@Test func authoritativeEmptyPushOnEmptyCacheIsANoOp() throws {
let ctx = try makeContext()
WatchCacheApplier.apply(splits: [], workouts: [], into: ctx)
#expect(splitIDs(in: ctx).isEmpty)
WatchCacheApplier.apply(routines: [], workouts: [], into: ctx)
#expect(routineIDs(in: ctx).isEmpty)
#expect(workoutIDs(in: ctx).isEmpty)
}
// MARK: - nil-decode guard: a decode failure must NOT prune
@Test func nilSplitsSkipsEntirelyAndDoesNotPrune() throws {
@Test func nilRoutinesSkipsEntirelyAndDoesNotPrune() throws {
let ctx = try makeContext()
WatchCacheApplier.apply(splits: [split(id: "SP1", name: "Upper")],
WatchCacheApplier.apply(routines: [routine(id: "SP1", name: "Upper")],
workouts: [workout(id: "01WKA")], into: ctx)
// A corrupt/absent splits payload decodes to nil (build mismatch). Skip the existing
// A corrupt/absent routines payload decodes to nil (build mismatch). Skip the existing
// rows must survive, never pruned against a bogus set.
let applied = WatchCacheApplier.apply(splits: nil, workouts: [], into: ctx)
let applied = WatchCacheApplier.apply(routines: nil, workouts: [], into: ctx)
#expect(applied == false)
#expect(splitIDs(in: ctx) == ["SP1"])
#expect(routineIDs(in: ctx) == ["SP1"])
#expect(workoutIDs(in: ctx) == ["01WKA"])
}
@Test func nilWorkoutsSkipsEntirelyAndDoesNotPrune() throws {
let ctx = try makeContext()
WatchCacheApplier.apply(splits: [split(id: "SP1", name: "Upper")],
WatchCacheApplier.apply(routines: [routine(id: "SP1", name: "Upper")],
workouts: [workout(id: "01WKA")], into: ctx)
let applied = WatchCacheApplier.apply(splits: [], workouts: nil, into: ctx)
let applied = WatchCacheApplier.apply(routines: [], workouts: nil, into: ctx)
#expect(applied == false)
#expect(splitIDs(in: ctx) == ["SP1"])
#expect(routineIDs(in: ctx) == ["SP1"])
#expect(workoutIDs(in: ctx) == ["01WKA"])
}
@@ -167,7 +167,7 @@ struct WatchCacheApplierTests {
]
doc.logs[0].updatedAt = Self.ts
WatchCacheApplier.apply(splits: [], workouts: [doc], into: ctx)
WatchCacheApplier.apply(routines: [], workouts: [doc], into: ctx)
let entity = try #require(CacheMapper.fetchWorkout(id: "01WKENTRIES", in: ctx))
let rebuilt = WorkoutDocument(from: entity)
#expect(rebuilt.logs[0].weight == 42.5)
@@ -179,9 +179,9 @@ struct WatchCacheApplierTests {
let ctx = try makeContext()
// Typed optionals to disambiguate onto the nil-aware overload (an authoritative,
// successfully-decoded if empty push), whose Bool return is the applied/skip signal.
let splits: [SplitDocument]? = []
let routines: [RoutineDocument]? = []
let workouts: [WorkoutDocument]? = []
let applied = WatchCacheApplier.apply(splits: splits, workouts: workouts, into: ctx)
let applied = WatchCacheApplier.apply(routines: routines, workouts: workouts, into: ctx)
#expect(applied == true)
}
}
@@ -17,14 +17,14 @@ struct WatchConnectivityBridgeTests {
private static let ts = Date(timeIntervalSince1970: 1_700_000_000)
private func makeContainer() throws -> ModelContainer {
let schema = Schema([Split.self, Exercise.self, Workout.self, WorkoutLog.self])
let schema = Schema([Routine.self, Exercise.self, Workout.self, WorkoutLog.self])
let config = ModelConfiguration(schema: schema, isStoredInMemoryOnly: true, cloudKitDatabase: .none)
return try ModelContainer(for: schema, configurations: [config])
}
private func workout(id: String, name: String, splitName: String) -> WorkoutDocument {
private func workout(id: String, name: String, routineName: String) -> WorkoutDocument {
WorkoutDocument(
schemaVersion: WorkoutDocument.currentSchemaVersion, id: id, splitID: "S", splitName: splitName,
schemaVersion: WorkoutDocument.currentSchemaVersion, id: id, routineID: "S", routineName: routineName,
start: Self.ts, end: nil, status: WorkoutStatus.inProgress.rawValue,
createdAt: Self.ts, updatedAt: Self.ts,
logs: [WorkoutLogDocument(id: "L-\(id)", exerciseName: name, order: 0, sets: 4,
@@ -38,7 +38,7 @@ struct WatchConnectivityBridgeTests {
@Test func updateWritesWorkoutOptimisticallyIntoTheCache() throws {
let container = try makeContainer()
let bridge = WatchConnectivityBridge(container: container)
let doc = workout(id: "01WKOPT", name: "Bench Press", splitName: "Push")
let doc = workout(id: "01WKOPT", name: "Bench Press", routineName: "Push")
#expect(CacheMapper.fetchWorkout(id: doc.id, in: container.mainContext) == nil)
@@ -46,7 +46,7 @@ struct WatchConnectivityBridgeTests {
let fetched = try #require(CacheMapper.fetchWorkout(id: doc.id, in: container.mainContext))
#expect(fetched.id == "01WKOPT")
#expect(fetched.splitName == "Push")
#expect(fetched.routineName == "Push")
#expect(fetched.logsArray.first?.exerciseName == "Bench Press")
}
@@ -54,16 +54,16 @@ struct WatchConnectivityBridgeTests {
let container = try makeContainer()
let bridge = WatchConnectivityBridge(container: container)
bridge.update(workout: workout(id: "01WKEDIT", name: "Bench Press", splitName: "Push"))
bridge.update(workout: workout(id: "01WKEDIT", name: "Bench Press", routineName: "Push"))
var edited = workout(id: "01WKEDIT", name: "Incline Press", splitName: "Push Day")
var edited = workout(id: "01WKEDIT", name: "Incline Press", routineName: "Push Day")
edited.status = WorkoutStatus.completed.rawValue
bridge.update(workout: edited)
let all = try container.mainContext.fetch(FetchDescriptor<Workout>())
#expect(all.count == 1) // updated in place, not duplicated
let fetched = try #require(CacheMapper.fetchWorkout(id: "01WKEDIT", in: container.mainContext))
#expect(fetched.splitName == "Push Day")
#expect(fetched.routineName == "Push Day")
#expect(fetched.status == .completed)
#expect(fetched.logsArray.first?.exerciseName == "Incline Press")
}
@@ -72,7 +72,7 @@ struct WatchConnectivityBridgeTests {
let bridge = try WatchConnectivityBridge(container: makeContainer())
#expect(bridge.lastSyncDate == nil)
#expect(bridge.editingWorkoutID == nil)
#expect(bridge.editingSplitID == nil)
#expect(bridge.editingRoutineID == nil)
#expect(bridge.liveIncoming == nil)
#expect(bridge.presentable == nil)
#expect(bridge.navigatedRunID == nil)