forked from shadowfacts/Tusker
Rename PushManager properties
This commit is contained in:
parent
68dad77f81
commit
edc887dd4c
|
@ -13,7 +13,7 @@ class DisabledPushManager: _PushManager {
|
|||
false
|
||||
}
|
||||
|
||||
var pushProxyRegistration: PushProxyRegistration? {
|
||||
var proxyRegistration: PushProxyRegistration? {
|
||||
nil
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -27,7 +27,7 @@ class PushManagerImpl: _PushManager {
|
|||
private var remoteNotificationsRegistrationContinuation: CheckedContinuation<Data, any Error>?
|
||||
|
||||
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) {
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue