@@ -32,6 +32,7 @@ final class ChatViewController: UIViewController {
32
32
case scrollingToBottom
33
33
case showingPreview
34
34
case showingAccessory
35
+ case updatingCollectionInIsolation
35
36
}
36
37
37
38
private enum ControllerActions {
@@ -124,6 +125,7 @@ final class ChatViewController: UIViewController {
124
125
chatLayout. settings. interSectionSpacing = 8
125
126
chatLayout. settings. additionalInsets = UIEdgeInsets ( top: 8 , left: 5 , bottom: 8 , right: 5 )
126
127
chatLayout. keepContentOffsetAtBottomOnBatchUpdates = true
128
+ chatLayout. processOnlyVisibleItemsOnAnimatedBatchUpdates = false
127
129
128
130
collectionView = UICollectionView ( frame: view. frame, collectionViewLayout: chatLayout)
129
131
view. addSubview ( collectionView)
@@ -156,7 +158,7 @@ final class ChatViewController: UIViewController {
156
158
currentControllerActions. options. insert ( . loadingInitialMessages)
157
159
chatController. loadInitialMessages { sections in
158
160
self . currentControllerActions. options. remove ( . loadingInitialMessages)
159
- self . processUpdates ( with: sections, animated: true )
161
+ self . processUpdates ( with: sections, animated: true , requiresIsolatedProcess : false )
160
162
}
161
163
162
164
KeyboardListener . shared. add ( delegate: self )
@@ -298,7 +300,7 @@ extension ChatViewController: UIScrollViewDelegate {
298
300
}
299
301
// Reloading the content without animation just because it looks better is the scrolling is in process.
300
302
let animated = !self . isUserInitiatedScrolling
301
- self . processUpdates ( with: sections, animated: animated) {
303
+ self . processUpdates ( with: sections, animated: animated, requiresIsolatedProcess : false ) {
302
304
self . currentControllerActions. options. remove ( . loadingPreviousMessages)
303
305
}
304
306
}
@@ -441,11 +443,11 @@ extension ChatViewController: UICollectionViewDelegate {
441
443
442
444
extension ChatViewController : ChatControllerDelegate {
443
445
444
- func update( with sections: [ Section ] ) {
445
- processUpdates ( with: sections, animated: true )
446
+ func update( with sections: [ Section ] , requiresIsolatedProcess : Bool ) {
447
+ processUpdates ( with: sections, animated: true , requiresIsolatedProcess : requiresIsolatedProcess )
446
448
}
447
449
448
- private func processUpdates( with sections: [ Section ] , animated: Bool = true , completion: ( ( ) -> Void ) ? = nil ) {
450
+ private func processUpdates( with sections: [ Section ] , animated: Bool = true , requiresIsolatedProcess : Bool , completion: ( ( ) -> Void ) ? = nil ) {
449
451
guard isViewLoaded else {
450
452
dataSource. sections = sections
451
453
return
@@ -459,7 +461,7 @@ extension ChatViewController: ChatControllerDelegate {
459
461
guard let self = self else {
460
462
return
461
463
}
462
- self . processUpdates ( with: sections, animated: animated, completion: completion)
464
+ self . processUpdates ( with: sections, animated: animated, requiresIsolatedProcess : requiresIsolatedProcess , completion: completion)
463
465
} )
464
466
currentInterfaceActions. add ( reaction: reaction)
465
467
return
@@ -475,6 +477,10 @@ extension ChatViewController: ChatControllerDelegate {
475
477
return
476
478
}
477
479
480
+ if requiresIsolatedProcess {
481
+ chatLayout. processOnlyVisibleItemsOnAnimatedBatchUpdates = true
482
+ currentInterfaceActions. options. insert ( . updatingCollectionInIsolation)
483
+ }
478
484
currentControllerActions. options. insert ( . updatingCollection)
479
485
collectionView. reload ( using: changeSet,
480
486
interrupt: { changeSet in
@@ -491,6 +497,10 @@ extension ChatViewController: ChatControllerDelegate {
491
497
} ,
492
498
completion: { _ in
493
499
DispatchQueue . main. async {
500
+ self . chatLayout. processOnlyVisibleItemsOnAnimatedBatchUpdates = false
501
+ if requiresIsolatedProcess {
502
+ self . currentInterfaceActions. options. remove ( . updatingCollectionInIsolation)
503
+ }
494
504
completion ? ( )
495
505
self . currentControllerActions. options. remove ( . updatingCollection)
496
506
}
@@ -595,7 +605,7 @@ extension ChatViewController: InputBarAccessoryViewDelegate {
595
605
self . scrollToBottom ( completion: {
596
606
self . chatController. sendMessage ( . text( messageText) ) { sections in
597
607
self . currentInterfaceActions. options. remove ( . sendingMessage)
598
- self . processUpdates ( with: sections, animated: true )
608
+ self . processUpdates ( with: sections, animated: true , requiresIsolatedProcess : false )
599
609
}
600
610
} )
601
611
}
0 commit comments