// 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 /// The rig's position in the user's manually-ordered set list, or `nil` for a rig that has /// not been placed (a legacy entry saved before ordering existed, or one not yet reordered). /// /// Persisted per-file and inside archive export/import, so a set-list order travels with a /// shared or backed-up library. `RigLibraryStore` sorts placed entries by this index and /// falls back to name/date for unplaced ones. Optional, so older files decode cleanly. public let sortIndex: Int? /// 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. /// - sortIndex: Manual set-list position, or `nil` when unplaced. Defaults to `nil`. public init( id: UUID = UUID(), name: String, savedAt: Date = Date(), state: ShoeGazeState, sortIndex: Int? = nil ) { 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 self.sortIndex = sortIndex } /// Returns a copy of this entry placed at `index` in the set list, keeping its identity, /// name, timestamp, and state. Used by `RigLibraryStore` reordering. /// /// - Parameter index: The new set-list position, or `nil` to unplace. /// - Returns: A copy at the new `sortIndex`. public func reordered(to index: Int?) -> RigEntry { RigEntry(id: id, name: name, savedAt: savedAt, state: state, sortIndex: index) } /// 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 { // A duplicate is unplaced (`sortIndex: nil`) so the store appends it to the set list rather // than colliding with the source's position. RigEntry(id: UUID(), name: name, savedAt: date, state: state, sortIndex: nil) } /// 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 { // Preserve the set-list position on rename — only the name and timestamp change. RigEntry(id: id, name: name, savedAt: date, state: state, sortIndex: sortIndex) } }