Files
ShoeGaze/Sources/LyrebirdShoeGaze/Presets/BoardVersion.swift
T
2026-06-21 12:25:18 -07:00

74 lines
2.9 KiB
Swift

// BoardVersion.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
/// A named, timestamped snapshot of a single `PedalBoard`'s label and pedal chain.
///
/// `BoardVersion` is the unit stored and retrieved by `BoardVersionStore`. Each version
/// captures a complete `BoardState` (pedal types, knob values, bypass flags) alongside
/// a human-readable `name` (e.g. `"Lead Crunch"`) and a `savedAt` timestamp.
///
/// `BoardVersion` is `Codable` so it can be written to and read from JSON files by
/// `BoardVersionStore`. It is `Identifiable` (via `UUID`) so SwiftUI `List` and
/// `ForEach` views can use it directly, and `Equatable` for round-trip testing.
///
/// ## Typical usage
///
/// ```swift
/// let store = BoardVersionStore()
/// store.capture(myBoard, name: "Lead Crunch")
/// try store.writeAllToDisk(in: BoardVersionStore.defaultDirectory())
/// ```
///
/// - Parameters:
/// - id: Stable unique identifier for this version.
/// - name: Human-readable label (e.g. `"Lead Crunch"`).
/// - savedAt: The `Date` when this version was captured.
/// - state: The full `BoardState` snapshot.
public struct BoardVersion: Codable, Identifiable, Equatable {
/// Stable unique identifier for this version entry.
public let id: UUID
/// Human-readable name for this version (e.g. `"Lead Crunch"`, `"Clean Wash"`).
public let name: String
/// Date and time at which this version was captured, at **whole-second precision**.
///
/// The timestamp is rounded to the nearest second at init so it survives a JSON
/// round-trip exactly (a `Date` serialised as a fractional `secondsSince1970` Double
/// otherwise loses precision through the decimal text form, which would break
/// `Equatable` after `writeToDisk` / `loadAllFromDisk`).
public let savedAt: Date
/// The full `BoardState` snapshot — label, pedal types, knob values, and bypass flags.
public let state: BoardState
/// Creates a board version snapshot.
///
/// - Parameters:
/// - id: Stable unique identifier. Defaults to a newly generated `UUID()`.
/// - name: Human-readable name for this version.
/// - savedAt: Capture timestamp. Defaults to `Date()` (now). Rounded to the
/// nearest whole second so persistence round-trips are lossless.
/// - state: The `BoardState` to store.
public init(
id: UUID = UUID(),
name: String,
savedAt: Date = Date(),
state: BoardState
) {
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
}
}