summaryrefslogtreecommitdiff
path: root/Shared/SystemStatusWidgetSnapshot.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Shared/SystemStatusWidgetSnapshot.swift')
-rw-r--r--Shared/SystemStatusWidgetSnapshot.swift27
1 files changed, 21 insertions, 6 deletions
diff --git a/Shared/SystemStatusWidgetSnapshot.swift b/Shared/SystemStatusWidgetSnapshot.swift
index 8489547..0b19365 100644
--- a/Shared/SystemStatusWidgetSnapshot.swift
+++ b/Shared/SystemStatusWidgetSnapshot.swift
@@ -33,25 +33,35 @@ struct SystemStatusWidgetSnapshot: Codable, Sendable {
enum SystemStatusWidgetSnapshotStore {
private static let snapshotKey = "systemStatus.widgetSnapshot"
- static func load(defaults: UserDefaults? = sharedDefaults()) -> SystemStatusWidgetSnapshot? {
+ static func load(
+ accountID: String? = ActiveAccountContextStore.load(),
+ defaults: UserDefaults? = sharedDefaults()
+ ) -> SystemStatusWidgetSnapshot? {
guard let defaults,
- let data = defaults.data(forKey: snapshotKey) else {
+ let data = defaults.data(forKey: scopedKey(for: accountID)) else {
return nil
}
return try? JSONDecoder().decode(SystemStatusWidgetSnapshot.self, from: data)
}
- static func save(_ snapshot: SystemStatusWidgetSnapshot, defaults: UserDefaults? = sharedDefaults()) {
+ static func save(
+ _ snapshot: SystemStatusWidgetSnapshot,
+ accountID: String? = ActiveAccountContextStore.load(),
+ defaults: UserDefaults? = sharedDefaults()
+ ) {
guard let defaults,
let data = try? JSONEncoder().encode(snapshot) else {
return
}
- defaults.set(data, forKey: snapshotKey)
+ defaults.set(data, forKey: scopedKey(for: accountID))
reloadWidgetTimelines()
}
- static func clear(defaults: UserDefaults? = sharedDefaults()) {
- defaults?.removeObject(forKey: snapshotKey)
+ static func clear(
+ accountID: String? = ActiveAccountContextStore.load(),
+ defaults: UserDefaults? = sharedDefaults()
+ ) {
+ defaults?.removeObject(forKey: scopedKey(for: accountID))
reloadWidgetTimelines()
}
@@ -59,6 +69,11 @@ enum SystemStatusWidgetSnapshotStore {
UserDefaults(suiteName: HutchAppGroup.identifier)
}
+ private static func scopedKey(for accountID: String?) -> String {
+ guard let accountID, !accountID.isEmpty else { return snapshotKey }
+ return "\(snapshotKey).\(accountID)"
+ }
+
private static func reloadWidgetTimelines() {
#if canImport(WidgetKit)
WidgetCenter.shared.reloadTimelines(ofKind: SystemStatusWidgetConfiguration.kind)