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:
@@ -32,12 +32,26 @@ public enum RigStore {
|
||||
return dir.appendingPathComponent(fileName)
|
||||
}
|
||||
|
||||
/// Writes the rig's current state to the autosave file. Returns `false` on any failure.
|
||||
/// The autosave file payload: the working graph plus the id of the rig-library entry it derives
|
||||
/// from, written together so the two can never drift apart.
|
||||
private struct AutosaveEnvelope: Codable {
|
||||
let currentRigID: UUID?
|
||||
let state: ShoeGazeState
|
||||
}
|
||||
|
||||
/// Writes the rig's current state — plus its source-rig id (``ShoeGaze/currentRigID``) — to the
|
||||
/// autosave file. Returns `false` on any failure.
|
||||
@discardableResult
|
||||
public static func save(_ shoeGaze: ShoeGaze) -> Bool {
|
||||
guard let url: URL = autosaveURL else { return false }
|
||||
do {
|
||||
let data: Data = try shoeGaze.rigData()
|
||||
let envelope: AutosaveEnvelope = AutosaveEnvelope(
|
||||
currentRigID: shoeGaze.currentRigID,
|
||||
state: shoeGaze.captureState()
|
||||
)
|
||||
let encoder: JSONEncoder = JSONEncoder()
|
||||
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
|
||||
let data: Data = try encoder.encode(envelope)
|
||||
try data.write(to: url, options: .atomic)
|
||||
return true
|
||||
} catch {
|
||||
@@ -46,7 +60,11 @@ public enum RigStore {
|
||||
}
|
||||
}
|
||||
|
||||
/// Restores a previously autosaved rig into `shoeGaze`, which should have no boards yet.
|
||||
/// Restores a previously autosaved rig into `shoeGaze`, which should have no boards yet, and sets
|
||||
/// ``ShoeGaze/currentRigID`` from the envelope so the highlight matches the restored graph.
|
||||
///
|
||||
/// Decodes the current envelope format; falls back to a bare `ShoeGazeState` (pre-envelope
|
||||
/// autosaves), in which case `currentRigID` is left `nil`.
|
||||
///
|
||||
/// - Returns: `true` if an autosave existed and produced at least one board; `false` if there
|
||||
/// was nothing to restore or the load failed (in which case the caller should seed a default).
|
||||
@@ -56,7 +74,16 @@ public enum RigStore {
|
||||
FileManager.default.fileExists(atPath: url.path) else { return false }
|
||||
do {
|
||||
let data: Data = try Data(contentsOf: url)
|
||||
try shoeGaze.loadRig(from: data)
|
||||
let decoder: JSONDecoder = JSONDecoder()
|
||||
if let envelope: AutosaveEnvelope = try? decoder.decode(AutosaveEnvelope.self, from: data) {
|
||||
try shoeGaze.loadRig(from: envelope.state)
|
||||
shoeGaze.currentRigID = envelope.currentRigID
|
||||
} else {
|
||||
// Pre-envelope autosave: a bare ShoeGazeState with no recorded source rig.
|
||||
let state: ShoeGazeState = try decoder.decode(ShoeGazeState.self, from: data)
|
||||
try shoeGaze.loadRig(from: state)
|
||||
shoeGaze.currentRigID = nil
|
||||
}
|
||||
return !shoeGaze.boards.isEmpty
|
||||
} catch {
|
||||
print("[RigStore] restore failed: \(error)")
|
||||
|
||||
@@ -71,6 +71,12 @@ public final class ShoeGaze {
|
||||
/// Read and mutated by `MIDIController`; survives an input-source switch (same rig).
|
||||
public var midiMap: MIDIMap?
|
||||
|
||||
/// The id of the rig-library entry this working session derives from, or `nil` for an unsaved
|
||||
/// session. Session provenance only — NOT part of a saved/exported rig; it is written into the
|
||||
/// autosave envelope (see `RigStore`) alongside the graph so the sidebar highlight and the
|
||||
/// restored graph can never diverge. The UI mirrors this into the rig-library selection.
|
||||
public var currentRigID: UUID?
|
||||
|
||||
// MARK: - Recording state
|
||||
|
||||
/// The folder into which `startRecording()` writes timestamped WAV files.
|
||||
|
||||
Reference in New Issue
Block a user