-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgetSnapshotStore.swift
More file actions
48 lines (39 loc) · 1.43 KB
/
Copy pathWidgetSnapshotStore.swift
File metadata and controls
48 lines (39 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// WidgetSnapshotStore.swift
// DevLogWidgetCore
//
// Created by opfic on 4/17/26.
//
import Foundation
public final class WidgetSnapshotStore {
private let store: WidgetSharedDefaultsStore
private let encoder = JSONEncoder()
private let decoder = JSONDecoder()
public init() {
self.store = WidgetSharedDefaultsStore()
}
public init(store: WidgetSharedDefaultsStore) {
self.store = store
}
public func saveTodaySnapshot(_ snapshot: TodayWidgetSnapshot) throws {
let data = try encoder.encode(snapshot)
store.setData(data, forKey: WidgetSnapshotKey.today)
}
public func loadTodaySnapshot() throws -> TodayWidgetSnapshot? {
guard let data = store.data(forKey: WidgetSnapshotKey.today) else { return nil }
return try decoder.decode(TodayWidgetSnapshot.self, from: data)
}
public func saveHeatmapSnapshot(_ snapshot: HeatmapWidgetSnapshot) throws {
let data = try encoder.encode(snapshot)
store.setData(data, forKey: WidgetSnapshotKey.heatmap)
}
public func loadHeatmapSnapshot() throws -> HeatmapWidgetSnapshot? {
guard let data = store.data(forKey: WidgetSnapshotKey.heatmap) else { return nil }
return try decoder.decode(HeatmapWidgetSnapshot.self, from: data)
}
public func clearSnapshots() {
WidgetSnapshotKey.snapshots.forEach {
store.removeObject(forKey: $0)
}
}
}