From 5decdd8693679c828f22b04ac3f3b8f29bb6a1a8 Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Sun, 9 Aug 2026 17:57:57 -0700 Subject: [PATCH] fix(rigs): refresh stale starters on update; tab follows current rig MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- .../Presets/Factory/FactoryRigLibrary.swift | 9 +- .../RigLibraryBootstrap.swift | 82 ++++++++----------- .../RigLibrarySidebarView.swift | 12 +++ 3 files changed, 50 insertions(+), 53 deletions(-) diff --git a/Sources/LyrebirdShoeGaze/Presets/Factory/FactoryRigLibrary.swift b/Sources/LyrebirdShoeGaze/Presets/Factory/FactoryRigLibrary.swift index 6b7eee3..2eb352c 100644 --- a/Sources/LyrebirdShoeGaze/Presets/Factory/FactoryRigLibrary.swift +++ b/Sources/LyrebirdShoeGaze/Presets/Factory/FactoryRigLibrary.swift @@ -70,10 +70,11 @@ extension ChainRouter { @MainActor public enum FactoryRigLibrary { - /// Catalog seed version. Bump this whenever new factory rigs are added so first-run catch-up - /// seeding re-runs on already-populated libraries and installs the newly-shipped starters. Never - /// re-adds a rig the user has since deleted at the *same* version — the marker only advances. - public static let seedVersion: Int = 1 + /// Catalog seed version. Bump this whenever the shipped starter rigs change — new rigs added, or + /// an existing rig's definition corrected — so launch catch-up re-runs on already-populated + /// libraries and refreshes every starter to its current form. Within a single version the marker + /// 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. /// diff --git a/Sources/LyrebirdShoeGazeUI/RigLibraryBootstrap.swift b/Sources/LyrebirdShoeGazeUI/RigLibraryBootstrap.swift index b48dfc4..f64bf5a 100644 --- a/Sources/LyrebirdShoeGazeUI/RigLibraryBootstrap.swift +++ b/Sources/LyrebirdShoeGazeUI/RigLibraryBootstrap.swift @@ -27,15 +27,16 @@ public enum RigLibraryBootstrap { RigLibraryStore() } - /// Resolves + loads `store` off the main thread, then performs a one-time catch-up seed of any - /// missing shipped starter rigs. Idempotent — safe to call once at every launch. + /// 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. /// - /// Seeding adds only factory rigs whose stable ids are **not already present**, so it never - /// duplicates a rig and never touches the user's own rigs. It runs at most once per - /// ``FactoryRigLibrary/seedVersion``: a first launch seeds every starter; a launch after an - /// update that ships new starters seeds just the new ones; and a starter the user later deletes - /// stays deleted (the version marker has already advanced). This deliberately replaces the old - /// "only seed an empty library" rule so users who already had rigs still receive the starters. + /// 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()``. @@ -51,60 +52,43 @@ public enum RigLibraryBootstrap { let seededVersion: Int = defaults.integer(forKey: seedVersionKey) // 0 when never seeded 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. - if seedMissingFactoryRigs(into: store) { + if installFactoryRigs(into: store) != nil { 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 - /// deleted (and any newly-shipped ones) without disturbing their own rigs, and advances the seed - /// marker so automatic catch-up seeding won't fight the manual restore. + /// 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 seed into. - /// - Returns: The number of starter rigs added (0 if all were already present). + /// - 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 added: Int = addMissingFactoryRigs(into: store) + let written: Int = installFactoryRigs(into: store) ?? 0 UserDefaults.standard.set(FactoryRigLibrary.seedVersion, forKey: seedVersionKey) - return added + return written } - /// Seeds missing starters, returning whether the operation completed without a build/encode error - /// (so the caller knows whether to advance the seed marker). - 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`. + /// 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). /// - /// - Returns: The number of rigs added. - /// - Throws: Any error from building the rigs or a synchronous encode failure in `store.save`. - private static func addMissingFactoryRigsThrowing(into store: RigLibraryStore) throws -> Int { - let existingIDs: Set = Set(store.entries.map { $0.id }) - let missing: [RigEntry] = try FactoryRigLibrary.makeEntries().filter { !existingIDs.contains($0.id) } - for entry: RigEntry in missing { - try store.save(entry) + /// - 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 } - return missing.count } } diff --git a/Sources/LyrebirdShoeGazeUI/RigLibrarySidebarView.swift b/Sources/LyrebirdShoeGazeUI/RigLibrarySidebarView.swift index 09266b0..220c3ca 100644 --- a/Sources/LyrebirdShoeGazeUI/RigLibrarySidebarView.swift +++ b/Sources/LyrebirdShoeGazeUI/RigLibrarySidebarView.swift @@ -77,6 +77,18 @@ struct RigLibrarySidebarView: View { actionBar } .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