Merge watch workout pushes per log instead of whole-document
ingestFromWatch arbitrated by whole-document updatedAt, so concurrent edits to the same workout on both devices (phone edits exercise A while the watch completes exercise B) lost one side wholesale — the newer snapshot replaced the other (H1). Reconcile per log instead. WorkoutMergePlanner (pure, deterministic) unions logs by id, resolves each by newest per-log updatedAt, and applies phone-authored deletion tombstones so an absent log is never ambiguous between "deleted on the phone" and "just added on the watch". Edits to different exercises now commute — delivery order and offline gaps stop mattering. A stale/duplicate push merges back to exactly the cached doc, so ingest re-pushes authoritative state rather than writing. The per-log updatedAt scaffolding shipped (unused) in schema v4; it's now stamped by transition(to:) on status flips and a new touch() at the content-only edit sites (order, notes, machine settings, adjusted entries, new logs) on both phone and watch. deletedLogIDs is new: additive on the wire and cache, phone-authored (deleteLog), pruned after a 30-day grace. Because an older build rewriting a file would strip the tombstones and resurrect a deleted exercise, WorkoutDocument schema bumps 4->5 (forward gate quarantines old builds) and the cache bumps 5->6. recomputeStatusFromLogs takes an injectable now: so the merge recomputes status/end deterministically. WorkoutMergePlannerTests pins the decision table (commute, per-log newer-wins, legacy-nil, watch-add, tombstone honored/resurrect/union/prune, status recompute, no-op re-push); WorkoutDocumentMapperTests gains the deletedLogIDs round-trip.
This commit is contained in:
@@ -605,6 +605,7 @@ struct ExerciseProgressView: View {
|
||||
guard let i = doc.logs.firstIndex(where: { $0.id == logID }),
|
||||
let last = doc.logs[i].setEntries?.indices.last else { return }
|
||||
doc.logs[i].setEntries?[last] = entry
|
||||
doc.logs[i].touch()
|
||||
doc.updatedAt = Date()
|
||||
onChange()
|
||||
}
|
||||
|
||||
@@ -191,6 +191,7 @@ struct ExerciseView: View {
|
||||
private func applyMachineSettings(_ settings: [MachineSetting]) {
|
||||
guard let i = doc.logs.firstIndex(where: { $0.id == logID }) else { return }
|
||||
doc.logs[i].machineSettings = settings
|
||||
doc.logs[i].touch()
|
||||
doc.updatedAt = Date()
|
||||
let snapshot = doc
|
||||
let exerciseName = doc.logs[i].exerciseName
|
||||
|
||||
@@ -51,6 +51,7 @@ struct NotesEditView: View {
|
||||
var doc = WorkoutDocument(from: workout)
|
||||
guard let i = doc.logs.firstIndex(where: { $0.id == logID }) else { return }
|
||||
doc.logs[i].notes = notesText
|
||||
doc.logs[i].touch()
|
||||
doc.updatedAt = Date()
|
||||
let snapshot = doc
|
||||
Task { await sync.save(workout: snapshot) }
|
||||
|
||||
@@ -158,6 +158,7 @@ struct PlanEditView: View {
|
||||
doc.logs[i].weight = Double(weight)
|
||||
doc.logs[i].durationSeconds = totalSeconds
|
||||
doc.logs[i].loadType = selectedLoadType.rawValue
|
||||
doc.logs[i].touch()
|
||||
doc.updatedAt = Date()
|
||||
let exerciseName = doc.logs[i].exerciseName
|
||||
let workoutDoc = doc
|
||||
|
||||
@@ -294,6 +294,9 @@ struct WorkoutLogListView: View {
|
||||
private func deleteLog(_ log: WorkoutLogDocument) {
|
||||
withAnimation {
|
||||
doc.logs.removeAll { $0.id == log.id }
|
||||
// Tombstone the deletion so a stale watch push still carrying this log can't
|
||||
// resurrect it — the per-log merge drops a tombstoned id (see WorkoutMergePlanner).
|
||||
doc.deletedLogIDs = (doc.deletedLogIDs ?? [:]).merging([log.id: Date()]) { _, new in new }
|
||||
save()
|
||||
}
|
||||
}
|
||||
@@ -302,8 +305,9 @@ struct WorkoutLogListView: View {
|
||||
var logs = sortedLogs
|
||||
logs.move(fromOffsets: source, toOffset: destination)
|
||||
for (index, log) in logs.enumerated() {
|
||||
if let i = doc.logs.firstIndex(where: { $0.id == log.id }) {
|
||||
if let i = doc.logs.firstIndex(where: { $0.id == log.id }), doc.logs[i].order != index {
|
||||
doc.logs[i].order = index
|
||||
doc.logs[i].touch()
|
||||
}
|
||||
}
|
||||
save()
|
||||
@@ -333,7 +337,8 @@ struct WorkoutLogListView: View {
|
||||
}
|
||||
doc.end = nil
|
||||
|
||||
let newLog = WorkoutLogDocument(planFrom: plan, order: doc.logs.count, date: now)
|
||||
var newLog = WorkoutLogDocument(planFrom: plan, order: doc.logs.count, date: now)
|
||||
newLog.touch(now) // a fresh log carries a definite creation time for the merge
|
||||
doc.logs.append(newLog)
|
||||
save()
|
||||
|
||||
@@ -389,6 +394,7 @@ struct WorkoutLogListView: View {
|
||||
private func applyMachineSettings(_ settings: [MachineSetting], toLogID id: String) {
|
||||
guard let i = doc.logs.firstIndex(where: { $0.id == id }) else { return }
|
||||
doc.logs[i].machineSettings = settings
|
||||
doc.logs[i].touch()
|
||||
doc.recomputeStatusFromLogs()
|
||||
doc.updatedAt = Date()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user