Rename PushManager properties

This commit is contained in:
Shadowfacts 2024-04-09 12:35:51 -04:00
parent 68dad77f81
commit edc887dd4c
4 changed files with 21 additions and 21 deletions

View File

@ -13,7 +13,7 @@ class DisabledPushManager: _PushManager {
false false
} }
var pushProxyRegistration: PushProxyRegistration? { var proxyRegistration: PushProxyRegistration? {
nil nil
} }

View File

@ -43,7 +43,7 @@ public struct PushManager {
@MainActor @MainActor
public protocol _PushManager { public protocol _PushManager {
var enabled: Bool { get } var enabled: Bool { get }
var pushProxyRegistration: PushProxyRegistration? { get } var proxyRegistration: PushProxyRegistration? { get }
func createSubscription(account: UserAccountInfo) throws -> PushSubscription func createSubscription(account: UserAccountInfo) throws -> PushSubscription
func removeSubscription(account: UserAccountInfo) func removeSubscription(account: UserAccountInfo)

View File

@ -27,7 +27,7 @@ class PushManagerImpl: _PushManager {
private var remoteNotificationsRegistrationContinuation: CheckedContinuation<Data, any Error>? private var remoteNotificationsRegistrationContinuation: CheckedContinuation<Data, any Error>?
private let defaults = UserDefaults(suiteName: "group.space.vaccor.Tusker")! private let defaults = UserDefaults(suiteName: "group.space.vaccor.Tusker")!
private(set) var pushProxyRegistration: PushProxyRegistration? { private(set) var proxyRegistration: PushProxyRegistration? {
get { get {
if let dict = defaults.dictionary(forKey: "PushProxyRegistration") as? [String: String], if let dict = defaults.dictionary(forKey: "PushProxyRegistration") as? [String: String],
let registration = PushProxyRegistration(defaultsDict: dict) { let registration = PushProxyRegistration(defaultsDict: dict) {
@ -40,7 +40,7 @@ class PushManagerImpl: _PushManager {
defaults.setValue(newValue?.defaultsDict, forKey: "PushProxyRegistration") defaults.setValue(newValue?.defaultsDict, forKey: "PushProxyRegistration")
} }
} }
private(set) var pushSubscriptions: [PushSubscription] { private(set) var subscriptions: [PushSubscription] {
get { get {
if let array = defaults.array(forKey: "PushSubscriptions") as? [[String: Any]] { if let array = defaults.array(forKey: "PushSubscriptions") as? [[String: Any]] {
return array.compactMap(PushSubscription.init(defaultsDict:)) return array.compactMap(PushSubscription.init(defaultsDict:))
@ -58,7 +58,7 @@ class PushManagerImpl: _PushManager {
} }
func createSubscription(account: UserAccountInfo) throws -> PushSubscription { func createSubscription(account: UserAccountInfo) throws -> PushSubscription {
guard let pushProxyRegistration else { guard let proxyRegistration else {
throw CreateSubscriptionError.notRegisteredWithProxy throw CreateSubscriptionError.notRegisteredWithProxy
} }
if let existing = pushSubscription(account: account) { if let existing = pushSubscription(account: account) {
@ -74,13 +74,13 @@ class PushManagerImpl: _PushManager {
} }
let subscription = PushSubscription( let subscription = PushSubscription(
accountID: account.id, accountID: account.id,
endpoint: endpointURL(registration: pushProxyRegistration, accountID: account.id), endpoint: endpointURL(registration: proxyRegistration, accountID: account.id),
secretKey: key, secretKey: key,
authSecret: authSecret, authSecret: authSecret,
alerts: [], alerts: [],
policy: .all policy: .all
) )
pushSubscriptions.append(subscription) subscriptions.append(subscription)
return subscription return subscription
} }
@ -92,11 +92,11 @@ class PushManagerImpl: _PushManager {
} }
func removeSubscription(account: UserAccountInfo) { func removeSubscription(account: UserAccountInfo) {
pushSubscriptions.removeAll { $0.accountID == account.id } subscriptions.removeAll { $0.accountID == account.id }
} }
func pushSubscription(account: UserAccountInfo) -> PushSubscription? { func pushSubscription(account: UserAccountInfo) -> PushSubscription? {
pushSubscriptions.first { $0.accountID == account.id } subscriptions.first { $0.accountID == account.id }
} }
func register(transactionID: UInt64) async throws -> PushProxyRegistration { func register(transactionID: UInt64) async throws -> PushProxyRegistration {
@ -113,22 +113,22 @@ class PushManagerImpl: _PushManager {
PushManager.logger.error("Proxy registration failed: \(String(describing: error))") PushManager.logger.error("Proxy registration failed: \(String(describing: error))")
throw PushRegistrationError.registeringWithProxy(error) throw PushRegistrationError.registeringWithProxy(error)
} }
pushProxyRegistration = registration proxyRegistration = registration
return registration return registration
} }
func unregister() async throws { func unregister() async throws {
guard let pushProxyRegistration else { guard let proxyRegistration else {
return return
} }
var url = URLComponents(url: endpoint, resolvingAgainstBaseURL: false)! 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!) var request = URLRequest(url: url.url!)
request.httpMethod = "DELETE" request.httpMethod = "DELETE"
let (data, resp) = try await URLSession.shared.data(for: request) let (data, resp) = try await URLSession.shared.data(for: request)
let status = (resp as! HTTPURLResponse).statusCode let status = (resp as! HTTPURLResponse).statusCode
if (200...299).contains(status) { if (200...299).contains(status) {
self.pushProxyRegistration = nil self.proxyRegistration = nil
PushManager.logger.debug("Unregistered from proxy") PushManager.logger.debug("Unregistered from proxy")
} else { } else {
PushManager.logger.error("Unregistering: unexpected status \(status)") PushManager.logger.error("Unregistering: unexpected status \(status)")
@ -138,20 +138,20 @@ class PushManagerImpl: _PushManager {
} }
func updateIfNecessary(updateSubscription: @escaping (PushSubscription) async -> Bool) async { func updateIfNecessary(updateSubscription: @escaping (PushSubscription) async -> Bool) async {
guard let pushProxyRegistration else { guard let proxyRegistration else {
return return
} }
PushManager.logger.debug("Push proxy registration: \(pushProxyRegistration.id, privacy: .public)") PushManager.logger.debug("Push proxy registration: \(proxyRegistration.id, privacy: .public)")
do { do {
let token = try await getDeviceToken().hexEncodedString() let token = try await getDeviceToken().hexEncodedString()
guard token != pushProxyRegistration.deviceToken else { guard token != proxyRegistration.deviceToken else {
// already up-to-date, nothing to do // already up-to-date, nothing to do
return return
} }
let newRegistration = try await update(registration: pushProxyRegistration, deviceToken: token) let newRegistration = try await update(registration: proxyRegistration, deviceToken: token)
self.pushProxyRegistration = newRegistration self.proxyRegistration = newRegistration
if pushProxyRegistration.endpoint != newRegistration.endpoint { if proxyRegistration.endpoint != newRegistration.endpoint {
self.pushSubscriptions = await AsyncSequenceAdaptor(wrapping: self.pushSubscriptions).map { self.subscriptions = await AsyncSequenceAdaptor(wrapping: self.subscriptions).map {
var copy = $0 var copy = $0
copy.endpoint = await self.endpointURL(registration: newRegistration, accountID: $0.accountID) copy.endpoint = await self.endpointURL(registration: newRegistration, accountID: $0.accountID)
if await updateSubscription(copy) { if await updateSubscription(copy) {

View File

@ -42,7 +42,7 @@ struct NotificationsPrefsView: View {
Text(error.localizedDescription) Text(error.localizedDescription)
} }
.task { @MainActor in .task { @MainActor in
pushProxyRegistration = PushManager.shared.pushProxyRegistration pushProxyRegistration = PushManager.shared.proxyRegistration
isSetup = pushProxyRegistration != nil ? .on : .off isSetup = pushProxyRegistration != nil ? .on : .off
if !UIApplication.shared.isRegisteredForRemoteNotifications { if !UIApplication.shared.isRegisteredForRemoteNotifications {
_ = await registerForRemoteNotifications() _ = await registerForRemoteNotifications()