Files
ShoeGaze/Sources/LyrebirdShoeGaze/Presets/RigEntry.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

99 lines
3.9 KiB
Swift

// RigEntry.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
/// A named, timestamped rig in the user's library.
///
/// `RigEntry` is the unit stored and retrieved by `RigLibraryStore`. Each entry pairs a complete
/// ``ShoeGazeState`` snapshot (every board and its pedals) with a human-readable `name`
/// (e.g. `"Ambient Wash"`) and a `savedAt` timestamp. It is the rig-level analogue of
/// `BoardVersion`, which captures a single board.
///
/// `RigEntry` is `Codable` so it round-trips through the per-rig JSON files written by
/// `RigLibraryStore` and through the bundled ``RigArchive`` export. It is `Identifiable`
/// (via `UUID`) for direct use in SwiftUI `List` / `ForEach`, and `Equatable` for round-trip tests.
///
/// ## Typical usage
///
/// ```swift
/// let entry = shoeGaze.captureEntry(name: "Ambient Wash")
/// try store.save(entry)
/// // later …
/// try shoeGaze.loadRig(from: entry.state)
/// ```
public struct RigEntry: Codable, Identifiable, Equatable {
/// Stable unique identifier for this entry. Also seeds the on-disk filename.
public let id: UUID
/// Human-readable name (e.g. `"Ambient Wash"`, `"Live Set A"`).
public let name: String
/// Date and time at which this entry was last saved, at **whole-second precision**.
///
/// Rounded to the nearest second at init so it survives a JSON `secondsSince1970`
/// round-trip exactly — otherwise the fractional `Double` text form loses precision and
/// breaks `Equatable` after a `RigLibraryStore` write/load cycle.
public let savedAt: Date
/// The full rig snapshot — every board, segment, and pedal parameter.
public let state: ShoeGazeState
/// Creates a rig library entry.
///
/// - Parameters:
/// - id: Stable unique identifier. Defaults to a newly generated `UUID()`.
/// - name: Human-readable name for this rig.
/// - savedAt: Save timestamp. Defaults to `Date()` (now). Rounded to the nearest
/// whole second so persistence round-trips are lossless.
/// - state: The ``ShoeGazeState`` rig snapshot to store.
public init(
id: UUID = UUID(),
name: String,
savedAt: Date = Date(),
state: ShoeGazeState
) {
self.id = id
self.name = name
// Round to whole seconds: a fractional Date loses precision through the JSON
// `secondsSince1970` Double text form, which would break `Equatable` after a
// disk round-trip.
self.savedAt = Date(timeIntervalSince1970: savedAt.timeIntervalSince1970.rounded())
self.state = state
}
/// Returns a copy of this entry with a new `name`, a fresh `id`, and `savedAt` updated to
/// `date`. Used by the library's "duplicate" action so a copied rig becomes an independent
/// entry rather than overwriting the original's file.
///
/// - Parameters:
/// - name: The new entry's name.
/// - date: The new save timestamp. Defaults to `Date()` (now).
/// - Returns: A new `RigEntry` carrying the same `state` under a new identity.
public func duplicated(
name: String,
date: Date = Date()
) -> RigEntry {
RigEntry(id: UUID(), name: name, savedAt: date, state: state)
}
/// Returns a copy of this entry with a new `name` and `savedAt` updated to `date`, keeping
/// the same `id`. Used by the library's "rename" action so the entry's identity and file
/// association are preserved.
///
/// - Parameters:
/// - name: The new name.
/// - date: The new save timestamp. Defaults to `Date()` (now).
/// - Returns: A new `RigEntry` with the same `id` and `state` under the new name.
public func renamed(
to name: String,
date: Date = Date()
) -> RigEntry {
RigEntry(id: id, name: name, savedAt: date, state: state)
}
}