metatext-app-ios-iphone-ipad/System/AppDelegate.swift

43 lines
1.3 KiB
Swift
Raw Normal View History

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