Review fixes: reindex activeBoardIndex; safer live-input rebuild

- moveBoard/removeBoard now keep activeBoardIndex pointing at the soloed board
  (track by identity on move; shift down or clear on remove), so the Solo
  highlight no longer lights the wrong cell after reorder or delete.
- rebuildWithLiveInput snapshots the rig before any teardown and aborts if the
  snapshot fails, instead of tearing down then seeding a fallback (no silent
  rig loss). The engine-already-has-input early return now also persists the
  live-input selection, via a shared selectLiveInputAndPersist helper.
- Test: activeBoardIndex tracks reorder and removal.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-23 21:22:44 -07:00
parent 652a5a76ca
commit 6ad813601f
3 changed files with 84 additions and 12 deletions
@@ -124,12 +124,22 @@ final class ShoeGazeBuilderiOSAppModel {
/// selects live input on channel 0. No-op if the engine already has an input channel.
private func rebuildWithLiveInput() {
guard engine.numInputChannels == 0 else {
try? shoeGaze.setInputSource(.liveInput(channel: 0))
// Engine already has an input channel: just select live input, no rebuild.
selectLiveInputAndPersist()
return
}
let wasRunning: Bool = engine.isRunning
let snapshot: Data? = try? shoeGaze.rigData()
// Snapshot the rig BEFORE any teardown. If this fails, abort the rebuild rather than
// tear the engine down and lose the rig — the user stays on the current engine.
let snapshot: Data
do {
snapshot = try shoeGaze.rigData()
} catch {
print("[ShoeGazeBuilderiOS] rig snapshot failed; aborting live-input rebuild: \(error)")
return
}
engine.stop()
shoeGaze.teardown()
@@ -141,17 +151,21 @@ final class ShoeGazeBuilderiOSAppModel {
self.iosDriver = rebuilt.driver
self.shoeGaze = rebuilt.shoeGaze
if let snapshot {
do {
try shoeGaze.loadRig(from: snapshot)
} catch {
print("[ShoeGazeBuilderiOS] rig restore after rebuild failed: \(error)")
seedFallbackBoard()
}
} else {
do {
// Teardown already happened, so a restore failure can only recover by seeding.
try shoeGaze.loadRig(from: snapshot)
} catch {
print("[ShoeGazeBuilderiOS] rig restore after rebuild failed: \(error)")
seedFallbackBoard()
}
selectLiveInputAndPersist()
if wasRunning { try? engine.start() }
}
/// Selects live input on channel 0 and persists the choice for restore-on-relaunch.
private func selectLiveInputAndPersist() {
do {
try shoeGaze.setInputSource(.liveInput(channel: 0))
UserDefaults.standard.set(
@@ -161,8 +175,6 @@ final class ShoeGazeBuilderiOSAppModel {
} catch {
print("[ShoeGazeBuilderiOS] selecting live input failed: \(error)")
}
if wasRunning { try? engine.start() }
}
/// Seeds a single default board so the rig is never empty after a failed restore.
+17
View File
@@ -345,6 +345,16 @@ public final class ShoeGaze {
// Remove the pedals and release the board's buses.
board.teardown()
boards.remove(at: index)
// Keep the soloed-board index valid: clear it if the soloed board was removed,
// otherwise shift it down when a board before it was removed.
if let active: Int = activeBoardIndex {
if active == index {
activeBoardIndex = nil
} else if active > index {
activeBoardIndex = active - 1
}
}
}
// MARK: - Chain router
@@ -395,9 +405,16 @@ public final class ShoeGaze {
/// so it is always a valid insertion point.
public func moveBoard(_ board: PedalBoard, to index: Int) {
guard let sourceIndex: Int = boards.firstIndex(where: { $0 === board }) else { return }
// Track the soloed board by identity so its index follows the reorder.
let soloedBoard: PedalBoard? = activeBoardIndex.flatMap { idx in
idx < boards.count ? boards[idx] : nil
}
let removed: PedalBoard = boards.remove(at: sourceIndex)
let destination: Int = min(index, boards.count)
boards.insert(removed, at: destination)
if let soloedBoard {
activeBoardIndex = boards.firstIndex(where: { $0 === soloedBoard })
}
}
// MARK: - Teardown
@@ -86,6 +86,49 @@ struct ShoeGazeMultiBoardTests {
}
}
@Test func activeBoardIndexTracksReorderAndRemoval() throws {
let renderDuration: Double = 0.05
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
numAudioBuses: 64
)
defer { try? FileManager.default.removeItem(at: url) }
let shoeGaze: ShoeGaze = ShoeGaze(engine: engine)
let board1: PedalBoard = try shoeGaze.addBoard()
try board1.add(OverdrivePedal())
try shoeGaze.finalizeBoard(board1)
let board2: PedalBoard = try shoeGaze.addBoard()
try board2.add(DelayPedal())
try shoeGaze.finalizeBoard(board2)
let board3: PedalBoard = try shoeGaze.addBoard()
try board3.add(OverdrivePedal())
try shoeGaze.finalizeBoard(board3)
// Solo board3 (index 2), then move it to the front — index must follow the board.
shoeGaze.soloBoard(board3, crossfadeSeconds: 0.0)
#expect(shoeGaze.activeBoardIndex == 2)
shoeGaze.moveBoard(board3, to: 0)
#expect(shoeGaze.boards.first === board3)
#expect(shoeGaze.activeBoardIndex == 0)
// Now order is [board3, board1, board2], soloed index 0. Removing a later board
// leaves the soloed index unchanged.
shoeGaze.removeBoard(board2)
#expect(shoeGaze.activeBoardIndex == 0)
// Re-solo board1 (now index 1), then remove the earlier board3 (index 0):
// the soloed index shifts down to 0.
shoeGaze.soloBoard(board1, crossfadeSeconds: 0.0)
#expect(shoeGaze.activeBoardIndex == 1)
shoeGaze.removeBoard(board3)
#expect(shoeGaze.activeBoardIndex == 0)
// Removing the soloed board clears the index.
shoeGaze.removeBoard(board1)
#expect(shoeGaze.activeBoardIndex == nil)
}
@Test func soloBlendAndAllControlBoardAudibility() throws {
let renderDuration: Double = 0.05
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(