// 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 } }