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

97 lines
4.8 KiB
Swift

// RigLibraryLocation.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
/// Resolves where the rig library lives on disk: the shared iCloud container when available,
/// otherwise a local Application Support folder.
///
/// Both the macOS and iOS builder apps declare the **same** iCloud container
/// (``containerIdentifier``) in their entitlements — even though their bundle identifiers differ —
/// so a single rig library is shared across a user's devices and across both apps. When iCloud is
/// unavailable (not signed in, entitlement missing, or running in a context without provisioning)
/// the library falls back to a local folder and simply does not sync.
public enum RigLibraryLocation {
/// The shared iCloud container identifier. Both builder targets must list this exact value in
/// `com.apple.developer.icloud-container-identifiers` / `ubiquity-container-identifiers`, and
/// the container must be enabled on both App IDs in the developer account.
public static let containerIdentifier: String = "iCloud.net.realizedsound.ShoeGazeData"
/// Subfolder (inside the container's `Documents/` or inside Application Support) holding the
/// per-rig JSON files.
private static let rigsFolderName: String = "Rigs"
/// Local Application Support namespace, matching `BoardVersionStore.defaultDirectory()`.
private static let localFolderName: String = "LyrebirdShoeGaze"
/// iCloud documents subfolder. Storing rigs under `Documents/` (rather than the container root)
/// makes them eligible to appear in the Files app when `NSUbiquitousContainers` is configured.
private static let documentsFolderName: String = "Documents"
/// A resolved library directory and whether it is backed by iCloud.
public struct Resolved: Equatable {
/// The directory holding per-rig JSON files (created if needed).
public let directory: URL
/// `true` when `directory` is inside the iCloud ubiquity container (sync active);
/// `false` for the local Application Support fallback.
public let isICloud: Bool
}
/// Resolves the rig library directory, preferring iCloud.
///
/// - Important: When an iCloud account is signed in,
/// `FileManager.url(forUbiquityContainerIdentifier:)` performs **blocking** I/O that can be
/// slow on its first call. Prefer calling this **once at app startup, off the main thread**.
/// When no iCloud account is present, the cheap `ubiquityIdentityToken` check short-circuits
/// straight to the local fallback without the blocking call. The created directory is returned
/// ready to use.
///
/// - Parameter containerIdentifier: The ubiquity container to use. Defaults to
/// ``containerIdentifier``.
/// - Returns: The resolved directory and whether iCloud backing is active.
public static func resolve(
containerIdentifier identifier: String = containerIdentifier
) -> Resolved {
let fileManager: FileManager = FileManager.default
// No iCloud account signed in → skip the potentially-slow container lookup entirely.
guard fileManager.ubiquityIdentityToken != nil else {
return Resolved(directory: localDirectory(), isICloud: false)
}
if let container: URL = fileManager.url(forUbiquityContainerIdentifier: identifier) {
let directory: URL = container
.appendingPathComponent(documentsFolderName, isDirectory: true)
.appendingPathComponent(rigsFolderName, isDirectory: true)
try? fileManager.createDirectory(at: directory, withIntermediateDirectories: true)
return Resolved(directory: directory, isICloud: true)
}
return Resolved(directory: localDirectory(), isICloud: false)
}
/// The local (non-iCloud) rig library directory, created if needed.
///
/// Falls back to a folder in the system temporary directory if Application Support cannot be
/// resolved. Used as the iCloud-unavailable fallback and directly by tests.
///
/// - Returns: A local `URL` for the rigs directory.
public static func localDirectory() -> URL {
let fileManager: FileManager = FileManager.default
let base: URL = (try? fileManager.url(
for: .applicationSupportDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true
)) ?? fileManager.temporaryDirectory
let directory: URL = base
.appendingPathComponent(localFolderName, isDirectory: true)
.appendingPathComponent(rigsFolderName, isDirectory: true)
try? fileManager.createDirectory(at: directory, withIntermediateDirectories: true)
return directory
}
}