metatext/System/AppDelegate.swift

43 lines
1.3 KiB
Swift
Raw Normal View History

2020-08-12 07:24:39 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
2020-09-05 02:31:43 +00:00
import UIKit
2020-08-12 07:24:39 +00:00
2020-09-08 02:35:28 +00:00
final class AppDelegate: NSObject {
2020-08-27 04:25:28 +00:00
@Published private var application: UIApplication?
2020-09-10 03:04:45 +00:00
private let deviceTokenSubject = PassthroughSubject<Data, Error>()
2020-08-12 07:24:39 +00:00
}
extension AppDelegate {
2020-09-06 21:37:54 +00:00
func registerForRemoteNotifications() -> AnyPublisher<Data, Error> {
2020-08-12 07:24:39 +00:00
$application
.compactMap { $0 }
.handleEvents(receiveOutput: { $0.registerForRemoteNotifications() })
.setFailureType(to: Error.self)
2020-09-10 03:04:45 +00:00
.zip(deviceTokenSubject)
2020-08-12 07:24:39 +00:00
.first()
2020-09-06 21:37:54 +00:00
.map { $1 }
2020-08-12 07:24:39 +00:00
.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) {
2020-09-10 03:04:45 +00:00
deviceTokenSubject.send(deviceToken)
2020-08-12 07:24:39 +00:00
}
2020-08-27 04:25:28 +00:00
func application(_ application: UIApplication,
2020-08-12 07:24:39 +00:00
didFailToRegisterForRemoteNotificationsWithError error: Error) {
2020-09-10 03:04:45 +00:00
deviceTokenSubject.send(completion: .failure(error))
2020-08-12 07:24:39 +00:00
}
}