-
Notifications
You must be signed in to change notification settings - Fork 74
/
SBUGroupChannelModule.List.swift
896 lines (761 loc) · 42.7 KB
/
SBUGroupChannelModule.List.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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
//
// SBUGroupChannelModule.List.swift
// SendbirdUIKit
//
// Created by Tez Park on 2021/09/30.
// Copyright © 2021 Sendbird, Inc. All rights reserved.
//
import UIKit
import SendbirdChatSDK
import AVFAudio
/// Event methods for the views updates and performing actions from the list component in a group channel.
public protocol SBUGroupChannelModuleListDelegate: SBUBaseChannelModuleListDelegate {
/// Called when tapped quoted message view in the cell.
/// - Parameters:
/// - didTapQuotedMessageView: `SBUQuotedBaseMessageView` object of the message cell.
func groupChannelModule(_ listComponent: SBUGroupChannelModule.List, didTapQuotedMessageView quotedMessageView: SBUQuotedBaseMessageView)
/// Called when tapped emoji in the cell.
/// - Parameters:
/// - emojiKey: emoji key
/// - messageCell: Message cell object
func groupChannelModule(_ listComponent: SBUGroupChannelModule.List, didTapEmoji emojiKey: String, messageCell: SBUBaseMessageCell)
/// Called when long tapped emoji in the cell.
/// - Parameters:
/// - emojiKey: emoji key
/// - messageCell: Message cell object
func groupChannelModule(_ listComponent: SBUGroupChannelModule.List, didLongTapEmoji emojiKey: String, messageCell: SBUBaseMessageCell)
/// Called when tapped the cell to get more emoji
/// - Parameters:
/// - messageCell: Message cell object
func groupChannelModule(_ listComponent: SBUGroupChannelModule.List, didTapMoreEmojiForCell messageCell: SBUBaseMessageCell)
/// Called when tapped the mentioned nickname in the cell.
/// - Parameters:
/// - user: The`SBUUser` object from the tapped mention.
func groupChannelModule(_ listComponent: SBUGroupChannelModule.List, didTapMentionUser user: SBUUser)
/// Called when tapped the thread info in the cell
/// - Parameter threadInfoView: The `SBUThreadInfoView` object from the tapped thread info.
/// - Since: 3.3.0
func groupChannelModuleDidTapThreadInfoView(_ threadInfoView: SBUThreadInfoView)
/// Called when one of the suggested reply options is tapped.
/// - Parameters:
/// - text: The reply text that is selected by user
/// - Since: 3.11.0
func groupChannelModule(_ listComponent: SBUGroupChannelModule.List, didSelect suggestedReplyOptionView: SBUSuggestedReplyOptionView)
/// Called when selected one of the files in the multiple file message cell.
/// - Parameters:
/// - index: The index number of the selected file in `MultipleFilesMessage.files`
/// - multipleFilesMessageCell: ``SBUMultipleFilesMessageCell`` that contains the tapped file.
/// - cellIndexPath: `IndexPath` value of the ``SBUMultipleFilesMessageCell``.
/// - Since: 3.10.0
func groupChannelModule(
_ listComponent: SBUGroupChannelModule.List,
didSelectFileAt index: Int,
multipleFilesMessageCell: SBUMultipleFilesMessageCell,
forRowAt cellIndexPath: IndexPath
)
/// Called when submit the form answer.
/// - Parameters:
/// - answer: The answer of the form that is submitted by user.
/// - messageCell: Message cell object
/// - Since: 3.11.0
func groupChannelModule(_ listComponent: SBUGroupChannelModule.List, didSubmit answer: SBUForm.Answer, messageCell: SBUBaseMessageCell)
/// Called when updated the form answer.
/// - Parameters:
/// - answer: The answer of the form that is updated by user.
/// - messageCell: Message cell object
/// - Since: 3.11.0
func groupChannelModule(_ listComponent: SBUGroupChannelModule.List, didUpdate answer: SBUForm.Answer, messageCell: SBUBaseMessageCell)
}
/// Methods to get data source for list component in a group channel.
public protocol SBUGroupChannelModuleListDataSource: SBUBaseChannelModuleListDataSource {
/// Ask to data source to return the highlight info
/// - Parameters:
/// - listComponent: `SBUGroupChannelModule.List` object.
/// - tableView: `UITableView` object from list component.
/// - Returns: `SBUHightlightMessageInfo` object.
func groupChannelModule(_ listComponent: SBUGroupChannelModule.List, highlightInfoInTableView tableView: UITableView) -> SBUHighlightMessageInfo?
/// Ask to data source to return the formData by messageId.
/// - Parameters:
/// - listComponent: `SBUGroupChannelModule.List` object.
/// - formAnswerByMessageId: Specific message id.
/// - Returns: `SBUForm.Answer` object.
///
/// - Since: 3.11.0
func groupChannelModule(_ listComponent: SBUGroupChannelModule.List, answersFor messageId: Int64?) -> [SBUForm.Answer]?
}
extension SBUGroupChannelModule {
/// A module component that represent the list of ``SBUGroupChannelModule``.
@objc(SBUGroupChannelModuleList)
@objcMembers open class List: SBUBaseChannelModule.List, SBUVoicePlayerDelegate {
// MARK: - UI properties (Public)
/// The message cell for `AdminMessage` object. Use `register(adminMessageCell:nib:)` to update.
public private(set) var adminMessageCell: SBUBaseMessageCell?
/// The message cell for `UserMessage` object. Use `register(userMessageCell:nib:)` to update.
public private(set) var userMessageCell: SBUBaseMessageCell?
/// The message cell for `FileMessage` object. Use `register(fileMessageCell:nib:)` to update.
public private(set) var fileMessageCell: SBUBaseMessageCell?
/// The message cell for `MultipleFilesMessage` object.
/// Use `register(multipleFilesMessageCell:nib:)` to update.
/// - Since: 3.10.0
public private(set) var multipleFilesMessageCell: SBUBaseMessageCell?
/// The message cell for some unknown message which is not a type of `AdminMessage` | `UserMessage` | ` FileMessage`. Use `register(unknownMessageCell:nib:)` to update.
public private(set) var unknownMessageCell: SBUBaseMessageCell?
/// The custom message cell for some `BaseMessage`. Use `register(customMessageCell:nib:)` to update.
public private(set) var customMessageCell: SBUBaseMessageCell?
/// A high light information of the message with ID and updated time.
public var highlightInfo: SBUHighlightMessageInfo? {
self.dataSource?.groupChannelModule(self, highlightInfoInTableView: self.tableView)
}
/// When message have highlightInfo, it is used to make sure it has been animated.
/// - Since: 3.4.0
public var isHighlightInfoAnimated: Bool = false
// MARK: - Logic properties (Public)
/// The object that acts as the delegate of the list component. The delegate must adopt the `SBUGroupChannelModuleListDelegate`.
public weak var delegate: SBUGroupChannelModuleListDelegate? {
get { self.baseDelegate as? SBUGroupChannelModuleListDelegate }
set { self.baseDelegate = newValue }
}
/// The object that acts as the data source of the list component. The data source must adopt the `SBUGroupChannelModuleListDataSource`.
public weak var dataSource: SBUGroupChannelModuleListDataSource? {
get { self.baseDataSource as? SBUGroupChannelModuleListDataSource }
set { self.baseDataSource = newValue }
}
/// The current *group* channel object casted from `baseChannel`
public var channel: GroupChannel? {
self.baseChannel as? GroupChannel
}
var voicePlayer: SBUVoicePlayer?
var voiceFileInfos: [String: SBUVoiceFileInfo] = [:]
var currentVoiceFileInfo: SBUVoiceFileInfo?
var currentVoiceContentView: SBUVoiceContentView?
var currentVoiceContentIndexPath: IndexPath?
/// Configures component with parameters.
/// - Parameters:
/// - delegate: `SBUGroupChannelModuleListDelegate` type listener
/// - dataSource: The data source that is type of `SBUGroupChannelModuleListDataSource`
/// - theme: `SBUChannelTheme` object
open func configure(
delegate: SBUGroupChannelModuleListDelegate,
dataSource: SBUGroupChannelModuleListDataSource,
theme: SBUChannelTheme
) {
self.delegate = delegate
self.dataSource = dataSource
self.theme = theme
self.setupViews()
self.setupLayouts()
self.setupStyles()
}
// MARK: - LifeCycle
open override func setupViews() {
super.setupViews()
// register cell (GroupChannel)
if self.adminMessageCell == nil {
self.register(adminMessageCell: SBUAdminMessageCell())
}
if self.userMessageCell == nil {
self.register(userMessageCell: SBUUserMessageCell())
}
if self.fileMessageCell == nil {
self.register(fileMessageCell: SBUFileMessageCell())
}
if self.multipleFilesMessageCell == nil {
self.register(messageCell: SBUMultipleFilesMessageCell())
}
if self.unknownMessageCell == nil {
self.register(unknownMessageCell: SBUUnknownMessageCell())
}
if let newMessageInfoView = self.newMessageInfoView {
newMessageInfoView.isHidden = true
self.addSubview(newMessageInfoView)
}
if let scrollBottomView = self.scrollBottomView {
scrollBottomView.isHidden = true
self.addSubview(scrollBottomView)
}
self.voicePlayer = SBUVoicePlayer(delegate: self)
}
open override func setupLayouts() {
super.setupLayouts()
self.channelStateBanner?
.sbu_constraint(equalTo: self, leading: 8, trailing: -8, top: 8)
.sbu_constraint(height: 24)
(self.newMessageInfoView as? SBUNewMessageInfo)?
.sbu_constraint(equalTo: self, bottom: 8, centerX: 0)
self.scrollBottomView?
.sbu_constraint(
width: SBUConstant.scrollBottomButtonSize.width,
height: SBUConstant.scrollBottomButtonSize.height
)
.sbu_constraint(equalTo: self, trailing: -16, bottom: 8)
}
/// Updates styles of the views in the list component with the `theme`.
/// - Parameters:
/// - theme: The object that is used as the theme of the list component. The theme must adopt the `SBUChannelTheme` class. The default value is `nil` to use the stored value.
/// - componentTheme: The object that is used as the theme of some UI component in the list component such as `scrollBottomView`. The theme must adopt the `SBUComponentTheme` class. The default value is `SBUTheme.componentTheme`
open override func updateStyles(theme: SBUChannelTheme? = nil, componentTheme: SBUComponentTheme = SBUTheme.componentTheme) {
super.updateStyles(theme: theme, componentTheme: componentTheme)
if let scrollBottomView = self.scrollBottomView {
setupScrollBottomViewStyle(scrollBottomView: scrollBottomView, theme: componentTheme)
}
(self.newMessageInfoView as? SBUNewMessageInfo)?.setupStyles()
(self.emptyView as? SBUEmptyView)?.setupStyles()
}
// MARK: - Scroll View
open override func setScrollBottomView(hidden: Bool) {
let hasNext = self.dataSource?.baseChannelModule(self, hasNextInTableView: self.tableView) ?? false
let isHidden = hidden && !hasNext
guard self.scrollBottomView?.isHidden != isHidden else { return }
self.scrollBottomView?.isHidden = isHidden
}
open override func scrollViewDidScroll(_ scrollView: UIScrollView) {
super.scrollViewDidScroll(scrollView)
self.setScrollBottomView(hidden: isScrollNearByBottom)
}
// MARK: - EmptyView
// MARK: - Menu
@available(*, deprecated, renamed: "calculateMessageMenuCGPoint(indexPath:position:)")
public func calculatorMenuPoint(
indexPath: IndexPath,
position: MessagePosition
) -> CGPoint {
self.calculateMessageMenuCGPoint(indexPath: indexPath, position: position)
}
/// Calculates the `CGPoint` value that indicates where to draw the message menu in the group channel screen.
/// - Parameters:
/// - indexPath: The index path of the selected message cell
/// - position: Message position
/// - Returns: `CGPoint` value
open func calculateMessageMenuCGPoint(
indexPath: IndexPath,
position: MessagePosition
) -> CGPoint {
let rowRect = self.tableView.rectForRow(at: indexPath)
let rowRectInSuperview = self.tableView.convert(
rowRect,
to: UIApplication.shared.currentWindow
)
let originX = (position == .right) ? rowRectInSuperview.width : rowRectInSuperview.origin.x
let menuPoint = CGPoint(x: originX, y: rowRectInSuperview.origin.y)
return menuPoint
}
open override func createMessageMenuItems(for message: BaseMessage) -> [SBUMenuItem] {
var items = super.createMessageMenuItems(for: message)
switch message {
case is UserMessage, is FileMessage, is MultipleFilesMessage:
if SendbirdUI.config.groupChannel.channel.replyType != .none {
let reply = self.createReplyMenuItem(for: message)
items.append(reply)
}
default: break
}
return items
}
open override func showMessageContextMenu(for message: BaseMessage, cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let messageMenuItems = self.createMessageMenuItems(for: message)
guard !messageMenuItems.isEmpty else { return }
guard let cell = cell as? SBUBaseMessageCell else { return }
let menuPoint = self.calculateMessageMenuCGPoint(indexPath: indexPath, position: cell.position)
SBUMenuView.show(items: messageMenuItems, point: menuPoint) {
cell.isSelected = false
}
}
// MARK: - Actions
/// Sets gestures in message cell.
/// - Parameters:
/// - cell: The message cell
/// - message: message object
/// - indexPath: Cell's indexPath
open func setMessageCellGestures(_ cell: SBUBaseMessageCell, message: BaseMessage, indexPath: IndexPath) {
if let multipleFilesMessageCell = cell as? SBUMultipleFilesMessageCell {
multipleFilesMessageCell.fileSelectHandler = { [weak self] _, index in
guard let self = self else { return }
self.delegate?.groupChannelModule(
self,
didSelectFileAt: index,
multipleFilesMessageCell: multipleFilesMessageCell,
forRowAt: indexPath
)
}
} else {
cell.tapHandlerToContent = { [weak self] in
guard let self = self else { return }
self.setTapGesture(cell, message: message, indexPath: indexPath)
}
}
cell.longPressHandlerToContent = { [weak self] in
guard let self = self else { return }
self.setLongTapGesture(cell, message: message, indexPath: indexPath)
}
}
// MARK: - TableView: Cell
/// Registers a custom cell as a admin message cell based on `SBUBaseMessageCell`.
/// - Parameters:
/// - adminMessageCell: Customized admin message cell
/// - nib: nib information. If the value is nil, the nib file is not used.
/// - Important: To register custom message cell, please use this function before calling `configure(delegate:dataSource:theme:)`
/// ```swift
/// listComponent.register(adminMessageCell: MyAdminMessageCell)
/// listComponent.configure(delegate: self, dataSource: self, theme: theme)
/// ```
open func register(adminMessageCell: SBUBaseMessageCell, nib: UINib? = nil) {
self.adminMessageCell = adminMessageCell
self.register(messageCell: adminMessageCell, nib: nib)
}
/// Registers a custom cell as a user message cell based on `SBUBaseMessageCell`.
/// - Parameters:
/// - userMessageCell: Customized user message cell
/// - nib: nib information. If the value is nil, the nib file is not used.
/// - Important: To register custom message cell, please use this function before calling `configure(delegate:dataSource:theme:)`
/// ```swift
/// listComponent.register(userMessageCell: MyUserMessageCell)
/// listComponent.configure(delegate: self, dataSource: self, theme: theme)
/// ```
open func register(userMessageCell: SBUBaseMessageCell, nib: UINib? = nil) {
self.userMessageCell = userMessageCell
self.register(messageCell: userMessageCell, nib: nib)
}
/// Registers a custom cell as a file message cell based on `SBUBaseMessageCell`.
/// - Parameters:
/// - fileMessageCell: Customized file message cell
/// - nib: nib information. If the value is nil, the nib file is not used.
/// - Important: To register custom message cell, please use this function before calling `configure(delegate:dataSource:theme:)`
/// ```swift
/// listComponent.register(fileMessageCell: MyFileMessageCell)
/// listComponent.configure(delegate: self, dataSource: self, theme: theme)
/// ```
open func register(fileMessageCell: SBUBaseMessageCell, nib: UINib? = nil) {
self.fileMessageCell = fileMessageCell
self.register(messageCell: fileMessageCell, nib: nib)
}
/// Registers a custom cell as a multiple files message cell based on `SBUBaseMessageCell`.
/// - Parameters:
/// - multipleFilesMessageCell: Customized multiple files message cell
/// - nib: nib information. If the value is nil, the nib file is not used.
/// - Important: To register custom message cell, please use this function before calling `configure(delegate:dataSource:theme:)`
/// ```swift
/// listComponent.register(multipleFilesMessageCell: MyMultipleFilesMessageCell)
/// listComponent.configure(delegate: self, dataSource: self, theme: theme)
/// ```
/// - Since: 3.10.0
open func register(multipleFilesMessageCell: SBUBaseMessageCell, nib: UINib? = nil) {
self.multipleFilesMessageCell = multipleFilesMessageCell
self.register(messageCell: multipleFilesMessageCell, nib: nib)
}
/// Registers a custom cell as a unknown message cell based on `SBUBaseMessageCell`.
/// - Parameters:
/// - unknownMessageCell: Customized unknown message cell
/// - nib: nib information. If the value is nil, the nib file is not used.
/// - Important: To register custom message cell, please use this function before calling `configure(delegate:dataSource:theme:)`
/// ```swift
/// listComponent.register(unknownMessageCell: MyUnknownMessageCell)
/// listComponent.configure(delegate: self, dataSource: self, theme: theme)
/// ```
open func register(unknownMessageCell: SBUBaseMessageCell, nib: UINib? = nil) {
self.unknownMessageCell = unknownMessageCell
self.register(messageCell: unknownMessageCell, nib: nib)
}
/// Registers a custom cell as a additional message cell based on `SBUBaseMessageCell`.
/// - Parameters:
/// - customMessageCell: Customized message cell
/// - nib: nib information. If the value is nil, the nib file is not used.
/// - Important: To register custom message cell, please use this function before calling `configure(delegate:dataSource:theme:)`
/// ```swift
/// listComponent.register(customMessageCell: MyCustomMessageCell)
/// listComponent.configure(delegate: self, dataSource: self, theme: theme)
/// ```
open func register(customMessageCell: SBUBaseMessageCell, nib: UINib? = nil) {
self.customMessageCell = customMessageCell
self.register(messageCell: customMessageCell, nib: nib)
}
/// Configures cell with message for a particular row.
/// - Parameters:
/// - messageCell: `SBUBaseMessageCell` object.
/// - message: The message for `messageCell`.
/// - indexPath: An index path representing the `messageCell`
open func configureCell(_ messageCell: SBUBaseMessageCell, message: BaseMessage, forRowAt indexPath: IndexPath) {
guard let channel = self.channel else {
SBULog.error("Channel must exist!")
return
}
// NOTE: to disable unwanted animation while configuring cells
UIView.setAnimationsEnabled(false)
let isSameDay = self.checkSameDayAsNextMessage(
currentIndex: indexPath.row,
fullMessageList: fullMessageList
)
let receiptState = SBUUtils.getReceiptState(of: message, in: channel)
let useReaction = SBUEmojiManager.isReactionEnabled(channel: self.channel)
switch (message, messageCell) {
// Admin message
case let (adminMessage, adminMessageCell) as (AdminMessage, SBUAdminMessageCell):
let configuration = SBUAdminMessageCellParams(
message: adminMessage,
hideDateView: isSameDay
)
adminMessageCell.configure(with: configuration)
self.setMessageCellAnimation(adminMessageCell, message: adminMessage, indexPath: indexPath)
self.setMessageCellGestures(adminMessageCell, message: adminMessage, indexPath: indexPath)
// Unknown message
case let (unknownMessage, unknownMessageCell) as (BaseMessage, SBUUnknownMessageCell):
let configuration = SBUUnknownMessageCellParams(
message: unknownMessage,
hideDateView: isSameDay,
groupPosition: self.getMessageGroupingPosition(currentIndex: indexPath.row),
receiptState: receiptState,
useReaction: useReaction,
joinedAt: self.channel?.joinedAt ?? 0,
messageOffsetTimestamp: self.channel?.messageOffsetTimestamp ?? 0
)
unknownMessageCell.configure(with: configuration)
self.setMessageCellAnimation(unknownMessageCell, message: unknownMessage, indexPath: indexPath)
self.setMessageCellGestures(unknownMessageCell, message: unknownMessage, indexPath: indexPath)
// User message
case let (userMessage, userMessageCell) as (UserMessage, SBUUserMessageCell):
let isLatestUserMessage = userMessage.messageId == fullMessageList.first(where: { $0.sender != nil })?.messageId
let formAnswers = self.dataSource?.groupChannelModule(self, answersFor: userMessage.messageId)
let configuration = SBUUserMessageCellParams(
message: userMessage,
hideDateView: isSameDay,
useMessagePosition: true,
groupPosition: self.getMessageGroupingPosition(currentIndex: indexPath.row),
receiptState: receiptState,
useReaction: useReaction,
withTextView: true,
joinedAt: self.channel?.joinedAt ?? 0,
messageOffsetTimestamp: self.channel?.messageOffsetTimestamp ?? 0,
shouldHideSuggestedReplies: !isLatestUserMessage,
shouldHideFormTypeMessage: false, // only group channel
formAnswers: formAnswers
)
userMessageCell.configure(with: configuration)
userMessageCell.configure(highlightInfo: self.highlightInfo)
(userMessageCell.quotedMessageView as? SBUQuotedBaseMessageView)?.delegate = self
(userMessageCell.threadInfoView as? SBUThreadInfoView)?.delegate = self
self.setMessageCellAnimation(userMessageCell, message: userMessage, indexPath: indexPath)
self.setMessageCellGestures(userMessageCell, message: userMessage, indexPath: indexPath)
// File message
case let (fileMessage, fileMessageCell) as (FileMessage, SBUFileMessageCell):
let voiceFileInfo = self.voiceFileInfos[fileMessage.cacheKey] ?? nil
let configuration = SBUFileMessageCellParams(
message: fileMessage,
hideDateView: isSameDay,
useMessagePosition: true,
groupPosition: self.getMessageGroupingPosition(currentIndex: indexPath.row),
receiptState: receiptState,
useReaction: useReaction,
joinedAt: self.channel?.joinedAt ?? 0,
messageOffsetTimestamp: self.channel?.messageOffsetTimestamp ?? 0,
voiceFileInfo: voiceFileInfo
)
if voiceFileInfo != nil {
self.currentVoiceFileInfo = nil
self.currentVoiceContentView = nil
}
fileMessageCell.configure(with: configuration)
fileMessageCell.configure(highlightInfo: self.highlightInfo)
(fileMessageCell.quotedMessageView as? SBUQuotedBaseMessageView)?.delegate = self
(fileMessageCell.threadInfoView as? SBUThreadInfoView)?.delegate = self
self.setMessageCellAnimation(fileMessageCell, message: fileMessage, indexPath: indexPath)
self.setMessageCellGestures(fileMessageCell, message: fileMessage, indexPath: indexPath)
self.setFileMessageCellImage(fileMessageCell, fileMessage: fileMessage)
if let voiceFileInfo = voiceFileInfo,
voiceFileInfo.isPlaying == true,
let voiceContentView = fileMessageCell.baseFileContentView as? SBUVoiceContentView {
self.currentVoiceContentIndexPath = indexPath
self.currentVoiceFileInfo = voiceFileInfo
self.currentVoiceContentView = voiceContentView
}
case let (multipleFilesMessage, multipleFilesMessageCell) as (MultipleFilesMessage, SBUMultipleFilesMessageCell):
let configuration = SBUMultipleFilesMessageCellParams(
message: multipleFilesMessage,
hideDateView: isSameDay,
useMessagePosition: true,
receiptState: receiptState,
useReaction: true
)
multipleFilesMessageCell.configure(with: configuration)
(multipleFilesMessageCell.quotedMessageView as? SBUQuotedBaseMessageView)?.delegate = self
self.setMessageCellAnimation(multipleFilesMessageCell, message: multipleFilesMessage, indexPath: indexPath)
self.setMessageCellGestures(multipleFilesMessageCell, message: multipleFilesMessage, indexPath: indexPath)
(multipleFilesMessageCell.threadInfoView as? SBUThreadInfoView)?.delegate = self
default:
let configuration = SBUBaseMessageCellParams(
message: message,
hideDateView: isSameDay,
messagePosition: .center,
groupPosition: .none,
receiptState: receiptState,
joinedAt: self.channel?.joinedAt ?? 0,
messageOffsetTimestamp: self.channel?.messageOffsetTimestamp ?? 0
)
messageCell.configure(with: configuration)
}
UIView.setAnimationsEnabled(true)
// TODO: Move to `setMessageCellGestures`?
messageCell.userProfileTapHandler = { [weak messageCell, weak self] in
guard let self = self else { return }
guard let cell = messageCell else { return }
guard let sender = cell.message?.sender else { return }
self.setUserProfileTapGesture(SBUUser(sender: sender))
}
// Reaction action
messageCell.emojiTapHandler = { [weak messageCell, weak self] emojiKey in
guard let self = self else { return }
guard let cell = messageCell else { return }
self.delegate?.groupChannelModule(self, didTapEmoji: emojiKey, messageCell: cell)
}
messageCell.emojiLongPressHandler = { [weak messageCell, weak self] emojiKey in
guard let self = self else { return }
guard let cell = messageCell else { return }
self.delegate?.groupChannelModule(self, didLongTapEmoji: emojiKey, messageCell: cell)
}
messageCell.moreEmojiTapHandler = { [weak messageCell, weak self] in
guard let self = self else { return }
guard let cell = messageCell else { return }
self.delegate?.groupChannelModule(self, didTapMoreEmojiForCell: cell)
}
messageCell.mentionTapHandler = { [weak self] user in
guard let self = self else { return }
self.delegate?.groupChannelModule(self, didTapMentionUser: user)
}
messageCell.suggestedReplySelectHandler = { [weak self] optionView in
guard let self = self else { return }
self.delegate?.groupChannelModule(self, didSelect: optionView)
}
messageCell.submitFormAnswerHandler = { [weak self] answer, cell in
guard let self = self else { return }
self.delegate?.groupChannelModule(self, didSubmit: answer, messageCell: cell)
}
messageCell.updateFormAnswerHandler = { [weak self] answer, cell in
guard let self = self else { return }
self.delegate?.groupChannelModule(self, didUpdate: answer, messageCell: cell)
}
}
open override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard indexPath.row < self.fullMessageList.count else {
SBULog.error("The index is out of range.")
return .init()
}
let message = fullMessageList[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: self.generateCellIdentifier(by: message)) ?? UITableViewCell()
cell.contentView.transform = CGAffineTransform(scaleX: 1, y: -1)
cell.selectionStyle = .none
guard let messageCell = cell as? SBUBaseMessageCell else {
SBULog.error("There are no message cells!")
return cell
}
self.configureCell(messageCell, message: message, forRowAt: indexPath)
return cell
}
open override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let fileMessageCell = cell as? SBUFileMessageCell,
let _ = fileMessageCell.baseFileContentView as? SBUVoiceContentView else { return }
}
/// Register the message cell to the table view.
public func register(messageCell: SBUBaseMessageCell, nib: UINib? = nil) {
if let nib = nib {
self.tableView.register(
nib,
forCellReuseIdentifier: messageCell.sbu_className
)
} else {
self.tableView.register(
type(of: messageCell), forCellReuseIdentifier: messageCell.sbu_className)
}
}
/// Generates identifier of message cell.
/// - Parameter message: Message object
/// - Returns: The identifier of message cell.
open func generateCellIdentifier(by message: BaseMessage) -> String {
switch message {
case is MultipleFilesMessage:
return multipleFilesMessageCell?.sbu_className ?? SBUMultipleFilesMessageCell.sbu_className
case is FileMessage:
return fileMessageCell?.sbu_className ?? SBUFileMessageCell.sbu_className
case is UserMessage:
return userMessageCell?.sbu_className ?? SBUUserMessageCell.sbu_className
case is AdminMessage:
return adminMessageCell?.sbu_className ?? SBUAdminMessageCell.sbu_className
default:
return unknownMessageCell?.sbu_className ?? SBUUnknownMessageCell.sbu_className
}
}
/// Sets animation in message cell.
/// - Parameters:
/// - cell: The message cell
/// - message: message object
/// - indexPath: Cell's indexPath
open func setMessageCellAnimation(_ messageCell: SBUBaseMessageCell, message: BaseMessage, indexPath: IndexPath) {
if message.messageId == highlightInfo?.messageId,
message.updatedAt == highlightInfo?.updatedAt,
self.highlightInfo?.animated == true,
self.isHighlightInfoAnimated == false {
self.cellAnimationDebouncer.add {
messageCell.messageContentView.animate(.shakeUpDown)
self.isHighlightInfoAnimated = true
}
}
}
// MARK: - Menu
}
}
extension SBUGroupChannelModule.List: SBUQuotedMessageViewDelegate {
open func didTapQuotedMessageView(_ quotedMessageView: SBUQuotedBaseMessageView) {
self.delegate?.groupChannelModule(self, didTapQuotedMessageView: quotedMessageView)
}
}
extension SBUGroupChannelModule.List: SBUThreadInfoViewDelegate {
open func threadInfoViewDidTap(_ threadInfoView: SBUThreadInfoView) {
self.delegate?.groupChannelModuleDidTapThreadInfoView(threadInfoView)
}
}
// MARK: - Voice message
extension SBUGroupChannelModule.List {
func pauseVoicePlayer() {
self.currentVoiceFileInfo?.isPlaying = false
self.voicePlayer?.pause()
}
func pauseVoicePlayer(cacheKey: String) {
if let voiceFileInfo = self.voiceFileInfos[cacheKey],
voiceFileInfo.isPlaying == true {
voiceFileInfo.isPlaying = false
self.voicePlayer?.pause()
}
}
func pauseAllVoicePlayer() {
self.currentVoiceFileInfo?.isPlaying = false
self.voicePlayer?.pause()
for (_, value) in self.voiceFileInfos {
value.isPlaying = false
}
}
/// Updates voice message
///
/// - Note: As a default, it's called from `baseChannelModule(_:didTapVoiceMessage:cell:forRowAt:)` delegate method.
///
/// - Parameters:
/// - cell: The message cell
/// - message: message object
/// - indexPath: Cell's indexPath
///
/// - Since: 3.4.0
func updateVoiceMessage(_ cell: SBUBaseMessageCell, message: BaseMessage, indexPath: IndexPath) {
guard let fileMessageCell = cell as? SBUFileMessageCell,
let fileMessage = message as? FileMessage,
let voiceContentView = fileMessageCell.baseFileContentView as? SBUVoiceContentView,
SBUUtils.getFileType(by: fileMessage) == .voice else { return }
if self.voiceFileInfos[fileMessage.cacheKey] == nil {
voiceContentView.updateVoiceContentStatus(.loading)
}
SBUCacheManager.File.loadFile(
urlString: fileMessage.url,
cacheKey: fileMessage.cacheKey,
fileName: fileMessage.name
) { [weak self] filePath, _ in
var playtime: Double = 0
let metaArrays = message.metaArrays(keys: [SBUConstant.voiceMessageDurationKey])
if metaArrays.count > 0 {
let value = metaArrays[0].value[0]
playtime = Double(value) ?? 0
}
guard let filePath = filePath else {
self?.pauseAllVoicePlayer()
voiceContentView.updateVoiceContentStatus(.none, time: playtime)
return
}
if voiceContentView.status == .loading || voiceContentView.status == .none {
voiceContentView.updateVoiceContentStatus(.prepared)
}
var voicefileInfo: SBUVoiceFileInfo?
if self?.voiceFileInfos[fileMessage.cacheKey] == nil {
voicefileInfo = SBUVoiceFileInfo(
fileName: fileMessage.name,
filePath: filePath,
playtime: playtime,
currentPlayTime: 0
)
self?.voiceFileInfos[fileMessage.cacheKey] = voicefileInfo
} else {
voicefileInfo = self?.voiceFileInfos[fileMessage.cacheKey]
}
var actionInSameView = false
if let voicefileInfo = voicefileInfo {
if self?.currentVoiceFileInfo?.isPlaying == true {
// updated status of previously contentView
let currentPlayTime = self?.currentVoiceFileInfo?.currentPlayTime ?? 0
self?.currentVoiceFileInfo?.isPlaying = false
self?.currentVoiceContentView?.updateVoiceContentStatus(.pause, time: currentPlayTime)
if self?.currentVoiceContentView == voiceContentView {
actionInSameView = true
}
}
self?.voicePlayer?.configure(voiceFileInfo: voicefileInfo)
}
if let voicefileInfo = voicefileInfo {
self?.voicePlayer?.configure(voiceFileInfo: voicefileInfo)
self?.currentVoiceContentIndexPath = indexPath
}
if self?.currentVoiceFileInfo != voicefileInfo {
self?.pauseAllVoicePlayer()
}
self?.currentVoiceFileInfo = voicefileInfo
self?.currentVoiceContentView = voiceContentView
switch voiceContentView.status {
case .none:
break
case .loading:
break
case .prepared:
self?.voicePlayer?.play()
case .playing:
self?.voicePlayer?.pause()
case .pause:
if actionInSameView == true { break }
let currentPlayTime = self?.currentVoiceFileInfo?.currentPlayTime ?? 0
self?.voicePlayer?.play(fromTime: currentPlayTime)
case .finishPlaying:
self?.voicePlayer?.play()
}
}
}
// MARK: - SBUVoicePlayerDelegate
public func voicePlayerDidReceiveError(_ player: SBUVoicePlayer, errorStatus: SBUVoicePlayerErrorStatus) {}
public func voicePlayerDidStart(_ player: SBUVoicePlayer) {
let currentPlayTime = self.currentVoiceFileInfo?.currentPlayTime ?? 0
self.currentVoiceFileInfo?.isPlaying = true
if let indexPath = self.currentVoiceContentIndexPath,
let cell = self.tableView.cellForRow(at: indexPath) as? SBUFileMessageCell,
let voiceContentView = cell.baseFileContentView as? SBUVoiceContentView {
voiceContentView.updateVoiceContentStatus(.playing, time: currentPlayTime)
}
}
public func voicePlayerDidPause(_ player: SBUVoicePlayer, voiceFileInfo: SBUVoiceFileInfo?) {
let currentPlayTime = self.currentVoiceFileInfo?.currentPlayTime ?? 0
self.currentVoiceFileInfo?.isPlaying = false
if let indexPath = self.currentVoiceContentIndexPath,
let cell = self.tableView.cellForRow(at: indexPath) as? SBUFileMessageCell,
let voiceContentView = cell.baseFileContentView as? SBUVoiceContentView {
voiceContentView.updateVoiceContentStatus(.pause, time: currentPlayTime)
}
}
public func voicePlayerDidStop(_ player: SBUVoicePlayer) {
let time = self.currentVoiceFileInfo?.playtime ?? 0
self.currentVoiceFileInfo?.isPlaying = false
if let indexPath = self.currentVoiceContentIndexPath,
let cell = self.tableView.cellForRow(at: indexPath) as? SBUFileMessageCell,
let voiceContentView = cell.baseFileContentView as? SBUVoiceContentView {
voiceContentView.updateVoiceContentStatus(.finishPlaying, time: time)
}
}
public func voicePlayerDidReset(_ player: SBUVoicePlayer) {}
public func voicePlayerDidUpdatePlayTime(_ player: SBUVoicePlayer, time: TimeInterval) {
self.currentVoiceFileInfo?.currentPlayTime = time
self.currentVoiceFileInfo?.isPlaying = true
if let indexPath = self.currentVoiceContentIndexPath,
let cell = self.tableView.cellForRow(at: indexPath) as? SBUFileMessageCell,
let voiceContentView = cell.baseFileContentView as? SBUVoiceContentView {
voiceContentView.updateVoiceContentStatus(.playing, time: time)
}
}
}