Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Core/Stores/LocalStores.swift3// Description: State that exists only on this Mac: watchlists (a native addition, the backend has none) and saved window workspaces.4import Foundation56public struct Watchlist: Sendable, Equatable, Identifiable, Codable {7 public var id: UUID8 public var name: String9 public var symbols: [String]10 public init(id: UUID = UUID(), name: String, symbols: [String]) { self.id = id; self.name = name; self.symbols = symbols }1112 public static func normalize(_ s: String) -> String? {13 let t = s.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()14 return t.range(of: "^[A-Z0-9^][A-Z0-9.\\-=^]{0,14}$", options: .regularExpression) != nil ? t : nil15 }16 /// Adds a symbol once; returns false when it is malformed or already present.17 @discardableResult18 public mutating func add(_ raw: String) -> Bool {19 guard let s = Self.normalize(raw), !symbols.contains(s) else { return false }20 symbols.append(s); return true21 }22}2324/// A saved arrangement of windows: what each one showed and where it was.25public struct SavedWorkspace: Sendable, Equatable, Identifiable, Codable {26 public struct Window: Sendable, Equatable, Codable {27 public var section: String28 public var symbol: String?29 public var frame: [Double]?30 /// "main" or "chart"31 public var kind: String32 public init(section: String, symbol: String?, frame: [Double]?, kind: String) { self.section = section; self.symbol = symbol; self.frame = frame; self.kind = kind }33 }34 public var id: UUID35 public var name: String36 public var windows: [Window]37 public var savedAt: Date38 public init(id: UUID = UUID(), name: String, windows: [Window], savedAt: Date = Date()) { self.id = id; self.name = name; self.windows = windows; self.savedAt = savedAt }39}4041/// A small JSON document on disk, written atomically. Used for watchlists and workspaces (not sensitive, kept in42/// Application Support; anything that came from the server goes through `DiskCache` instead).43public struct JSONFile<T: Codable & Sendable>: Sendable {44 public let url: URL45 public init(url: URL) { self.url = url }46 public func load() -> T? { (try? Data(contentsOf: url)).flatMap { try? JSONDecoder().decode(T.self, from: $0) } }47 public func save(_ value: T) throws {48 try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)49 let e = JSONEncoder(); e.outputFormatting = [.prettyPrinted, .sortedKeys]50 try e.encode(value).write(to: url, options: .atomic)51 }52}53