diff options
| author | Christian Cleberg <[email protected]> | 2026-04-12 23:19:20 -0500 |
|---|---|---|
| committer | Christian Cleberg <[email protected]> | 2026-04-12 23:19:20 -0500 |
| commit | ee9f2904aa319231b7047fca3cb069f0c07019cd (patch) | |
| tree | 69a18234512c63f38f0d477e0023969698ec3c9e /Shared/SystemStatusWidgetSnapshot.swift | |
| parent | 72f860e0e171f6aac4e7d896e7a571147dbcb5c5 (diff) | |
| download | hutch-ee9f2904aa319231b7047fca3cb069f0c07019cd.tar.gz hutch-ee9f2904aa319231b7047fca3cb069f0c07019cd.tar.bz2 hutch-ee9f2904aa319231b7047fca3cb069f0c07019cd.zip | |
feat: add multi-account support
Implements: https://todo.sr.ht/~ccleberg/hutch/49
Implements: https://todo.sr.ht/~ccleberg/hutch/50
Implements: https://todo.sr.ht/~ccleberg/hutch/51
Diffstat (limited to 'Shared/SystemStatusWidgetSnapshot.swift')
| -rw-r--r-- | Shared/SystemStatusWidgetSnapshot.swift | 27 |
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) |
