krz/hutch
an ios client for sourcehut
clone: git clone https://gitbay.org/krz/hutch.git
v3.8.1: Hutch/Networking/SystemStatusCacheStore.swift · raw
1import Foundation
2
3actor SystemStatusCacheStore {
4 private let snapshotCacheKey = "systemStatusSnapshotCache"
5 private let incidentCacheKey = "systemStatusIncidentCache"
6 private let defaults: UserDefaults
7
8 init(defaults: UserDefaults = .standard) {
9 self.defaults = defaults
10 }
11
12 func loadSnapshotHTML() -> (html: String, timestamp: Date)? {
13 guard let html = defaults.string(forKey: snapshotCacheKey) else {
14 return nil
15 }
16
17 let timestampValue = defaults.double(forKey: snapshotTimestampKey)
18 guard timestampValue > 0 else {
19 defaults.removeObject(forKey: snapshotCacheKey)
20 defaults.removeObject(forKey: snapshotTimestampKey)
21 return nil
22 }
23
24 return (html, Date(timeIntervalSince1970: timestampValue))
25 }
26
27 func saveSnapshotHTML(_ html: String, timestamp: Date) {
28 defaults.set(html, forKey: snapshotCacheKey)
29 defaults.set(timestamp.timeIntervalSince1970, forKey: snapshotTimestampKey)
30 }
31
32 func loadIncidentFeedData() -> (data: Data, timestamp: Date)? {
33 guard let data = defaults.data(forKey: incidentCacheKey) else {
34 return nil
35 }
36
37 let timestampValue = defaults.double(forKey: incidentTimestampKey)
38 guard timestampValue > 0 else {
39 defaults.removeObject(forKey: incidentCacheKey)
40 defaults.removeObject(forKey: incidentTimestampKey)
41 return nil
42 }
43
44 return (data, Date(timeIntervalSince1970: timestampValue))
45 }
46
47 func saveIncidentFeedData(_ data: Data, timestamp: Date) {
48 defaults.set(data, forKey: incidentCacheKey)
49 defaults.set(timestamp.timeIntervalSince1970, forKey: incidentTimestampKey)
50 }
51
52 private var snapshotTimestampKey: String {
53 "\(snapshotCacheKey).timestamp"
54 }
55
56 private var incidentTimestampKey: String {
57 "\(incidentCacheKey).timestamp"
58 }
59}