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.
60 lines
1.6 KiB
Swift
60 lines
1.6 KiB
Swift
//
|
|
// NotesEditView.swift
|
|
// Workouts
|
|
//
|
|
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
|
|
//
|
|
|
|
import SwiftUI
|
|
import SwiftData
|
|
|
|
struct NotesEditView: View {
|
|
@Environment(SyncEngine.self) private var sync
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
let workout: Workout
|
|
let logID: String
|
|
|
|
@State private var notesText: String = ""
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
Form {
|
|
Section {
|
|
TextEditor(text: $notesText)
|
|
.frame(minHeight: 200)
|
|
}
|
|
}
|
|
.navigationTitle("Edit Notes")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbar {
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
Button("Cancel") {
|
|
dismiss()
|
|
}
|
|
}
|
|
ToolbarItem(placement: .confirmationAction) {
|
|
Button("Save") {
|
|
saveChanges()
|
|
dismiss()
|
|
}
|
|
}
|
|
}
|
|
.onAppear {
|
|
notesText = WorkoutDocument(from: workout)
|
|
.logs.first(where: { $0.id == logID })?.notes ?? ""
|
|
}
|
|
}
|
|
}
|
|
|
|
private func saveChanges() {
|
|
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) }
|
|
}
|
|
}
|