-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNotificationsPermissionsClientLive.swift
63 lines (58 loc) · 1.98 KB
/
NotificationsPermissionsClientLive.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import NotificationsPermissionsClient
import UserNotifications
import Combine
#if os(macOS)
import AppKit
#elseif os(watchOS)
import WatchKit
#else
import UIKit
#endif
extension NotificationsPermissionsClient {
public static let live = NotificationsPermissionsClient(
requestAuthorizationStatus: .live,
requestAuthorization: .live,
configureRemoteNotifications: .live
)
}
extension NotificationsPermissionsClient.Operations.RequestAuthorizationStatus {
public static let live = NotificationsPermissionsClient.Operations.RequestAuthorizationStatus {
Deferred {
Future { promise in
let userNotificationCenter = UNUserNotificationCenter.current()
userNotificationCenter.getNotificationSettings { settings in
promise(.success(.init(status: settings.authorizationStatus)))
}
}
}.eraseToAnyPublisher()
}
}
extension NotificationsPermissionsClient.Operations.RequestAuthorization {
public static let live = NotificationsPermissionsClient.Operations.RequestAuthorization { params in
Deferred {
Future { promise in
let userNotificationCenter = UNUserNotificationCenter.current()
userNotificationCenter.requestAuthorization(options: params.options) { granted, error in
userNotificationCenter.getNotificationSettings { settings in
promise(.success(.init(status: settings.authorizationStatus)))
}
}
}
}.eraseToAnyPublisher()
}
}
extension NotificationsPermissionsClient.Operations.ConfigureRemoteNotifications {
public static let live = NotificationsPermissionsClient.Operations.ConfigureRemoteNotifications { action in
#if os(macOS)
let application = NSApplication.shared
#elseif os(watchOS)
let application = WKExtension.shared()
#else
let application = UIApplication.shared
#endif
switch action {
case .register: application.registerForRemoteNotifications()
case .unregister: application.unregisterForRemoteNotifications()
}
}
}