summaryrefslogtreecommitdiff
path: root/Shared/SystemStatusWidgetSnapshot.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Shared/SystemStatusWidgetSnapshot.swift')
-rw-r--r--Shared/SystemStatusWidgetSnapshot.swift17
1 files changed, 11 insertions, 6 deletions
diff --git a/Shared/SystemStatusWidgetSnapshot.swift b/Shared/SystemStatusWidgetSnapshot.swift
index 0b19365..025e58c 100644
--- a/Shared/SystemStatusWidgetSnapshot.swift
+++ b/Shared/SystemStatusWidgetSnapshot.swift
@@ -34,11 +34,11 @@ enum SystemStatusWidgetSnapshotStore {
private static let snapshotKey = "systemStatus.widgetSnapshot"
static func load(
- accountID: String? = ActiveAccountContextStore.load(),
+ accountID: String? = nil,
defaults: UserDefaults? = sharedDefaults()
) -> SystemStatusWidgetSnapshot? {
guard let defaults,
- let data = defaults.data(forKey: scopedKey(for: accountID)) else {
+ let data = defaults.data(forKey: scopedKey(for: resolvedAccountID(accountID, defaults: defaults))) else {
return nil
}
return try? JSONDecoder().decode(SystemStatusWidgetSnapshot.self, from: data)
@@ -46,22 +46,23 @@ enum SystemStatusWidgetSnapshotStore {
static func save(
_ snapshot: SystemStatusWidgetSnapshot,
- accountID: String? = ActiveAccountContextStore.load(),
+ accountID: String? = nil,
defaults: UserDefaults? = sharedDefaults()
) {
guard let defaults,
let data = try? JSONEncoder().encode(snapshot) else {
return
}
- defaults.set(data, forKey: scopedKey(for: accountID))
+ defaults.set(data, forKey: scopedKey(for: resolvedAccountID(accountID, defaults: defaults)))
reloadWidgetTimelines()
}
static func clear(
- accountID: String? = ActiveAccountContextStore.load(),
+ accountID: String? = nil,
defaults: UserDefaults? = sharedDefaults()
) {
- defaults?.removeObject(forKey: scopedKey(for: accountID))
+ guard let defaults else { return }
+ defaults.removeObject(forKey: scopedKey(for: resolvedAccountID(accountID, defaults: defaults)))
reloadWidgetTimelines()
}
@@ -74,6 +75,10 @@ enum SystemStatusWidgetSnapshotStore {
return "\(snapshotKey).\(accountID)"
}
+ private static func resolvedAccountID(_ accountID: String?, defaults: UserDefaults) -> String? {
+ accountID ?? ActiveAccountContextStore.load(defaults: defaults)
+ }
+
private static func reloadWidgetTimelines() {
#if canImport(WidgetKit)
WidgetCenter.shared.reloadTimelines(ofKind: SystemStatusWidgetConfiguration.kind)