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>
This commit is contained in:
@@ -70,10 +70,11 @@ extension ChainRouter {
|
|||||||
@MainActor
|
@MainActor
|
||||||
public enum FactoryRigLibrary {
|
public enum FactoryRigLibrary {
|
||||||
|
|
||||||
/// Catalog seed version. Bump this whenever new factory rigs are added so first-run catch-up
|
/// Catalog seed version. Bump this whenever the shipped starter rigs change — new rigs added, or
|
||||||
/// seeding re-runs on already-populated libraries and installs the newly-shipped starters. Never
|
/// an existing rig's definition corrected — so launch catch-up re-runs on already-populated
|
||||||
/// re-adds a rig the user has since deleted at the *same* version — the marker only advances.
|
/// libraries and refreshes every starter to its current form. Within a single version the marker
|
||||||
public static let seedVersion: Int = 1
|
/// only advances once, so a starter the user deletes stays deleted until the next bump.
|
||||||
|
public static let seedVersion: Int = 2
|
||||||
|
|
||||||
/// Stable ids of every built-in rig, for detecting whether a library entry is a factory rig.
|
/// Stable ids of every built-in rig, for detecting whether a library entry is a factory rig.
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -27,15 +27,16 @@ public enum RigLibraryBootstrap {
|
|||||||
RigLibraryStore()
|
RigLibraryStore()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolves + loads `store` off the main thread, then performs a one-time catch-up seed of any
|
/// Resolves + loads `store` off the main thread, then performs a one-time catch-up install of the
|
||||||
/// missing shipped starter rigs. Idempotent — safe to call once at every launch.
|
/// shipped starter rigs. Idempotent — safe to call once at every launch.
|
||||||
///
|
///
|
||||||
/// Seeding adds only factory rigs whose stable ids are **not already present**, so it never
|
/// Installing (re)saves every factory rig by its stable id, so it **adds** missing starters and
|
||||||
/// duplicates a rig and never touches the user's own rigs. It runs at most once per
|
/// **refreshes** existing ones to their current shipped definition (a corrected rig replaces the
|
||||||
/// ``FactoryRigLibrary/seedVersion``: a first launch seeds every starter; a launch after an
|
/// stale copy in place, keeping its library position). User rigs are never touched — factory rigs
|
||||||
/// update that ships new starters seeds just the new ones; and a starter the user later deletes
|
/// have their own fixed ids. It runs at most once per ``FactoryRigLibrary/seedVersion``: a first
|
||||||
/// stays deleted (the version marker has already advanced). This deliberately replaces the old
|
/// launch installs every starter; a launch after a version bump refreshes/adds; and within a
|
||||||
/// "only seed an empty library" rule so users who already had rigs still receive the starters.
|
/// 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:
|
/// - Parameters:
|
||||||
/// - store: The store from ``makeStore()``.
|
/// - store: The store from ``makeStore()``.
|
||||||
@@ -51,60 +52,43 @@ public enum RigLibraryBootstrap {
|
|||||||
let seededVersion: Int = defaults.integer(forKey: seedVersionKey) // 0 when never seeded
|
let seededVersion: Int = defaults.integer(forKey: seedVersionKey) // 0 when never seeded
|
||||||
guard seededVersion < FactoryRigLibrary.seedVersion else { return }
|
guard seededVersion < FactoryRigLibrary.seedVersion else { return }
|
||||||
|
|
||||||
// Seed missing starters. Only advance the marker if seeding succeeded, so a transient failure
|
// 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.
|
// (which should not happen for this deterministic data) is retried on the next launch.
|
||||||
if seedMissingFactoryRigs(into: store) {
|
if installFactoryRigs(into: store) != nil {
|
||||||
defaults.set(FactoryRigLibrary.seedVersion, forKey: seedVersionKey)
|
defaults.set(FactoryRigLibrary.seedVersion, forKey: seedVersionKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds any shipped starter rig not already in `store`, keyed by stable id. Safe to call anytime.
|
/// (Re)installs every shipped starter rig to its current definition. Safe to call anytime.
|
||||||
///
|
///
|
||||||
/// Use this to drive a user-facing "Restore Starter Rigs" action: it re-adds starters the user
|
/// Drives the user-facing "Restore Starter Rigs" action: it re-adds starters the user deleted,
|
||||||
/// deleted (and any newly-shipped ones) without disturbing their own rigs, and advances the seed
|
/// adds newly-shipped ones, and refreshes any stale copies — all without disturbing the user's own
|
||||||
/// marker so automatic catch-up seeding won't fight the manual restore.
|
/// rigs — and advances the seed marker so automatic catch-up won't fight the manual restore.
|
||||||
///
|
///
|
||||||
/// - Parameter store: The library to seed into.
|
/// - Parameter store: The library to install into.
|
||||||
/// - Returns: The number of starter rigs added (0 if all were already present).
|
/// - Returns: The number of starter rigs written (0 on a build/encode failure).
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public static func restoreStarterRigs(into store: RigLibraryStore) -> Int {
|
public static func restoreStarterRigs(into store: RigLibraryStore) -> Int {
|
||||||
let added: Int = addMissingFactoryRigs(into: store)
|
let written: Int = installFactoryRigs(into: store) ?? 0
|
||||||
UserDefaults.standard.set(FactoryRigLibrary.seedVersion, forKey: seedVersionKey)
|
UserDefaults.standard.set(FactoryRigLibrary.seedVersion, forKey: seedVersionKey)
|
||||||
return added
|
return written
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Seeds missing starters, returning whether the operation completed without a build/encode error
|
/// Builds every starter rig and saves it, overwriting any existing entry with the same stable id
|
||||||
/// (so the caller knows whether to advance the seed marker).
|
/// (`store.save` upserts by id, keeping an overwritten entry's library position).
|
||||||
private static func seedMissingFactoryRigs(into store: RigLibraryStore) -> Bool {
|
|
||||||
do {
|
|
||||||
_ = try addMissingFactoryRigsThrowing(into: store)
|
|
||||||
return true
|
|
||||||
} catch {
|
|
||||||
print("[RigLibraryBootstrap] seeding factory rigs failed: \(error)")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Non-throwing convenience: seeds missing starters and returns the count added (0 on error).
|
|
||||||
private static func addMissingFactoryRigs(into store: RigLibraryStore) -> Int {
|
|
||||||
do {
|
|
||||||
return try addMissingFactoryRigsThrowing(into: store)
|
|
||||||
} catch {
|
|
||||||
print("[RigLibraryBootstrap] restoring factory rigs failed: \(error)")
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Builds the starter rigs and saves the ones whose ids are missing from `store`.
|
|
||||||
///
|
///
|
||||||
/// - Returns: The number of rigs added.
|
/// - Parameter store: The library to install into.
|
||||||
/// - Throws: Any error from building the rigs or a synchronous encode failure in `store.save`.
|
/// - Returns: The number of rigs written, or `nil` if building/encoding failed.
|
||||||
private static func addMissingFactoryRigsThrowing(into store: RigLibraryStore) throws -> Int {
|
private static func installFactoryRigs(into store: RigLibraryStore) -> Int? {
|
||||||
let existingIDs: Set<UUID> = Set(store.entries.map { $0.id })
|
do {
|
||||||
let missing: [RigEntry] = try FactoryRigLibrary.makeEntries().filter { !existingIDs.contains($0.id) }
|
let entries: [RigEntry] = try FactoryRigLibrary.makeEntries()
|
||||||
for entry: RigEntry in missing {
|
for entry: RigEntry in entries {
|
||||||
try store.save(entry)
|
try store.save(entry)
|
||||||
|
}
|
||||||
|
return entries.count
|
||||||
|
} catch {
|
||||||
|
print("[RigLibraryBootstrap] installing factory rigs failed: \(error)")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return missing.count
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,6 +77,18 @@ struct RigLibrarySidebarView: View {
|
|||||||
actionBar
|
actionBar
|
||||||
}
|
}
|
||||||
.frame(width: RigLibrarySidebarView.width)
|
.frame(width: RigLibrarySidebarView.width)
|
||||||
|
// Open on the tab that contains the currently-loaded rig, so a session restored on a starter
|
||||||
|
// rig (e.g. Garcia) shows Starters, and one on a user rig shows Mine.
|
||||||
|
.onAppear { tab = tabForCurrentRig() }
|
||||||
|
.onChange(of: currentRigID) { tab = tabForCurrentRig() }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The tab whose group contains the currently-loaded rig: Starters for a factory rig, else Mine.
|
||||||
|
private func tabForCurrentRig() -> RigTab {
|
||||||
|
if let id: UUID = currentRigID, FactoryRigLibrary.isFactory(id) {
|
||||||
|
return .starters
|
||||||
|
}
|
||||||
|
return .mine
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Header
|
// MARK: - Header
|
||||||
|
|||||||
Reference in New Issue
Block a user