feat(rigs): confirm before overwriting a built-in factory rig
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 <noreply@anthropic.com>
This commit is contained in:
@@ -48,6 +48,21 @@ extension PedalBoard {
|
|||||||
@MainActor
|
@MainActor
|
||||||
public enum FactoryRigLibrary {
|
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<UUID> = 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.
|
/// Every built-in rig, in the order a picker should present them.
|
||||||
public static let all: [FactoryRig] = [
|
public static let all: [FactoryRig] = [
|
||||||
glideWall,
|
glideWall,
|
||||||
|
|||||||
@@ -149,6 +149,18 @@ public struct ShoeGazeBuilderView: View {
|
|||||||
let message: String
|
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
|
/// 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).
|
/// "Save & Clear" flow is separate (it saves to the library, not a file).
|
||||||
@State private var showExporter: Bool = false
|
@State private var showExporter: Bool = false
|
||||||
@@ -304,6 +316,32 @@ public struct ShoeGazeBuilderView: View {
|
|||||||
.background(rigImportAnchor)
|
.background(rigImportAnchor)
|
||||||
.background(actionErrorAnchor)
|
.background(actionErrorAnchor)
|
||||||
.rigNamePrompt($namePrompt)
|
.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(
|
.confirmationDialog(
|
||||||
"Erase all rigs?",
|
"Erase all rigs?",
|
||||||
isPresented: $showEraseAllDialog,
|
isPresented: $showEraseAllDialog,
|
||||||
@@ -1649,6 +1687,12 @@ public struct ShoeGazeBuilderView: View {
|
|||||||
}
|
}
|
||||||
return
|
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)
|
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).
|
/// 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(
|
namePrompt = RigNamePrompt(
|
||||||
title: "Save Rig As",
|
title: "Save Rig As",
|
||||||
initialText: currentRigName() ?? defaultNewRigName(),
|
initialText: currentRigName() ?? defaultNewRigName(),
|
||||||
confirmLabel: "Save"
|
confirmLabel: "Save"
|
||||||
) { name in
|
) { name in
|
||||||
persist(shoeGaze.captureEntry(name: name))
|
persist(shoeGaze.captureEntry(name: name), then: completion)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
// MARK: - Seeding entries
|
||||||
|
|
||||||
@Test func makeEntriesMatchesCatalogAndIsStable() throws {
|
@Test func makeEntriesMatchesCatalogAndIsStable() throws {
|
||||||
|
|||||||
Reference in New Issue
Block a user