From ee9f2904aa319231b7047fca3cb069f0c07019cd Mon Sep 17 00:00:00 2001 From: Christian Cleberg Date: Sun, 12 Apr 2026 23:19:20 -0500 Subject: 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 --- Shared/SystemStatusWidgetSnapshot.swift | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'Shared/SystemStatusWidgetSnapshot.swift') 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) -- cgit v1.2.3