From bb418ca2e8198a35c477bd4e270e8daafff9625c Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Sun, 9 Aug 2026 12:42:03 -0700 Subject: [PATCH] feat(rigs): confirm before overwriting a built-in factory rig MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Saving over a currently-loaded factory rig now shows a confirmation dialog instead of silently replacing the shipped original: Overwrite (destructive), Save as New Rig… (new id, keeps the original), or Cancel. The pending follow-up (e.g. the Save & Clear clear step) is threaded through all three paths. Adds FactoryRigLibrary.factoryIDs / isFactory(_:) so the UI can tell a shipped rig from a user rig by id, with a test. Co-Authored-By: Claude Opus 4.8 --- .../Presets/Factory/FactoryRigLibrary.swift | 15 ++++++ .../ShoeGazeBuilderView.swift | 51 ++++++++++++++++++- .../FactoryRigLibraryTests.swift | 10 ++++ 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/Sources/LyrebirdShoeGaze/Presets/Factory/FactoryRigLibrary.swift b/Sources/LyrebirdShoeGaze/Presets/Factory/FactoryRigLibrary.swift index 3cb1942..97b3233 100644 --- a/Sources/LyrebirdShoeGaze/Presets/Factory/FactoryRigLibrary.swift +++ b/Sources/LyrebirdShoeGaze/Presets/Factory/FactoryRigLibrary.swift @@ -48,6 +48,21 @@ extension PedalBoard { @MainActor public enum FactoryRigLibrary { + /// Stable ids of every built-in rig, for detecting whether a library entry is a factory rig. + /// + /// Because factory rigs are seeded with their fixed ``FactoryRig/id``, an entry recalled from the + /// library can be identified as shipped-vs-user by id membership here. Callers use this to guard + /// destructive actions (e.g. warn before overwriting a starter rig). + public static let factoryIDs: Set = Set(all.map { $0.id }) + + /// Whether `id` belongs to a shipped factory rig. + /// + /// - Parameter id: A ``RigEntry/id`` from the rig library. + /// - Returns: `true` if the entry is one of the built-in starter rigs. + public static func isFactory(_ id: UUID) -> Bool { + factoryIDs.contains(id) + } + /// Every built-in rig, in the order a picker should present them. public static let all: [FactoryRig] = [ glideWall, diff --git a/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift b/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift index 52fb870..f272d4c 100644 --- a/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift +++ b/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift @@ -149,6 +149,18 @@ public struct ShoeGazeBuilderView: View { let message: String } + /// A pending confirmation to overwrite a built-in factory rig. Non-`nil` shows the dialog; + /// carries the target entry and the follow-up to run once the user resolves the choice (so the + /// "Save & Clear" flow still clears after a confirmed overwrite). + private struct FactoryOverwriteRequest: Identifiable { + let id: UUID = UUID() + let entry: RigEntry + let completion: () -> Void + } + + /// Drives the "overwrite a starter rig?" confirmation dialog. `nil` = not shown. + @State private var factoryOverwrite: FactoryOverwriteRequest? + /// Archive export state. Cross-platform: backs the sidebar's Export Archive and the /// "Save & Clear" flow is separate (it saves to the library, not a file). @State private var showExporter: Bool = false @@ -304,6 +316,32 @@ public struct ShoeGazeBuilderView: View { .background(rigImportAnchor) .background(actionErrorAnchor) .rigNamePrompt($namePrompt) + .confirmationDialog( + "Overwrite starter rig?", + isPresented: Binding( + get: { factoryOverwrite != nil }, + set: { presented in if !presented { factoryOverwrite = nil } } + ), + titleVisibility: .visible, + presenting: factoryOverwrite + ) { request in + Button("Overwrite", role: .destructive) { + persist( + shoeGaze.captureEntry(name: request.entry.name, id: request.entry.id), + then: request.completion + ) + } + Button("Save as New Rig…") { + // Defer one runloop hop: presenting the name-entry alert while the confirmation + // dialog is still dismissing can drop the new presentation on some SwiftUI versions. + let completion: () -> Void = request.completion + DispatchQueue.main.async { promptSaveAs(then: completion) } + } + Button("Cancel", role: .cancel) { } + } message: { request in + Text("“\(request.entry.name)” is a built-in starter rig. Overwriting replaces it in " + + "your library. Save as a new rig to keep the original.") + } .confirmationDialog( "Erase all rigs?", isPresented: $showEraseAllDialog, @@ -1649,6 +1687,12 @@ public struct ShoeGazeBuilderView: View { } return } + // Overwriting a built-in starter rig replaces it in the library. Confirm first, offering a + // non-destructive "Save as New Rig" so the shipped original can be kept. + if FactoryRigLibrary.isFactory(existing.id) { + factoryOverwrite = FactoryOverwriteRequest(entry: existing, completion: completion) + return + } persist(shoeGaze.captureEntry(name: existing.name, id: id), then: completion) } @@ -1664,13 +1708,16 @@ public struct ShoeGazeBuilderView: View { } /// Prompts for a name and saves the current session as a new library entry (new id). - private func promptSaveAs() { + /// + /// - Parameter completion: Follow-up run once the new entry is persisted (e.g. the "Save & Clear" + /// clear step, or the pending action from a factory-rig overwrite prompt). + private func promptSaveAs(then completion: @escaping () -> Void = { }) { namePrompt = RigNamePrompt( title: "Save Rig As", initialText: currentRigName() ?? defaultNewRigName(), confirmLabel: "Save" ) { name in - persist(shoeGaze.captureEntry(name: name)) + persist(shoeGaze.captureEntry(name: name), then: completion) } } diff --git a/Tests/LyrebirdShoeGazeTests/FactoryRigLibraryTests.swift b/Tests/LyrebirdShoeGazeTests/FactoryRigLibraryTests.swift index 650a1e6..9faa750 100644 --- a/Tests/LyrebirdShoeGazeTests/FactoryRigLibraryTests.swift +++ b/Tests/LyrebirdShoeGazeTests/FactoryRigLibraryTests.swift @@ -51,6 +51,16 @@ struct FactoryRigLibraryTests { } } + @Test func factoryDetectionRecognisesEveryRigAndRejectsOthers() { + let rigs: [FactoryRig] = FactoryRigLibrary.all + #expect(FactoryRigLibrary.factoryIDs.count == rigs.count) + for rig: FactoryRig in rigs { + #expect(FactoryRigLibrary.isFactory(rig.id), "\(rig.name) not recognised as factory") + } + // A freshly generated id is not a factory rig. + #expect(!FactoryRigLibrary.isFactory(UUID())) + } + // MARK: - Seeding entries @Test func makeEntriesMatchesCatalogAndIsStable() throws {