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-08-12 09:24:39 +02:00
private let remoteNotificationDeviceTokens = PassthroughSubject<Data, Error>()
}
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)
.zip(remoteNotificationDeviceTokens)
.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) {
remoteNotificationDeviceTokens.send(deviceToken)
}
2020-08-27 06:25:28 +02:00
func application(_ application: UIApplication,
2020-08-12 09:24:39 +02:00
didFailToRegisterForRemoteNotificationsWithError error: Error) {
remoteNotificationDeviceTokens.send(completion: .failure(error))
}
}