Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/RigLibraryBootstrap.swift
T
josh 5decdd8693 fix(rigs): refresh stale starters on update; tab follows current rig
- Seeding now (re)installs every starter by stable id, so a corrected rig
  definition replaces the stale copy already in a library (save upserts by
  id, keeping its position) — user rigs untouched. Bumped seedVersion to 2
  so existing libraries pick up the two-board Garcia rig (the old router
  version showed "No board at index 0" because the builder UI renders boards,
  not routers, and the stale entry was never refreshed). "Restore Starter
  Rigs" likewise refreshes all starters, not just missing ones.
- The Mine/Starters toggle now opens on the group containing the currently
  loaded rig (Starters when a factory rig like Garcia is active, else Mine),
  and follows the selection when the current rig changes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-09 17:57:57 -07:00

95 lines
4.7 KiB
Swift

// RigLibraryBootstrap.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
import LyrebirdShoeGaze
/// Setup of the rig library for an app shell, split so nothing blocks the launch path.
///
/// ``makeStore()`` returns an auto-resolving store instantly (safe to construct in a synchronous
/// initializer and hand to the UI). ``start(_:seedFrom:)`` then does the blocking work — resolving
/// the iCloud-or-local location and the initial load — off the main thread, then performs a one-time
/// catch-up seed of any missing shipped starter rigs.
@MainActor
public enum RigLibraryBootstrap {
/// `UserDefaults` key holding the highest ``FactoryRigLibrary/seedVersion`` this device has
/// already seeded. Per-device (not synced): the seed is idempotent by stable rig id, so a device
/// that receives the rigs via iCloud sync simply finds them already present and seeds nothing.
private static let seedVersionKey: String = "net.realizedsound.shoegaze.factorySeedVersion"
/// Creates an auto-resolving store. Construct this synchronously at launch; call
/// ``start(_:seedFrom:)`` afterwards (e.g. from a SwiftUI `.task` or a `Task`).
public static func makeStore() -> RigLibraryStore {
RigLibraryStore()
}
/// Resolves + loads `store` off the main thread, then performs a one-time catch-up install of the
/// shipped starter rigs. Idempotent — safe to call once at every launch.
///
/// Installing (re)saves every factory rig by its stable id, so it **adds** missing starters and
/// **refreshes** existing ones to their current shipped definition (a corrected rig replaces the
/// stale copy in place, keeping its library position). User rigs are never touched — factory rigs
/// have their own fixed ids. It runs at most once per ``FactoryRigLibrary/seedVersion``: a first
/// launch installs every starter; a launch after a version bump refreshes/adds; and within a
/// version a starter the user deletes stays deleted (the marker has already advanced). This
/// deliberately replaces the old "only seed an empty library" rule so existing users get starters.
///
/// - Parameters:
/// - store: The store from ``makeStore()``.
/// - shoeGaze: The working session (currently unused by seeding; kept for call-site stability
/// and future working-rig seeding needs).
public static func start(
_ store: RigLibraryStore,
seedFrom shoeGaze: ShoeGaze
) async {
await store.start()
let defaults: UserDefaults = .standard
let seededVersion: Int = defaults.integer(forKey: seedVersionKey) // 0 when never seeded
guard seededVersion < FactoryRigLibrary.seedVersion else { return }
// Install the starters. Only advance the marker if it succeeded, so a transient failure
// (which should not happen for this deterministic data) is retried on the next launch.
if installFactoryRigs(into: store) != nil {
defaults.set(FactoryRigLibrary.seedVersion, forKey: seedVersionKey)
}
}
/// (Re)installs every shipped starter rig to its current definition. Safe to call anytime.
///
/// Drives the user-facing "Restore Starter Rigs" action: it re-adds starters the user deleted,
/// adds newly-shipped ones, and refreshes any stale copies — all without disturbing the user's own
/// rigs — and advances the seed marker so automatic catch-up won't fight the manual restore.
///
/// - Parameter store: The library to install into.
/// - Returns: The number of starter rigs written (0 on a build/encode failure).
@discardableResult
public static func restoreStarterRigs(into store: RigLibraryStore) -> Int {
let written: Int = installFactoryRigs(into: store) ?? 0
UserDefaults.standard.set(FactoryRigLibrary.seedVersion, forKey: seedVersionKey)
return written
}
/// Builds every starter rig and saves it, overwriting any existing entry with the same stable id
/// (`store.save` upserts by id, keeping an overwritten entry's library position).
///
/// - Parameter store: The library to install into.
/// - Returns: The number of rigs written, or `nil` if building/encoding failed.
private static func installFactoryRigs(into store: RigLibraryStore) -> Int? {
do {
let entries: [RigEntry] = try FactoryRigLibrary.makeEntries()
for entry: RigEntry in entries {
try store.save(entry)
}
return entries.count
} catch {
print("[RigLibraryBootstrap] installing factory rigs failed: \(error)")
return nil
}
}
}