-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathDemoChatChannelListVC.swift
More file actions
472 lines (395 loc) 路 15.4 KB
/
Copy pathDemoChatChannelListVC.swift
File metadata and controls
472 lines (395 loc) 路 15.4 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//
// Copyright 漏 2026 Stream.io Inc. All rights reserved.
//
import Foundation
import StreamChat
import StreamChatUI
import UIKit
final class DemoChatChannelListVC: ChatChannelListVC {
/// The `UIButton` instance used for navigating to new channel screen creation.
lazy var createChannelButton: UIButton = {
let button = UIButton()
button.setImage(UIImage(systemName: "plus.message")!, for: .normal)
return button
}()
lazy var filterChannelsButton: UIButton = {
let button = UIButton()
button.setImage(UIImage(systemName: "slider.horizontal.3")!, for: .normal)
return button
}()
private lazy var eventsController = controller.client.eventsController()
private lazy var connectionController = controller.client.connectionController()
private lazy var connectionDelegate = BannerShowingConnectionDelegate(
showUnder: navigationController!.navigationBar
)
var highlightSelectedChannel: Bool { splitViewController?.isCollapsed == false }
var selectedChannel: ChatChannel?
var currentUserId: UserId {
controller.client.currentUserId!
}
var initialQuery: ChannelListQuery!
lazy var hiddenChannelsQuery: ChannelListQuery = .init(filter: .and([
.containMembers(userIds: [currentUserId]),
.equal(.hidden, to: true)
]))
lazy var allBlockedChannelsQuery: ChannelListQuery = .init(filter: .and([
.containMembers(userIds: [currentUserId]),
.or([
.equal(.blocked, to: false),
.equal(.blocked, to: true)
])
]))
lazy var blockedUnblockedWithHiddenChannelsQuery: ChannelListQuery = .init(
filter: .and([
.containMembers(userIds: [currentUserId]),
.or([
.and([.equal(.blocked, to: false), .equal(.hidden, to: false)]),
.and([.equal(.blocked, to: true), .equal(.hidden, to: true)])
])
])
)
lazy var channelModeratorChannelsQuery: ChannelListQuery = .init(
filter: .and([
.containMembers(userIds: [currentUserId]),
.equal(.channelRole, to: "channel_moderator")
])
)
lazy var unreadCountChannelsQuery: ChannelListQuery = .init(
filter: .and([
.containMembers(userIds: [currentUserId]),
.hasUnread
]),
sort: [.init(key: .unreadCount, isAscending: false)]
)
lazy var sortedByHasUnreadChannelsQuery: ChannelListQuery = .init(
filter: .and([
.containMembers(userIds: [currentUserId])
]),
sort: [
.init(key: .hasUnread, isAscending: false),
.init(key: .updatedAt, isAscending: false)
]
)
lazy var mutedChannelsQuery: ChannelListQuery = .init(filter: .and([
.containMembers(userIds: [currentUserId]),
.equal(.muted, to: true)
]))
lazy var coolChannelsQuery: ChannelListQuery = .init(filter: .and([
.containMembers(userIds: [currentUserId]),
.equal("is_cool", to: true)
]))
lazy var archivedChannelsQuery: ChannelListQuery = .init(filter: .and([
.containMembers(userIds: [currentUserId]),
.equal(.archived, to: true)
]))
lazy var pinnedChannelsQuery: ChannelListQuery = .init(filter: .and([
.containMembers(userIds: [currentUserId]),
.equal(.pinned, to: true)
]))
lazy var equalMembersQuery: ChannelListQuery = .init(filter:
.equal(.members, values: [currentUserId, "r2-d2"])
)
lazy var premiumTaggedChannelsQuery: ChannelListQuery = .init(filter: .in(.filterTags, values: ["premium"]))
lazy var predefinedMessagingChannelsQuery: ChannelListQuery = .init(
predefinedFilter: "user_per_channel_type_channels",
filterValues: ["channel_type": .string(ChannelType.messaging.rawValue), "user_id": .string(currentUserId)],
sortValues: nil
)
lazy var predefinedArchivedChannelsQuery: ChannelListQuery = .init(
predefinedFilter: "user_per_channel_type_archived_hidden",
filterValues: [
"channel_type": .string(ChannelType.messaging.rawValue),
"hidden": .bool(false),
"user_id": .string(currentUserId),
"archived": .bool(true)
],
sortValues: nil
)
lazy var predefinedHiddenChannelsQuery: ChannelListQuery = .init(
predefinedFilter: "user_per_channel_type_archived_hidden",
filterValues: [
"channel_type": .string(ChannelType.messaging.rawValue),
"hidden": .bool(true),
"user_id": .string(currentUserId),
"archived": .bool(false)
],
sortValues: nil
)
lazy var livestreamChannelsQuery: ChannelListQuery = .init(filter: .equal(.type, to: .livestream))
var demoRouter: DemoChatChannelListRouter? {
router as? DemoChatChannelListRouter
}
override func viewDidLoad() {
super.viewDidLoad()
title = "Channels"
initialQuery = controller.query
if AppConfig.shared.demoAppConfig.shouldShowConnectionBanner {
connectionController.delegate = connectionDelegate
}
navigationItem.rightBarButtonItems = [
UIBarButtonItem(customView: filterChannelsButton),
UIBarButtonItem(customView: createChannelButton)
]
createChannelButton.addTarget(self, action: #selector(didTapCreateNewChannel), for: .touchUpInside)
filterChannelsButton.addTarget(self, action: #selector(didTapFilterChannelsButton), for: .touchUpInside)
emptyView.actionButtonPressed = { [weak self] in
guard let self = self else { return }
self.didTapCreateNewChannel(self)
}
}
override func setUpLayout() {
super.setUpLayout()
if isChatChannelListStatesEnabled {
NSLayoutConstraint.activate([
channelListErrorView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
channelListErrorView.heightAnchor.constraint(equalToConstant: 60)
])
}
}
@objc private func didTapCreateNewChannel(_ sender: Any) {
demoRouter?.showCreateNewChannelFlow()
}
@objc private func didTapFilterChannelsButton(_ sender: Any) {
let defaultChannelsAction = UIAlertAction(
title: "Initial Channels",
style: .default,
handler: { [weak self] _ in
self?.title = "Channels"
self?.setInitialChannelsQuery()
}
)
let hiddenChannelsAction = UIAlertAction(
title: "Hidden Channels",
style: .default,
handler: { [weak self] _ in
self?.title = "Hidden Channels"
self?.setHiddenChannelsQuery()
}
)
let allBlockedChannelsAction = UIAlertAction(
title: "Blocked and Non-Blocked Channels",
style: .default,
handler: { [weak self] _ in
self?.title = "All Blocked Channels"
self?.setAllBlockedChannelsQuery()
}
)
let blockedUnBlockedExcludingDeletedChannelsAction = UIAlertAction(
title: "Blocked Unblocked with Matching Hidden Channels",
style: .default,
handler: { [weak self] _ in
self?.title = "Blocked Unblocked with Matching Hidden Channels"
self?.setBlockedUnblockedWithHiddenChannelsQuery()
}
)
let channelRoleChannelsAction = UIAlertAction(
title: "Moderator Channels",
style: .default,
handler: { [weak self] _ in
self?.title = "Moderator Channels"
self?.setChannelModeratorChannelsQuery()
}
)
let unreadCountChannelsAction = UIAlertAction(
title: "Unread Count Channels",
style: .default,
handler: { [weak self] _ in
self?.title = "Unread Count Channels"
self?.setUnreadCountChannelsQuery()
}
)
let hasUnreadChannelsAction = UIAlertAction(
title: "Sorted hasUnread Channels",
style: .default,
handler: { [weak self] _ in
self?.title = "Sorted hasUnread Channels"
self?.setSortedByHasUnreadChannelsQuery()
}
)
let coolChannelsAction = UIAlertAction(
title: "Cool Channels",
style: .default,
handler: { [weak self] _ in
self?.title = "Cool Channels"
self?.setCoolChannelsQuery()
}
)
let mutedChannelsAction = UIAlertAction(
title: "Muted Channels",
style: .default,
handler: { [weak self] _ in
self?.title = "Muted Channels"
self?.setMutedChannelsQuery()
}
)
let archivedChannelsAction = UIAlertAction(
title: "Archived Channels",
style: .default
) { [weak self] _ in
self?.title = "Archived Channels"
self?.setArchivedChannelsQuery()
}
let pinnedChannelsAction = UIAlertAction(
title: "Pinned Channels",
style: .default
) { [weak self] _ in
self?.title = "Pinned Channels"
self?.setPinnedChannelsQuery()
}
let equalMembersAction = UIAlertAction(
title: "R2-D2 Channels (Equal Members)",
style: .default
) { [weak self] _ in
self?.title = "R2-D2 Channels (Equal Members)"
self?.setEqualMembersChannelsQuery()
}
let taggedChannelsAction = UIAlertAction(
title: "Premium Tagged Channels",
style: .default,
handler: { [weak self] _ in
self?.title = "Premium Tagged Channels"
self?.setPremiumTaggedChannelsQuery()
}
)
let predefinedMessagingChannelsAction = UIAlertAction(
title: "Messaging (Predefined)",
style: .default,
handler: { [weak self] _ in
self?.title = "Messaging (Predefined)"
self?.setPredefinedMessagingChannelsQuery()
}
)
let predefinedArchivedChannelsAction = UIAlertAction(
title: "Archived (Predefined)",
style: .default,
handler: { [weak self] _ in
self?.title = "Archived (Predefined)"
self?.setPredefinedArchivedChannelsQuery()
}
)
let predefinedHiddenChannelsAction = UIAlertAction(
title: "Hidden (Predefined)",
style: .default,
handler: { [weak self] _ in
self?.title = "Hidden (Predefined)"
self?.setPredefinedHiddenChannelsQuery()
}
)
let livestreamChannelsAction = UIAlertAction(
title: "Livestream Channels",
style: .default
) { [weak self] _ in
self?.title = "Livestream Channels"
self?.setLivestreamChannelsQuery()
}
presentAlert(
title: "Filter Channels",
actions: [
defaultChannelsAction,
unreadCountChannelsAction,
hasUnreadChannelsAction,
hiddenChannelsAction,
allBlockedChannelsAction,
blockedUnBlockedExcludingDeletedChannelsAction,
mutedChannelsAction,
coolChannelsAction,
pinnedChannelsAction,
archivedChannelsAction,
equalMembersAction,
channelRoleChannelsAction,
taggedChannelsAction,
predefinedMessagingChannelsAction,
predefinedArchivedChannelsAction,
predefinedHiddenChannelsAction,
livestreamChannelsAction
].sorted(by: { $0.title ?? "" < $1.title ?? "" }),
preferredStyle: .actionSheet,
sourceView: filterChannelsButton
)
}
func setHiddenChannelsQuery() {
replaceQuery(hiddenChannelsQuery)
}
func setAllBlockedChannelsQuery() {
replaceQuery(allBlockedChannelsQuery)
}
func setBlockedUnblockedWithHiddenChannelsQuery() {
replaceQuery(blockedUnblockedWithHiddenChannelsQuery)
}
func setChannelModeratorChannelsQuery() {
replaceQuery(channelModeratorChannelsQuery)
}
func setUnreadCountChannelsQuery() {
replaceQuery(unreadCountChannelsQuery)
}
func setSortedByHasUnreadChannelsQuery() {
replaceQuery(sortedByHasUnreadChannelsQuery)
}
func setMutedChannelsQuery() {
replaceQuery(mutedChannelsQuery)
}
func setCoolChannelsQuery() {
let controller = self.controller.client.channelListController(
query: coolChannelsQuery,
filter: { channel in
channel.extraData["is_cool"]?.boolValue ?? false
}
)
replaceChannelListController(controller)
}
func setArchivedChannelsQuery() {
replaceQuery(archivedChannelsQuery)
}
func setPinnedChannelsQuery() {
replaceQuery(pinnedChannelsQuery)
}
func setEqualMembersChannelsQuery() {
replaceQuery(equalMembersQuery)
}
func setPremiumTaggedChannelsQuery() {
replaceQuery(premiumTaggedChannelsQuery)
}
func setPredefinedMessagingChannelsQuery() {
replaceQuery(predefinedMessagingChannelsQuery)
}
func setPredefinedArchivedChannelsQuery() {
replaceQuery(predefinedArchivedChannelsQuery)
}
func setPredefinedHiddenChannelsQuery() {
replaceQuery(predefinedHiddenChannelsQuery)
}
func setLivestreamChannelsQuery() {
replaceQuery(livestreamChannelsQuery)
}
func setInitialChannelsQuery() {
replaceQuery(initialQuery)
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.deselectItem(at: indexPath, animated: true)
let channel = controller.channels[indexPath.row]
selectedChannel = controller.channels[indexPath.row]
router.showChannel(for: channel.cid)
}
override func controller(_ controller: DataController, didChangeState state: DataController.State) {
super.controller(controller, didChangeState: state)
if highlightSelectedChannel && (state == .remoteDataFetched || state == .localDataFetched) && selectedChannel == nil {
guard let channel = self.controller.channels.first else { return }
router.showChannel(for: channel.cid)
selectedChannel = channel
}
}
override func controller(_ controller: ChatChannelListController, didChangeChannels changes: [ListChange<ChatChannel>]) {
super.controller(controller, didChangeChannels: changes)
guard highlightSelectedChannel else { return }
guard let selectedChannel = selectedChannel else { return }
guard let selectedChannelRow = controller.channels.firstIndex(of: selectedChannel) else {
return
}
let selectedItemIndexPath = IndexPath(row: selectedChannelRow, section: 0)
collectionView.selectItem(
at: selectedItemIndexPath,
animated: false,
scrollPosition: .centeredHorizontally
)
}
}