From edc887dd4cf66fde9ac79ae619b082afa7b1d8eb Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 9 Apr 2024 12:35:51 -0400 Subject: [PATCH] Rename PushManager properties --- .../DisabledPushManager.swift | 2 +- .../PushNotifications/PushManager.swift | 2 +- .../PushNotifications/PushManagerImpl.swift | 36 +++++++++---------- .../NotificationsPrefsView.swift | 2 +- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/Packages/PushNotifications/Sources/PushNotifications/DisabledPushManager.swift b/Packages/PushNotifications/Sources/PushNotifications/DisabledPushManager.swift index db73ed1d..1f732c22 100644 --- a/Packages/PushNotifications/Sources/PushNotifications/DisabledPushManager.swift +++ b/Packages/PushNotifications/Sources/PushNotifications/DisabledPushManager.swift @@ -13,7 +13,7 @@ class DisabledPushManager: _PushManager { false } - var pushProxyRegistration: PushProxyRegistration? { + var proxyRegistration: PushProxyRegistration? { nil } diff --git a/Packages/PushNotifications/Sources/PushNotifications/PushManager.swift b/Packages/PushNotifications/Sources/PushNotifications/PushManager.swift index c355d10a..62019ab9 100644 --- a/Packages/PushNotifications/Sources/PushNotifications/PushManager.swift +++ b/Packages/PushNotifications/Sources/PushNotifications/PushManager.swift @@ -43,7 +43,7 @@ public struct PushManager { @MainActor public protocol _PushManager { var enabled: Bool { get } - var pushProxyRegistration: PushProxyRegistration? { get } + var proxyRegistration: PushProxyRegistration? { get } func createSubscription(account: UserAccountInfo) throws -> PushSubscription func removeSubscription(account: UserAccountInfo) diff --git a/Packages/PushNotifications/Sources/PushNotifications/PushManagerImpl.swift b/Packages/PushNotifications/Sources/PushNotifications/PushManagerImpl.swift index 5d42d8be..56d890da 100644 --- a/Packages/PushNotifications/Sources/PushNotifications/PushManagerImpl.swift +++ b/Packages/PushNotifications/Sources/PushNotifications/PushManagerImpl.swift @@ -27,7 +27,7 @@ class PushManagerImpl: _PushManager { private var remoteNotificationsRegistrationContinuation: CheckedContinuation? private let defaults = UserDefaults(suiteName: "group.space.vaccor.Tusker")! - private(set) var pushProxyRegistration: PushProxyRegistration? { + private(set) var proxyRegistration: PushProxyRegistration? { get { if let dict = defaults.dictionary(forKey: "PushProxyRegistration") as? [String: String], let registration = PushProxyRegistration(defaultsDict: dict) { @@ -40,7 +40,7 @@ class PushManagerImpl: _PushManager { defaults.setValue(newValue?.defaultsDict, forKey: "PushProxyRegistration") } } - private(set) var pushSubscriptions: [PushSubscription] { + private(set) var subscriptions: [PushSubscription] { get { if let array = defaults.array(forKey: "PushSubscriptions") as? [[String: Any]] { return array.compactMap(PushSubscription.init(defaultsDict:)) @@ -58,7 +58,7 @@ class PushManagerImpl: _PushManager { } func createSubscription(account: UserAccountInfo) throws -> PushSubscription { - guard let pushProxyRegistration else { + guard let proxyRegistration else { throw CreateSubscriptionError.notRegisteredWithProxy } if let existing = pushSubscription(account: account) { @@ -74,13 +74,13 @@ class PushManagerImpl: _PushManager { } let subscription = PushSubscription( accountID: account.id, - endpoint: endpointURL(registration: pushProxyRegistration, accountID: account.id), + endpoint: endpointURL(registration: proxyRegistration, accountID: account.id), secretKey: key, authSecret: authSecret, alerts: [], policy: .all ) - pushSubscriptions.append(subscription) + subscriptions.append(subscription) return subscription } @@ -92,11 +92,11 @@ class PushManagerImpl: _PushManager { } func removeSubscription(account: UserAccountInfo) { - pushSubscriptions.removeAll { $0.accountID == account.id } + subscriptions.removeAll { $0.accountID == account.id } } func pushSubscription(account: UserAccountInfo) -> PushSubscription? { - pushSubscriptions.first { $0.accountID == account.id } + subscriptions.first { $0.accountID == account.id } } func register(transactionID: UInt64) async throws -> PushProxyRegistration { @@ -113,22 +113,22 @@ class PushManagerImpl: _PushManager { PushManager.logger.error("Proxy registration failed: \(String(describing: error))") throw PushRegistrationError.registeringWithProxy(error) } - pushProxyRegistration = registration + proxyRegistration = registration return registration } func unregister() async throws { - guard let pushProxyRegistration else { + guard let proxyRegistration else { return } var url = URLComponents(url: endpoint, resolvingAgainstBaseURL: false)! - url.path = "/app/v1/registrations/\(pushProxyRegistration.id)" + url.path = "/app/v1/registrations/\(proxyRegistration.id)" var request = URLRequest(url: url.url!) request.httpMethod = "DELETE" let (data, resp) = try await URLSession.shared.data(for: request) let status = (resp as! HTTPURLResponse).statusCode if (200...299).contains(status) { - self.pushProxyRegistration = nil + self.proxyRegistration = nil PushManager.logger.debug("Unregistered from proxy") } else { PushManager.logger.error("Unregistering: unexpected status \(status)") @@ -138,20 +138,20 @@ class PushManagerImpl: _PushManager { } func updateIfNecessary(updateSubscription: @escaping (PushSubscription) async -> Bool) async { - guard let pushProxyRegistration else { + guard let proxyRegistration else { return } - PushManager.logger.debug("Push proxy registration: \(pushProxyRegistration.id, privacy: .public)") + PushManager.logger.debug("Push proxy registration: \(proxyRegistration.id, privacy: .public)") do { let token = try await getDeviceToken().hexEncodedString() - guard token != pushProxyRegistration.deviceToken else { + guard token != proxyRegistration.deviceToken else { // already up-to-date, nothing to do return } - let newRegistration = try await update(registration: pushProxyRegistration, deviceToken: token) - self.pushProxyRegistration = newRegistration - if pushProxyRegistration.endpoint != newRegistration.endpoint { - self.pushSubscriptions = await AsyncSequenceAdaptor(wrapping: self.pushSubscriptions).map { + let newRegistration = try await update(registration: proxyRegistration, deviceToken: token) + self.proxyRegistration = newRegistration + if proxyRegistration.endpoint != newRegistration.endpoint { + self.subscriptions = await AsyncSequenceAdaptor(wrapping: self.subscriptions).map { var copy = $0 copy.endpoint = await self.endpointURL(registration: newRegistration, accountID: $0.accountID) if await updateSubscription(copy) { diff --git a/Tusker/Screens/Preferences/Notifications/NotificationsPrefsView.swift b/Tusker/Screens/Preferences/Notifications/NotificationsPrefsView.swift index a923f8d1..6240fa03 100644 --- a/Tusker/Screens/Preferences/Notifications/NotificationsPrefsView.swift +++ b/Tusker/Screens/Preferences/Notifications/NotificationsPrefsView.swift @@ -42,7 +42,7 @@ struct NotificationsPrefsView: View { Text(error.localizedDescription) } .task { @MainActor in - pushProxyRegistration = PushManager.shared.pushProxyRegistration + pushProxyRegistration = PushManager.shared.proxyRegistration isSetup = pushProxyRegistration != nil ? .on : .off if !UIApplication.shared.isRegisteredForRemoteNotifications { _ = await registerForRemoteNotifications()