-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
NotificationContainerControllerNode.swift
250 lines (214 loc) · 9.65 KB
/
NotificationContainerControllerNode.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import Foundation
import UIKit
import Display
import AsyncDisplayKit
import SwiftSignalKit
import TelegramPresentationData
import ChatMessageNotificationItem
private final class NotificationContainerControllerNodeView: UITracingLayerView {
var hitTestImpl: ((CGPoint, UIEvent?) -> UIView?)?
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return self.hitTestImpl?(point, event)
}
}
final class NotificationContainerControllerNode: ASDisplayNode {
private var validLayout: ContainerViewLayout?
private var topItemAndNode: (NotificationItem, NotificationItemContainerNode)?
private var blockingItemAndNode: (NotificationItem, NotificationItemContainerNode)?
var displayingItemsUpdated: ((Bool) -> Void)?
private var timeoutTimer: SwiftSignalKit.Timer?
private var presentationData: PresentationData
init(presentationData: PresentationData) {
self.presentationData = presentationData
super.init()
self.setViewBlock({
return NotificationContainerControllerNodeView()
})
self.backgroundColor = nil
self.isOpaque = false
}
func updatePresentationData(_ presentationData: PresentationData) {
self.presentationData = presentationData
}
override func didLoad() {
super.didLoad()
(self.view as! NotificationContainerControllerNodeView).hitTestImpl = { [weak self] point, event in
return self?.hitTest(point, with: event)
}
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if let (_, blockingItemNode) = self.blockingItemAndNode {
return blockingItemNode.hitTest(point, with: event)
}
if let (_, topItemNode) = self.topItemAndNode {
return topItemNode.hitTest(point, with: event)
}
return nil
}
func containerLayoutUpdated(_ layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) {
self.validLayout = layout
if let (_, topItemNode) = self.topItemAndNode {
transition.updateFrame(node: topItemNode, frame: CGRect(origin: CGPoint(), size: layout.size))
topItemNode.updateLayout(layout: layout, transition: transition)
}
}
func removeItemsWithGroupingKey(_ key: AnyHashable) {
if let (item, topItemNode) = self.topItemAndNode {
if item.groupingKey == key {
self.topItemAndNode = nil
self.displayingItemsUpdated?(false)
topItemNode.animateOut(completion: { [weak topItemNode] in
topItemNode?.removeFromSupernode()
})
}
}
}
func enqueue(_ item: NotificationItem) {
if self.blockingItemAndNode != nil {
return
}
if let (_, topItemNode) = self.topItemAndNode {
topItemNode.animateOut(completion: { [weak topItemNode] in
topItemNode?.removeFromSupernode()
})
}
var useCompactLayout = false
if let validLayout = self.validLayout {
useCompactLayout = min(validLayout.size.width, validLayout.size.height) < 375.0
}
let itemNode = item.node(compact: useCompactLayout)
let containerNode = NotificationItemContainerNode(theme: self.presentationData.theme, contentNode: itemNode)
containerNode.item = item
containerNode.dismissed = { [weak self] item in
if let strongSelf = self {
if let (topItem, topItemNode) = strongSelf.topItemAndNode, topItem.groupingKey != nil && topItem.groupingKey == item.groupingKey {
topItemNode.removeFromSupernode()
strongSelf.topItemAndNode = nil
if let strongSelf = self, strongSelf.topItemAndNode == nil {
strongSelf.displayingItemsUpdated?(false)
}
}
}
}
containerNode.cancelTimeout = { [weak self] item in
if let strongSelf = self {
if let (topItem, _) = strongSelf.topItemAndNode, topItem.groupingKey != nil && topItem.groupingKey == item.groupingKey {
strongSelf.timeoutTimer?.invalidate()
strongSelf.timeoutTimer = nil
}
}
}
containerNode.resumeTimeout = { [weak self] item in
if let strongSelf = self {
if let (topItem, _) = strongSelf.topItemAndNode, topItem.groupingKey != nil && topItem.groupingKey == item.groupingKey {
strongSelf.resetTimeoutTimer()
}
}
}
self.topItemAndNode = (item, containerNode)
if let blockingItemAndNode = self.blockingItemAndNode {
self.insertSubnode(containerNode, belowSubnode: blockingItemAndNode.1)
} else {
self.addSubnode(containerNode)
}
if let validLayout = self.validLayout {
containerNode.updateLayout(layout: validLayout, transition: .immediate)
containerNode.frame = CGRect(origin: CGPoint(), size: validLayout.size)
containerNode.animateIn()
}
self.displayingItemsUpdated?(true)
self.resetTimeoutTimer()
}
func setBlocking(_ item: NotificationItem?) {
if let (_, blockingItemNode) = self.blockingItemAndNode {
blockingItemNode.animateOut(completion: { [weak blockingItemNode] in
blockingItemNode?.removeFromSupernode()
})
self.blockingItemAndNode = nil
}
if let item = item {
if let (_, topItemNode) = self.topItemAndNode {
topItemNode.animateOut(completion: { [weak topItemNode] in
topItemNode?.removeFromSupernode()
})
}
self.topItemAndNode = nil
var useCompactLayout = false
if let validLayout = self.validLayout {
useCompactLayout = min(validLayout.size.width, validLayout.size.height) < 375.0
}
let itemNode = item.node(compact: useCompactLayout)
let containerNode = NotificationItemContainerNode(theme: self.presentationData.theme, contentNode: itemNode)
containerNode.item = item
containerNode.dismissed = { [weak self] item in
if let strongSelf = self {
if let (topItem, topItemNode) = strongSelf.topItemAndNode, topItem.groupingKey != nil && topItem.groupingKey == item.groupingKey {
topItemNode.removeFromSupernode()
strongSelf.topItemAndNode = nil
if let strongSelf = self, strongSelf.topItemAndNode == nil {
strongSelf.displayingItemsUpdated?(false)
}
}
}
}
containerNode.cancelTimeout = { [weak self] item in
if let strongSelf = self {
if let (topItem, _) = strongSelf.topItemAndNode, topItem.groupingKey != nil && topItem.groupingKey == item.groupingKey {
strongSelf.timeoutTimer?.invalidate()
strongSelf.timeoutTimer = nil
}
}
}
containerNode.resumeTimeout = { [weak self] item in
if let strongSelf = self {
if let (topItem, _) = strongSelf.topItemAndNode, topItem.groupingKey != nil && topItem.groupingKey == item.groupingKey {
strongSelf.resetTimeoutTimer()
}
}
}
self.blockingItemAndNode = (item, containerNode)
self.addSubnode(containerNode)
if let validLayout = self.validLayout {
containerNode.updateLayout(layout: validLayout, transition: .immediate)
containerNode.frame = CGRect(origin: CGPoint(), size: validLayout.size)
containerNode.animateIn()
}
self.displayingItemsUpdated?(true)
}
}
func removeItems(_ f: (NotificationItem) -> Bool) {
if let (topItem, topItemNode) = self.topItemAndNode {
if f(topItem) {
self.topItemAndNode = nil
topItemNode.animateOut(completion: { [weak self, weak topItemNode] in
topItemNode?.removeFromSupernode()
if let strongSelf = self, strongSelf.topItemAndNode == nil {
strongSelf.displayingItemsUpdated?(false)
}
})
}
}
}
private func resetTimeoutTimer() {
self.timeoutTimer?.invalidate()
let timeout: Double
#if DEBUG
timeout = 6.0
#else
timeout = 5.0
#endif
let timeoutTimer = SwiftSignalKit.Timer(timeout: 5.0, repeat: false, completion: { [weak self] in
if let strongSelf = self {
if let (_, topItemNode) = strongSelf.topItemAndNode {
strongSelf.topItemAndNode = nil
strongSelf.displayingItemsUpdated?(false)
topItemNode.animateOut(completion: { [weak topItemNode] in
topItemNode?.removeFromSupernode()
})
}
}
}, queue: Queue.mainQueue())
self.timeoutTimer = timeoutTimer
timeoutTimer.start()
}
}