-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiftNotif.swift
112 lines (92 loc) · 3.92 KB
/
SwiftNotif.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
107
108
109
110
111
112
//
// SwiftNotif.swift
// SwiftNotif
//
// Created by Seyyed Parsa Neshaei on 8/6/17.
// Copyright © 2017 Seyyed Parsa Neshaei. All rights reserved.
//
import Foundation
public class SwiftNotif {
private static var allNotifs: [String: (([String: Any]) -> Void)] = [:]
private static var pendingNotifs: [[String: Any?]] = []
/**
Checks if a notification is observed or not.
- Parameters:
- key: The unique key for the notification.
*/
public static func isObserved(key: String) -> Bool {
return allNotifs[key] != nil
}
/**
Observes for a notification and runs the neccessary code when the notification is posted.
- Parameters:
- key: The unique key for the notification. This can be later used to trigger the notification.
- code: The code to trigger when a new notification is posted. It receives a `context` property which can be used by the user to pass a context when triggering the notification.
*/
public static func observe(key: String, code: @escaping (([String: Any]) -> Void)) {
allNotifs[key] = code
for i in 0..<pendingNotifs.count {
let pN = pendingNotifs[i]
if pN["key"] as? String ?? "" == key {
let context = pN["context"] as? [String: Any] ?? [:]
let completion = pN["context"] as? (() -> Void)
pendingNotifs.remove(at: i)
trigger(key: key, context: context, completion: completion)
break
}
}
}
/**
Stops listening for a specified notification. To continue receiving notifications from the specified notification, it must be observed again.
- Parameters:
- key: The unique key for the notification to be unobserved.
*/
public static func unobserve(key: String) {
allNotifs.removeValue(forKey: key)
for i in 0..<pendingNotifs.count {
let pN = pendingNotifs[i]
if pN["key"] as? String ?? "" == key {
pendingNotifs.remove(at: i)
}
}
}
/**
Stops listening for all notifications. To continue receiving notifications, they must be observed again.
*/
public static func unobserveAllNotifications() {
allNotifs = [:]
pendingNotifs = []
}
/**
Triggers a notification. If the notification is not yet observed, it will wait to be triggered as soon as it is observed. To pass context and add a completion block, use the other method `trigger(key:context:completion:)`
- Parameters:
- key: The unique key for the notification to be triggered.
*/
public static func trigger(key: String) {
guard let code = allNotifs[key] else {
pendingNotifs.append(["key": key])
return
}
code([:])
}
/**
Triggers a notification. If the notification is not yet observed, it will wait to be triggered as soon as it is observed.
- Parameters:
- key: The unique key for the notification to be triggered.
- context: A dictionary which is passed to the `code` block - specified when the notification was observed - as a parameter to indicate the current context.
- completion: A block which will run after the notification is triggered and the `code` block - specified when the notification was observed - is called. If the notification is not observed yet, `completion` will run as soon as it is observed.
*/
public static func trigger(key: String, context: [String: Any], completion: (() -> Void)? = nil) {
guard let code = allNotifs[key] else {
pendingNotifs.append([
"key": key,
"context": context,
"completion": completion
])
return
}
code(context)
guard let c = completion else { return }
c()
}
}