Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/RigLibraryBootstrap.swift
T
josh dc42db0c59 Add iCloud-synced rig library
Replace the single hand-saved rig file with a managed library of named
rigs persisted in app storage and synced across devices and both builder
apps via a shared iCloud container (iCloud.net.realizedsound.ShoeGazeData),
falling back to local storage when iCloud is unavailable.

Core (LyrebirdShoeGaze/Presets):
- RigEntry: named, timestamped ShoeGazeState (a whole multi-board rig).
- RigArchive: portable multi-rig JSON bundle for backup/transfer; legacy
  single-rig files import as a one-rig archive.
- RigLibraryStore (@MainActor @Observable): one JSON file per rig, with
  NSFileCoordinator writes and NSMetadataQuery live remote-change watching.
- RigLibraryLocation: iCloud-or-local directory resolver (skips the slow
  ubiquity lookup when no iCloud account is signed in).
- ShoeGaze.loadRig(from: ShoeGazeState) + captureEntry; shared filename
  sanitizer factored out of BoardVersionStore.

UI (LyrebirdShoeGazeUI):
- RigLibrarySidebarView: persistent left listing with new/save/save-as/
  duplicate/rename/delete/erase-all and archive import/export.
- RigNamePrompt; RigLibraryBootstrap one-call shell setup.
- ShoeGazeBuilderView: sidebar wired in; selecting a rig replaces the
  session; File menu / sidebar do whole-library archive import/export.

Shells/entitlements: both builder targets own+start the store and carry
the iCloud Documents entitlement; NSUbiquitousContainers exposes the rigs
in the Files app. (macOS pbxproj also carries a pre-existing 1.0->1.1.1
version bump.)

Tests: RigEntry/RigArchive round-trip + RigLibraryStore CRUD/import/export
(15 new; full suite 206 passing).

Provisioning (manual, not done here): enable the iCloud container on both
App IDs and select it in Signing & Capabilities for both targets.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-27 17:56:10 -07:00

68 lines
2.7 KiB
Swift

// RigLibraryBootstrap.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
import LyrebirdShoeGaze
/// One-call setup of the rig library for an app shell.
///
/// Resolves the (iCloud-or-local) library location, constructs and starts a ``RigLibraryStore``,
/// seeds it with the current working rig the first time it is empty (so the listing is never blank
/// and the user's existing setup becomes their first saved rig), and reports which entry should be
/// highlighted as the current rig on this launch.
@MainActor
public enum RigLibraryBootstrap {
/// The result of bootstrapping: a started store and the rig to highlight at launch.
public struct Result {
public let store: RigLibraryStore
public let initialRigID: UUID?
}
/// Builds and starts the rig library for `shoeGaze`.
///
/// - Important: Resolving the iCloud container performs blocking I/O. Call this once at app
/// startup; it is acceptable there but should not be called repeatedly.
///
/// - Parameters:
/// - shoeGaze: The session whose current rig seeds the library when it is first empty.
/// - seedName: Name for the seeded first rig. Defaults to `"Default"`.
/// - Returns: The started store and the entry id to seed the builder's current-rig highlight.
public static func make(
for shoeGaze: ShoeGaze,
seedName: String = "Default"
) -> Result {
let resolved: RigLibraryLocation.Resolved = RigLibraryLocation.resolve()
let store: RigLibraryStore = RigLibraryStore(resolved: resolved)
store.start()
let defaults: UserDefaults = UserDefaults.standard
let key: String = ShoeGazeBuilderView.currentRigDefaultsKey
// First run with an empty library: capture the current working rig as the first entry and
// remember it as current.
if store.entries.isEmpty, !shoeGaze.boards.isEmpty {
let entry: RigEntry = shoeGaze.captureEntry(name: seedName)
do {
try store.save(entry)
defaults.set(entry.id.uuidString, forKey: key)
} catch {
print("[RigLibraryBootstrap] seeding library failed: \(error)")
}
}
// Resolve the highlight only if the remembered id still exists in the library.
let initial: UUID? = {
guard let stored: String = defaults.string(forKey: key),
let id: UUID = UUID(uuidString: stored),
store.entries.contains(where: { $0.id == id }) else { return nil }
return id
}()
return Result(store: store, initialRigID: initial)
}
}