From 4c957b86ae69210eb3ef3f7ed94f72c8548e47f5 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 9 Apr 2024 21:07:14 -0400 Subject: [PATCH] Fix push subscription policy/alerts not persisting --- .../PushNotifications/DisabledPushManager.swift | 3 +++ .../Sources/PushNotifications/PushManager.swift | 1 + .../Sources/PushNotifications/PushManagerImpl.swift | 10 ++++++++++ .../Notifications/PushInstanceSettingsView.swift | 1 + 4 files changed, 15 insertions(+) diff --git a/Packages/PushNotifications/Sources/PushNotifications/DisabledPushManager.swift b/Packages/PushNotifications/Sources/PushNotifications/DisabledPushManager.swift index 1f732c22..ef7454c5 100644 --- a/Packages/PushNotifications/Sources/PushNotifications/DisabledPushManager.swift +++ b/Packages/PushNotifications/Sources/PushNotifications/DisabledPushManager.swift @@ -24,6 +24,9 @@ class DisabledPushManager: _PushManager { func removeSubscription(account: UserAccountInfo) { } + func updateSubscription(account: UserAccountInfo, alerts: PushSubscription.Alerts, policy: PushSubscription.Policy) { + } + func pushSubscription(account: UserAccountInfo) -> PushSubscription? { nil } diff --git a/Packages/PushNotifications/Sources/PushNotifications/PushManager.swift b/Packages/PushNotifications/Sources/PushNotifications/PushManager.swift index 62019ab9..f282a81b 100644 --- a/Packages/PushNotifications/Sources/PushNotifications/PushManager.swift +++ b/Packages/PushNotifications/Sources/PushNotifications/PushManager.swift @@ -47,6 +47,7 @@ public protocol _PushManager { func createSubscription(account: UserAccountInfo) throws -> PushSubscription func removeSubscription(account: UserAccountInfo) + func updateSubscription(account: UserAccountInfo, alerts: PushSubscription.Alerts, policy: PushSubscription.Policy) func pushSubscription(account: UserAccountInfo) -> PushSubscription? func register(transactionID: UInt64) async throws -> PushProxyRegistration diff --git a/Packages/PushNotifications/Sources/PushNotifications/PushManagerImpl.swift b/Packages/PushNotifications/Sources/PushNotifications/PushManagerImpl.swift index 56d890da..b0bdc482 100644 --- a/Packages/PushNotifications/Sources/PushNotifications/PushManagerImpl.swift +++ b/Packages/PushNotifications/Sources/PushNotifications/PushManagerImpl.swift @@ -95,6 +95,16 @@ class PushManagerImpl: _PushManager { subscriptions.removeAll { $0.accountID == account.id } } + func updateSubscription(account: UserAccountInfo, alerts: PushSubscription.Alerts, policy: PushSubscription.Policy) { + guard let index = subscriptions.firstIndex(where: { $0.accountID == account.id }) else { + return + } + var copy = subscriptions[index] + copy.alerts = alerts + copy.policy = policy + subscriptions[index] = copy + } + func pushSubscription(account: UserAccountInfo) -> PushSubscription? { subscriptions.first { $0.accountID == account.id } } diff --git a/Tusker/Screens/Preferences/Notifications/PushInstanceSettingsView.swift b/Tusker/Screens/Preferences/Notifications/PushInstanceSettingsView.swift index ce6968b6..63b38b8d 100644 --- a/Tusker/Screens/Preferences/Notifications/PushInstanceSettingsView.swift +++ b/Tusker/Screens/Preferences/Notifications/PushInstanceSettingsView.swift @@ -108,6 +108,7 @@ struct PushInstanceSettingsView: View { do { let result = try await mastodonController.updatePushSubscription(alerts: alerts, policy: policy) PushManager.logger.debug("Push subscription \(result.id) updated on \(account.instanceURL)") + await PushManager.shared.updateSubscription(account: account, alerts: alerts, policy: policy) subscription?.alerts = alerts subscription?.policy = policy return true