@@ -16,13 +16,13 @@ class DemoLivestreamChatChannelVC: _ViewController,
16
16
/// Controller for observing data changes within the channel.
17
17
var livestreamChannelController : LivestreamChannelController !
18
18
19
+ /// Controller to observe web socket events.
20
+ lazy var eventsController = livestreamChannelController. client. eventsController ( )
21
+
19
22
/// User search controller for suggestion users when typing in the composer.
20
23
lazy var userSuggestionSearchController : ChatUserSearchController =
21
24
livestreamChannelController. client. userSearchController ( )
22
25
23
- /// A controller for observing web socket events.
24
- lazy var eventsController : EventsController = client. eventsController ( )
25
-
26
26
/// The size of the channel avatar.
27
27
var channelAvatarSize : CGSize {
28
28
CGSize ( width: 32 , height: 32 )
@@ -58,38 +58,8 @@ class DemoLivestreamChatChannelVC: _ViewController,
58
58
messageListVC. listView. isLastCellFullyVisible
59
59
}
60
60
61
- private var isLastMessageVisibleOrSeen : Bool {
62
- isLastMessageFullyVisible
63
- }
64
-
65
61
/// Banner view to show when chat is paused due to scrolling
66
- private lazy var pauseBannerView : UIView = {
67
- let banner = UIView ( )
68
- banner. backgroundColor = appearance. colorPalette. background2
69
- banner. layer. cornerRadius = 12
70
- banner. layer. shadowColor = UIColor . black. cgColor
71
- banner. layer. shadowOffset = CGSize ( width: 0 , height: 2 )
72
- banner. layer. shadowOpacity = 0.1
73
- banner. layer. shadowRadius = 4
74
- banner. translatesAutoresizingMaskIntoConstraints = false
75
-
76
- let label = UILabel ( )
77
- label. text = " Chat paused due to scroll "
78
- label. font = appearance. fonts. footnote
79
- label. textColor = appearance. colorPalette. text
80
- label. textAlignment = . center
81
- label. translatesAutoresizingMaskIntoConstraints = false
82
-
83
- banner. addSubview ( label)
84
- NSLayoutConstraint . activate ( [
85
- label. leadingAnchor. constraint ( equalTo: banner. leadingAnchor, constant: 16 ) ,
86
- label. trailingAnchor. constraint ( equalTo: banner. trailingAnchor, constant: - 16 ) ,
87
- label. topAnchor. constraint ( equalTo: banner. topAnchor, constant: 8 ) ,
88
- label. bottomAnchor. constraint ( equalTo: banner. bottomAnchor, constant: - 8 )
89
- ] )
90
-
91
- return banner
92
- } ( )
62
+ private lazy var pauseBannerView = LivestreamPauseBannerView ( )
93
63
94
64
override func setUp( ) {
95
65
super. setUp ( )
@@ -113,12 +83,6 @@ class DemoLivestreamChatChannelVC: _ViewController,
113
83
messageListVC. swipeToReplyGestureHandler. onReply = { [ weak self] message in
114
84
self ? . messageComposerVC. content. quoteMessage ( message)
115
85
}
116
-
117
- // Initialize messages from controller
118
- messages = livestreamChannelController. messages
119
-
120
- // Initialize pause banner state
121
- pauseBannerView. alpha = 0.0
122
86
}
123
87
124
88
private func setChannelControllerToComposerIfNeeded( ) {
@@ -172,9 +136,6 @@ class DemoLivestreamChatChannelVC: _ViewController,
172
136
constant: - 16
173
137
)
174
138
] )
175
-
176
- // Initially hide the banner
177
- pauseBannerView. isHidden = true
178
139
}
179
140
180
141
override func viewDidAppear( _ animated: Bool ) {
@@ -183,14 +144,6 @@ class DemoLivestreamChatChannelVC: _ViewController,
183
144
keyboardHandler. start ( )
184
145
}
185
146
186
- override func viewWillAppear( _ animated: Bool ) {
187
- super. viewWillAppear ( animated)
188
-
189
- if let draftMessage = livestreamChannelController. channel? . draftMessage {
190
- messageComposerVC. content. draftMessage ( draftMessage)
191
- }
192
- }
193
-
194
147
override func viewWillDisappear( _ animated: Bool ) {
195
148
super. viewWillDisappear ( animated)
196
149
@@ -211,29 +164,6 @@ class DemoLivestreamChatChannelVC: _ViewController,
211
164
messageComposerVC. updateContent ( )
212
165
}
213
166
214
- // MARK: - Actions
215
-
216
- /// Jump to a given message.
217
- /// In case the message is already loaded, it directly goes to it.
218
- /// If not, it will load the messages around it and go to that page.
219
- ///
220
- /// This function is an high-level abstraction of `messageListVC.jumpToMessage(id:onHighlight:)`.
221
- ///
222
- /// - Parameters:
223
- /// - id: The id of message which the message list should go to.
224
- /// - animated: `true` if you want to animate the change in position; `false` if it should be immediate.
225
- /// - shouldHighlight: Whether the message should be highlighted when jumping to it. By default it is highlighted.
226
- func jumpToMessage( id: MessageId , animated: Bool = true , shouldHighlight: Bool = true ) {
227
- if shouldHighlight {
228
- messageListVC. jumpToMessage ( id: id, animated: animated) { [ weak self] indexPath in
229
- self ? . messageListVC. highlightCell ( at: indexPath)
230
- }
231
- return
232
- }
233
-
234
- messageListVC. jumpToMessage ( id: id, animated: animated)
235
- }
236
-
237
167
// MARK: - ChatMessageListVCDataSource
238
168
239
169
var messages : [ ChatMessage ] = [ ]
@@ -298,8 +228,8 @@ class DemoLivestreamChatChannelVC: _ViewController,
298
228
livestreamChannelController. resume ( )
299
229
}
300
230
301
- if isLastMessageFullyVisible {
302
- messageListVC . scrollToBottomButton . isHidden = true
231
+ if ! isLastMessageFullyVisible && !livestreamChannelController . isPaused && scrollView . isDragging {
232
+ livestreamChannelController . pause ( )
303
233
}
304
234
}
305
235
@@ -317,9 +247,6 @@ class DemoLivestreamChatChannelVC: _ViewController,
317
247
318
248
// Load older messages when displaying messages near the end of the array
319
249
if indexPath. item >= messageCount - 10 {
320
- if messageListVC. listView. isDragging && !messageListVC. listView. isLastCellFullyVisible {
321
- livestreamChannelController. pause ( )
322
- }
323
250
livestreamChannelController. loadPreviousMessages ( )
324
251
}
325
252
}
@@ -345,6 +272,11 @@ class DemoLivestreamChatChannelVC: _ViewController,
345
272
}
346
273
case is MarkUnreadActionItem :
347
274
dismiss ( animated: true )
275
+ case is CopyActionItem :
276
+ UIPasteboard . general. string = message. text
277
+ dismiss ( animated: true ) { [ weak self] in
278
+ self ? . presentAlert ( title: " Message copied to clipboard " )
279
+ }
348
280
default :
349
281
return
350
282
}
@@ -420,30 +352,33 @@ class DemoLivestreamChatChannelVC: _ViewController,
420
352
messageListVC. scrollToBottomButton. content = . init( messages: skippedMessagesAmount, mentions: 0 )
421
353
}
422
354
423
- // MARK: - EventsControllerDelegate
355
+ func eventsController( _ controller: EventsController , didReceiveEvent event: any Event ) {
356
+ if event is NewMessagePendingEvent {
357
+ if livestreamChannelController. isPaused {
358
+ pauseBannerView. setState ( . resuming)
359
+ }
360
+ }
424
361
425
- func eventsController( _ controller: EventsController , didReceiveEvent event: Event ) {
426
- if let newMessagePendingEvent = event as? NewMessagePendingEvent {
427
- let newMessage = newMessagePendingEvent. message
428
- if !isFirstPageLoaded && newMessage. isSentByCurrentUser && !newMessage. isPartOfThread {
429
- livestreamChannelController. loadFirstPage ( )
362
+ if let newMessageEvent = event as? MessageNewEvent , newMessageEvent. message. isSentByCurrentUser {
363
+ if livestreamChannelController. isPaused {
364
+ pauseBannerView. setState ( . resuming)
365
+ livestreamChannelController. resume ( )
430
366
}
431
367
}
432
368
}
433
369
434
- /// Shows or hides the pause banner with animation
370
+ /// Shows or hides the pause banner.
435
371
private func showPauseBanner( _ show: Bool ) {
436
- UIView . animate ( withDuration : 0.3 , animations : {
437
- self . pauseBannerView. isHidden = !show
438
- self . pauseBannerView . alpha = show ? 1.0 : 0.0
439
- } )
372
+ if show {
373
+ pauseBannerView. setState ( . paused )
374
+ }
375
+ pauseBannerView . setVisible ( show , animated : true )
440
376
}
441
377
}
442
378
443
379
/// A custom composer view controller for livestream channels that uses LivestreamChannelController
444
380
/// and disables voice recording functionality.
445
381
class DemoLivestreamComposerVC : ComposerVC {
446
- /// Reference to the livestream channel controller
447
382
var livestreamChannelController : LivestreamChannelController ?
448
383
449
384
override func addAttachmentToContent(
0 commit comments