From cb1ea5ea4f163d87053285d3fb7999b12a3558b9 Mon Sep 17 00:00:00 2001 From: Christian Cleberg Date: Sun, 12 Apr 2026 00:15:13 -0500 Subject: harden system status and app reliability --- Hutch/Networking/SystemStatusCacheStore.swift | 59 +++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Hutch/Networking/SystemStatusCacheStore.swift (limited to 'Hutch/Networking/SystemStatusCacheStore.swift') diff --git a/Hutch/Networking/SystemStatusCacheStore.swift b/Hutch/Networking/SystemStatusCacheStore.swift new file mode 100644 index 0000000..63c64b9 --- /dev/null +++ b/Hutch/Networking/SystemStatusCacheStore.swift @@ -0,0 +1,59 @@ +import Foundation + +actor SystemStatusCacheStore { + private let snapshotCacheKey = "systemStatusSnapshotCache" + private let incidentCacheKey = "systemStatusIncidentCache" + private let defaults: UserDefaults + + init(defaults: UserDefaults = .standard) { + self.defaults = defaults + } + + func loadSnapshotHTML() -> (html: String, timestamp: Date)? { + guard let html = defaults.string(forKey: snapshotCacheKey) else { + return nil + } + + let timestampValue = defaults.double(forKey: snapshotTimestampKey) + guard timestampValue > 0 else { + defaults.removeObject(forKey: snapshotCacheKey) + defaults.removeObject(forKey: snapshotTimestampKey) + return nil + } + + return (html, Date(timeIntervalSince1970: timestampValue)) + } + + func saveSnapshotHTML(_ html: String, timestamp: Date) { + defaults.set(html, forKey: snapshotCacheKey) + defaults.set(timestamp.timeIntervalSince1970, forKey: snapshotTimestampKey) + } + + func loadIncidentFeedData() -> (data: Data, timestamp: Date)? { + guard let data = defaults.data(forKey: incidentCacheKey) else { + return nil + } + + let timestampValue = defaults.double(forKey: incidentTimestampKey) + guard timestampValue > 0 else { + defaults.removeObject(forKey: incidentCacheKey) + defaults.removeObject(forKey: incidentTimestampKey) + return nil + } + + return (data, Date(timeIntervalSince1970: timestampValue)) + } + + func saveIncidentFeedData(_ data: Data, timestamp: Date) { + defaults.set(data, forKey: incidentCacheKey) + defaults.set(timestamp.timeIntervalSince1970, forKey: incidentTimestampKey) + } + + private var snapshotTimestampKey: String { + "\(snapshotCacheKey).timestamp" + } + + private var incidentTimestampKey: String { + "\(incidentCacheKey).timestamp" + } +} -- cgit v1.2.3