metatext/System/AppDelegate.swift

43 lines
1.4 KiB
Swift
Raw Normal View History

2020-08-12 07:24:39 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import UIKit
import Combine
class AppDelegate: NSObject {
2020-08-27 04:25:28 +00:00
@Published private var application: UIApplication?
2020-08-12 07:24:39 +00:00
private let remoteNotificationDeviceTokens = PassthroughSubject<Data, Error>()
}
extension AppDelegate {
func registerForRemoteNotifications() -> AnyPublisher<String, Error> {
$application
.compactMap { $0 }
.handleEvents(receiveOutput: { $0.registerForRemoteNotifications() })
.setFailureType(to: Error.self)
.zip(remoteNotificationDeviceTokens)
.first()
.map { $1.hexEncodedString() }
.eraseToAnyPublisher()
}
}
2020-08-27 04:25:28 +00:00
extension AppDelegate: UIApplicationDelegate {
2020-08-12 07:24:39 +00:00
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
self.application = application
return true
}
2020-08-27 04:25:28 +00:00
func application(_ application: UIApplication,
2020-08-12 07:24:39 +00:00
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
remoteNotificationDeviceTokens.send(deviceToken)
}
2020-08-27 04:25:28 +00:00
func application(_ application: UIApplication,
2020-08-12 07:24:39 +00:00
didFailToRegisterForRemoteNotificationsWithError error: Error) {
remoteNotificationDeviceTokens.send(completion: .failure(error))
}
}