summaryrefslogtreecommitdiff
path: root/Shared/NeedsAttentionSnapshot.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Shared/NeedsAttentionSnapshot.swift')
-rw-r--r--Shared/NeedsAttentionSnapshot.swift56
1 files changed, 47 insertions, 9 deletions
diff --git a/Shared/NeedsAttentionSnapshot.swift b/Shared/NeedsAttentionSnapshot.swift
index f386af6..d4c581c 100644
--- a/Shared/NeedsAttentionSnapshot.swift
+++ b/Shared/NeedsAttentionSnapshot.swift
@@ -7,6 +7,26 @@ enum HutchAppGroup {
static let identifier = "group.net.cleberg.Hutch"
}
+enum ActiveAccountContextStore {
+ private static let activeAccountIDKey = "activeAccount.id"
+
+ static func load(defaults: UserDefaults? = sharedDefaults()) -> String? {
+ defaults?.string(forKey: activeAccountIDKey)
+ }
+
+ static func save(_ accountID: String, defaults: UserDefaults? = sharedDefaults()) {
+ defaults?.set(accountID, forKey: activeAccountIDKey)
+ }
+
+ static func clear(defaults: UserDefaults? = sharedDefaults()) {
+ defaults?.removeObject(forKey: activeAccountIDKey)
+ }
+
+ private static func sharedDefaults() -> UserDefaults? {
+ UserDefaults(suiteName: HutchAppGroup.identifier)
+ }
+}
+
enum NeedsAttentionWidgetConfiguration {
static let kind = "NeedsAttentionWidget"
}
@@ -37,22 +57,29 @@ struct NeedsAttentionSnapshot: Codable, Sendable {
enum NeedsAttentionSnapshotStore {
private static let snapshotKey = "needsAttention.snapshot"
- static func load(defaults: UserDefaults? = sharedDefaults()) -> NeedsAttentionSnapshot? {
+ static func load(
+ accountID: String? = ActiveAccountContextStore.load(),
+ defaults: UserDefaults? = sharedDefaults()
+ ) -> NeedsAttentionSnapshot? {
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(NeedsAttentionSnapshot.self, from: data)
}
- static func save(_ snapshot: NeedsAttentionSnapshot, defaults: UserDefaults? = sharedDefaults()) {
+ static func save(
+ _ snapshot: NeedsAttentionSnapshot,
+ 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()
}
@@ -60,23 +87,25 @@ enum NeedsAttentionSnapshotStore {
unreadInboxThreads: Int? = nil,
assignedOpenTickets: Int? = nil,
failedBuilds: Int? = nil,
+ accountID: String? = ActiveAccountContextStore.load(),
defaults: UserDefaults? = sharedDefaults()
) {
- let existing = load(defaults: defaults)
+ let existing = load(accountID: accountID, defaults: defaults)
let snapshot = NeedsAttentionSnapshot(
unreadInboxThreads: unreadInboxThreads ?? existing?.unreadInboxThreads,
assignedOpenTickets: assignedOpenTickets ?? existing?.assignedOpenTickets,
failedBuilds: failedBuilds ?? existing?.failedBuilds,
updatedAt: .now
)
- save(snapshot, defaults: defaults)
+ save(snapshot, accountID: accountID, defaults: defaults)
}
static func adjustUnreadInboxThreads(
by delta: Int,
+ accountID: String? = ActiveAccountContextStore.load(),
defaults: UserDefaults? = sharedDefaults()
) {
- guard let existing = load(defaults: defaults),
+ guard let existing = load(accountID: accountID, defaults: defaults),
let unreadInboxThreads = existing.unreadInboxThreads else {
return
}
@@ -88,12 +117,16 @@ enum NeedsAttentionSnapshotStore {
failedBuilds: existing.failedBuilds,
updatedAt: .now
),
+ accountID: accountID,
defaults: defaults
)
}
- 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()
}
@@ -101,6 +134,11 @@ enum NeedsAttentionSnapshotStore {
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: NeedsAttentionWidgetConfiguration.kind)