Packages/MoonriseKit/Sources/Feeds/SunSnapshot.swift
29 lines · 1146 bytes
1import Foundation
2
3/// The NOAA feeds behind the Sun tab, saved after each complete load so the tab has something to show offline.
4public struct SunSnapshot: Sendable, Equatable, Codable {
5 public var fetched: Date
6 public var kp: [KpBar]
7 public var xray: [XRaySample]
8 public var ovation: Ovation?
9 public var scales: NOAAScales?
10 public var probabilities: SolarProbabilities?
11
12 public init(fetched: Date, kp: [KpBar], xray: [XRaySample], ovation: Ovation?, scales: NOAAScales?, probabilities: SolarProbabilities?) {
13 self.fetched = fetched
14 self.kp = kp
15 self.xray = xray
16 self.ovation = ovation
17 self.scales = scales
18 self.probabilities = probabilities
19 }
20
21 public func save(to file: URL) throws {
22 try FileManager.default.createDirectory(at: file.deletingLastPathComponent(), withIntermediateDirectories: true)
23 try JSONEncoder().encode(self).write(to: file, options: .atomic)
24 }
25
26 public static func load(from file: URL) -> SunSnapshot? {
27 (try? Data(contentsOf: file)).flatMap { try? JSONDecoder().decode(SunSnapshot.self, from: $0) }
28 }
29}