Persist the source rig id inside the autosave; single source of truth
The rig-highlight/graph mismatch kept returning because the highlight (a UserDefault) and the working graph (the autosave file) were two separate stores that could diverge — and once an earlier build failed to load and autosaved a fallback Default over the real graph, the divergence was permanent. Fix the class of bug: the loaded rig's id now lives on ShoeGaze.currentRigID and is written *inside* the autosave envelope alongside the graph (one atomic file). RigStore.restore sets it from the envelope (nil for pre-envelope files or a fallback seed); the sidebar highlight reads it directly. The separate UserDefault key, restoreCurrentRig, and persistCurrentRigID are removed. When the app falls back to seeding Default, no rig is highlighted — honest, and it can never mislabel the graph again. 339 tests pass; both app targets build. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -56,9 +56,6 @@ public struct ShoeGazeBuilderView: View {
|
||||
private static let testLoopTag: Int = -1
|
||||
/// UserDefaults key persisting the last input selection (`-1` = test loop, `>= 0` = channel).
|
||||
private static let inputSelectionDefaultsKey: String = "ShoeGaze.inputSelection"
|
||||
/// UserDefaults key persisting the loaded rig's id (its `uuidString`) so the same rig can be
|
||||
/// highlighted on the next launch. Read by the app shells to seed `initialRigID`.
|
||||
public static let currentRigDefaultsKey: String = "ShoeGaze.currentRigID"
|
||||
/// Maximum crossfade duration honoured by board-switch keyboard shortcuts.
|
||||
private static let maxCrossfadeSeconds: Double = 10.0
|
||||
/// Highest board reachable via a ⌘-number switch shortcut.
|
||||
@@ -116,10 +113,14 @@ public struct ShoeGazeBuilderView: View {
|
||||
|
||||
/// The library entry currently loaded into the session, or `nil` for an unsaved/new rig. Drives
|
||||
/// the sidebar highlight and decides whether Save overwrites or prompts for a name.
|
||||
@State private var currentRigID: UUID?
|
||||
|
||||
/// Guards the one-time `currentRigID` seed from `initialRigID` on appear.
|
||||
@State private var didRestoreRig: Bool = false
|
||||
///
|
||||
/// Backed by ``ShoeGaze/currentRigID`` (the single source of truth), so it is persisted inside
|
||||
/// the autosave envelope together with the graph and restored consistently — the highlight can
|
||||
/// never drift from the loaded rig. `@Observable` drives sidebar updates when it changes.
|
||||
private var currentRigID: UUID? {
|
||||
get { shoeGaze.currentRigID }
|
||||
nonmutating set { shoeGaze.currentRigID = newValue }
|
||||
}
|
||||
|
||||
/// Drives the shared rig-name entry alert (New / Save As / Rename / Duplicate).
|
||||
@State private var namePrompt: RigNamePrompt?
|
||||
@@ -220,17 +221,14 @@ public struct ShoeGazeBuilderView: View {
|
||||
.background(boardSwitchShortcuts)
|
||||
.onAppear {
|
||||
restoreInputSelection()
|
||||
restoreCurrentRig()
|
||||
syncIdleTimer()
|
||||
maybeWarnICloudUnavailable()
|
||||
}
|
||||
.onDisappear { releaseIdleTimer() }
|
||||
.onChange(of: transportTick) { syncIdleTimer() }
|
||||
.onChange(of: library.isLoaded) {
|
||||
restoreCurrentRig()
|
||||
maybeWarnICloudUnavailable()
|
||||
}
|
||||
.onChange(of: currentRigID) { persistCurrentRigID() }
|
||||
.background(boardsSheetAnchor)
|
||||
.background(rigExportAnchor)
|
||||
.background(rigImportAnchor)
|
||||
@@ -333,7 +331,7 @@ public struct ShoeGazeBuilderView: View {
|
||||
private var rigSidebar: some View {
|
||||
RigLibrarySidebarView(
|
||||
store: library,
|
||||
currentRigID: $currentRigID,
|
||||
currentRigID: Binding(get: { shoeGaze.currentRigID }, set: { shoeGaze.currentRigID = $0 }),
|
||||
onSelect: { entry in requestLoad(entry) },
|
||||
onNew: { promptNewRig() },
|
||||
onSave: { saveCurrentRig() },
|
||||
@@ -718,10 +716,9 @@ public struct ShoeGazeBuilderView: View {
|
||||
/// Persists the current rig to the autosave file. Called after structural edits so state
|
||||
/// survives a restart even without an explicit Save; the app shells also save on background.
|
||||
private func autosave() {
|
||||
// The autosave envelope records `shoeGaze.currentRigID` alongside the graph, so the highlight
|
||||
// and the restored graph stay consistent without any separate bookkeeping.
|
||||
RigStore.save(shoeGaze)
|
||||
// Pair the current-rig id with every graph write so the persisted highlight can never drift
|
||||
// from the autosaved graph (the source of the highlight/graph mismatch on restart).
|
||||
persistCurrentRigID()
|
||||
}
|
||||
|
||||
/// Removes every board, then seeds one fresh empty board so the builder is never boardless.
|
||||
@@ -897,33 +894,6 @@ public struct ShoeGazeBuilderView: View {
|
||||
|
||||
// MARK: - Rig library
|
||||
|
||||
/// Highlights the rig the working session derives from, once the library has loaded. Runs once.
|
||||
///
|
||||
/// The current rig id is written paired with every autosave (see ``autosave()``), so the stored
|
||||
/// id always tracks the loaded graph — including after edits such as learning a MIDI binding,
|
||||
/// which change the graph but not which saved rig it came from. Only highlights when nothing is
|
||||
/// selected yet and the saved id still exists in the library.
|
||||
private func restoreCurrentRig() {
|
||||
guard !didRestoreRig, library.isLoaded else { return }
|
||||
didRestoreRig = true
|
||||
guard currentRigID == nil,
|
||||
let stored: String = UserDefaults.standard.string(
|
||||
forKey: ShoeGazeBuilderView.currentRigDefaultsKey),
|
||||
let id: UUID = UUID(uuidString: stored),
|
||||
library.entries.contains(where: { $0.id == id }) else { return }
|
||||
currentRigID = id
|
||||
}
|
||||
|
||||
/// Persists `currentRigID` so the same rig is highlighted on the next launch.
|
||||
private func persistCurrentRigID() {
|
||||
let defaults: UserDefaults = UserDefaults.standard
|
||||
if let id: UUID = currentRigID {
|
||||
defaults.set(id.uuidString, forKey: ShoeGazeBuilderView.currentRigDefaultsKey)
|
||||
} else {
|
||||
defaults.removeObject(forKey: ShoeGazeBuilderView.currentRigDefaultsKey)
|
||||
}
|
||||
}
|
||||
|
||||
/// Loads a library entry into the session, replacing the current rig. Stops/restarts the engine
|
||||
/// around the rebuild, resets the board selection, and records the entry as current.
|
||||
private func loadEntry(_ entry: RigEntry) {
|
||||
|
||||
Reference in New Issue
Block a user