-
Notifications
You must be signed in to change notification settings - Fork 279
/
NotificationsCommandManager.swift
106 lines (90 loc) · 3.44 KB
/
NotificationsCommandManager.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import Communicator
import PromiseKit
import UserNotifications
public protocol NotificationCommandHandler {
func handle(_ payload: [String: Any]) -> Promise<Void>
}
public class NotificationCommandManager {
public static var didUpdateComplicationsNotification: Notification.Name {
.init(rawValue: "didUpdateComplicationsNotification")
}
public enum CommandError: Error {
case notCommand
case unknownCommand
}
public init() {
register(command: "request_location_update", handler: HandlerLocationUpdate())
register(command: "clear_notification", handler: HandlerClearNotification())
#if os(iOS)
register(command: "update_complications", handler: HandlerUpdateComplications())
#endif
}
private var commands = [String: NotificationCommandHandler]()
public func register(command: String, handler: NotificationCommandHandler) {
commands[command] = handler
}
public func handle(_ payload: [AnyHashable: Any]) -> Promise<Void> {
guard let hadict = payload["homeassistant"] as? [String: Any],
let command = hadict["command"] as? String else {
return .init(error: CommandError.notCommand)
}
if let handler = commands[command] {
return handler.handle(hadict)
} else {
return .init(error: CommandError.unknownCommand)
}
}
public func updateComplications() -> Promise<Void> {
#if os(iOS)
HandlerUpdateComplications().handle([:])
#else
return .value(())
#endif
}
}
private struct HandlerLocationUpdate: NotificationCommandHandler {
private enum LocationUpdateError: Error {
case notEnabled
}
func handle(_ payload: [String: Any]) -> Promise<Void> {
guard Current.settingsStore.locationSources.pushNotifications else {
Current.Log.info("ignoring request, location source of notifications is disabled")
return .init(error: LocationUpdateError.notEnabled)
}
Current.Log.verbose("Received remote request to provide a location update")
return Current.backgroundTask(withName: "push-location-request") { remaining in
Current.api.then(on: nil) { api in
api.GetAndSendLocation(trigger: .PushNotification, maximumBackgroundTime: remaining)
}
}
}
}
private struct HandlerClearNotification: NotificationCommandHandler {
func handle(_ payload: [String: Any]) -> Promise<Void> {
Current.Log.verbose("clearing notification for \(payload)")
let keys = ["tag", "collapseId"].compactMap { payload[$0] as? String }
if !keys.isEmpty {
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: keys)
}
return .value(())
}
}
#if os(iOS)
private struct HandlerUpdateComplications: NotificationCommandHandler {
func handle(_ payload: [String: Any]) -> Promise<Void> {
Promise<Void> { seal in
Communicator.shared.transfer(ComplicationInfo(content: [:])) { result in
switch result {
case .success: seal.fulfill(())
case let .failure(error): seal.reject(error)
}
}
}.get {
NotificationCenter.default.post(
name: NotificationCommandManager.didUpdateComplicationsNotification,
object: nil
)
}
}
}
#endif