import Foundation /// JSON snapshot cache of the library at `~/.local/state/tv-anarchy/library.json`. /// A plain Codable file (a few-hundred-show library fits in memory trivially) — /// the offline-browsable source of truth when ~/media / black are unreachable. public enum LibraryStore { public static func snapshotURL() -> URL { FileManager.default.homeDirectoryForCurrentUser .appendingPathComponent(".local/state/tv-anarchy/library.json") } public static func load() -> LibrarySnapshot? { guard let data = try? Data(contentsOf: snapshotURL()) else { return nil } let dec = JSONDecoder() dec.dateDecodingStrategy = .iso8601 return try? dec.decode(LibrarySnapshot.self, from: data) } public static func save(_ snapshot: LibrarySnapshot) { let url = snapshotURL() try? FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true) let enc = JSONEncoder() enc.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes] enc.dateEncodingStrategy = .iso8601 guard let data = try? enc.encode(snapshot) else { return } try? data.write(to: url, options: .atomic) } }