Files
ShoeGaze/docs/plans/1.2-rig-library.md
T
josh 9a8e5461c0 Make rig library loads/resolve fully off-main
Move the remaining synchronous main-thread I/O off the main actor:

- RigLibraryStore now constructs instantly with a local placeholder and
  exposes an awaitable async start(): it resolves the iCloud-or-local
  location off-main (the blocking ubiquity lookup), loads entries off-main
  via the shared ioQueue (so a reload can't drop a just-saved rig), then
  sets isLoaded. reload() is async; iCloud metadata updates re-read via it.
- Seeding moves into RigLibraryBootstrap.start(_:seedFrom:), which seeds a
  "Default" rig only AFTER the (possibly iCloud) library has loaded and is
  still empty — preventing a duplicate "Default" on every synced device on
  first launch.
- Sidebar shows a Loading state until isLoaded; the builder restores the
  persisted current-rig highlight when isLoaded flips true (initialRigID
  param dropped).
- Shells construct the store synchronously and start it asynchronously
  (macOS via Task in didFinishLaunching; iOS via .task). Codec + file
  helpers are nonisolated static so background work needs no actor hop.
- Tests await store.start().

Full suite: 206 passing.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-27 19:50:38 -07:00

80 lines
5.2 KiB
Markdown

# Rig Library (iCloud-synced rigs)
Replaces the single hand-saved rig file with a managed **library of named rigs** persisted in app
storage and synced across devices and both apps via a shared iCloud container. A rig is a whole
session (`ShoeGazeState`) and may contain multiple boards.
## What shipped
**Core (`LyrebirdShoeGaze/Presets/`)**
- `RigEntry` — a named, timestamped `ShoeGazeState` (rig-level analogue of `BoardVersion`).
- `RigArchive` — a portable bundle of many `RigEntry` in one JSON file (Export/Import Archive). A
legacy single-rig file (bare `ShoeGazeState`) imports as a one-rig archive.
- `RigLibraryLocation` — resolves the library directory: shared iCloud container `Documents/Rigs/`
when available, else local Application Support (`LyrebirdShoeGaze/Rigs/`).
- `RigLibraryStore` (`@MainActor @Observable`) — one JSON file per rig
(`<sanitized-name>-<uuid>.json`); save/delete/duplicate/rename/eraseAll/import/export. iCloud
reads/writes are coordinated via `NSFileCoordinator`; remote changes are watched with
`NSMetadataQuery` so a rig saved on one device appears on another live.
- `ShoeGaze.loadRig(from: ShoeGazeState)` overload + `ShoeGaze.captureEntry(name:id:)`.
- `sanitizePresetFilename` factored out of `BoardVersionStore` and shared.
**UI (`LyrebirdShoeGazeUI/`)**
- `RigLibrarySidebarView` — persistent left listing; rows show name + timestamp, context menu
(Rename / Duplicate / Delete), bottom bar (New / Save / Save As / ⋯ → Import / Export / Erase
All), and an iCloud status glyph.
- `RigNamePrompt` — shared name-entry alert (New / Save As / Rename / Duplicate).
- `RigLibraryBootstrap` — one-call shell setup: resolve location, start the store, seed it from the
working rig on first run, resolve the launch highlight.
- `ShoeGazeBuilderView` gains the sidebar; selecting a rig **replaces** the current session
(engine stopped/restarted around the rebuild). When the current rig has unsaved changes
(`captureState()` differs from its library entry), switching first prompts **Save /
Don't Save / Cancel** (both platforms). The old single-rig Save/Load is gone; the File menu /
sidebar now do whole-library **Archive** import/export.
**Shells / entitlements**
- macOS `AppDelegate` and iOS app model own + start the store and pass it to the view; macOS File
menu = New Rig (⌘N), Save Rig (⌘S), Export/Import Archive (⌘E/⌘I). The per-rig menu items forward
to the builder view via `Notification.Name` (`shoeGazeNewRig` / `shoeGazeSaveRig`) so naming
prompts and the current-rig highlight stay in the view that owns that state.
- Both builder targets carry the iCloud entitlement for container
`iCloud.net.realizedsound.ShoeGazeData`; `NSUbiquitousContainers` exposes `Documents/Rigs` in the
Files app.
## Manual provisioning (required before iCloud sync works)
Code is in place but signing is not provisionable from here. In Xcode, for **both** builder targets
(`ShoeGazeBuilder`, `ShoeGazeBuilderiOS`), under Signing & Capabilities:
1. Confirm the **iCloud** capability is present with **iCloud Documents** checked and the container
`iCloud.net.realizedsound.ShoeGazeData` selected (entitlement files already declare it).
2. Register that container on **both** App IDs in the developer account (same Team `HE5VJFE9E4`).
3. Build signed to a device/Mac signed into iCloud to verify cross-device sync.
If iCloud is unavailable the library transparently falls back to local storage (no sync) — the app
still works; the sidebar shows the `icloud.slash` glyph.
## Notes / follow-ups
- Editing a loaded rig does not auto-save to its library entry — Save is explicit. `RigStore`
autosave still preserves unsaved working state across restarts.
### Known limitations / deferred (from pre-PR review)
- **Off-main I/O end to end.** Save/delete/erase update the in-memory `entries` on the main actor
instantly (UI never blocks) and run the file write/delete — including `NSFileCoordinator` — on a
serial background queue (`ioQueue`). Location resolution (`RigLibraryLocation.resolve()`, the
blocking ubiquity lookup) and the initial/refresh *reads* now also run off the main thread:
`RigLibraryStore` constructs instantly with a local placeholder, and `await start()` resolves the
real iCloud-or-local location, loads via `ioQueue`, then sets `isLoaded`. Reads share `ioQueue`
with writes, so a reload never drops a just-saved rig. `flushPendingWrites()` drains the queue
(tests, app termination / iOS background).
- **Loading + correct first-run seeding.** The sidebar shows a "Loading…" state until `isLoaded`.
`RigLibraryBootstrap.start(_:seedFrom:)` seeds the working rig as "Default" **only after** the
(possibly iCloud) library has loaded and is still empty — so a first launch on a device that
already has synced rigs does not create a duplicate "Default" everywhere. The builder restores the
persisted current-rig highlight once `isLoaded` flips true.
- **`eraseAll()`** clears the in-memory list even if a file deletion fails (it still throws the
first error). A re-`start()` re-scans and re-surfaces any undeleted rigs, so state self-heals.
- Possible later work: conflict UI for simultaneous multi-device edits; the async-resolve redesign
above.