@@ -205,6 +205,8 @@ public final class CollectionViewChatLayout: UICollectionViewLayout {
205
205
206
206
private let _flipsHorizontallyInOppositeLayoutDirection : Bool
207
207
208
+ private var reconfigureItemsIndexPaths : [ IndexPath ] = [ ]
209
+
208
210
// MARK: IOS 15.1 fix flags
209
211
210
212
private var needsIOS15_1IssueFix : Bool {
@@ -529,7 +531,7 @@ public final class CollectionViewChatLayout: UICollectionViewLayout {
529
531
let newItemSize = itemSize ( with: preferredMessageAttributes)
530
532
let newInterItemSpacing = interItemSpacing ( for: preferredMessageAttributes. kind, at: preferredMessageAttributes. indexPath)
531
533
let newItemAlignment : ChatItemAlignment
532
- if controller. reloadedIndexes. contains ( preferredMessageAttributes. indexPath) {
534
+ if controller. reloadedIndexes. contains ( preferredMessageAttributes. indexPath) || reconfigureItemsIndexPaths . contains ( preferredMessageAttributes . indexPath ) {
533
535
newItemAlignment = alignment ( for: preferredMessageAttributes. kind, at: preferredMessageAttributes. indexPath)
534
536
} else {
535
537
newItemAlignment = preferredMessageAttributes. alignment
@@ -604,6 +606,12 @@ public final class CollectionViewChatLayout: UICollectionViewLayout {
604
606
return invalidationContext
605
607
}
606
608
609
+ /// If you want to use new `UICollectionView.reconfigureItems(..)` api and expect the reconfiguration to happen animated as well
610
+ // - you must call this method next to the `UICollectionView` one. `UIKit` in its classic way uses private API to process it.
611
+ public func reconfigureItems( at indexPaths: [ IndexPath ] ) {
612
+ reconfigureItemsIndexPaths = indexPaths
613
+ }
614
+
607
615
/// Invalidates the current layout using the information in the provided context object.
608
616
public override func invalidateLayout( with context: UICollectionViewLayoutInvalidationContext ) {
609
617
guard let collectionView else {
@@ -690,6 +698,22 @@ public final class CollectionViewChatLayout: UICollectionViewLayout {
690
698
controller. process ( changeItems: changeItems)
691
699
state = . afterUpdate
692
700
dontReturnAttributes = false
701
+
702
+ if let collectionView,
703
+ !reconfigureItemsIndexPaths. isEmpty {
704
+ reconfigureItemsIndexPaths. filter { collectionView. indexPathsForVisibleItems. contains ( $0) } . forEach { indexPath in
705
+
706
+ let cell = collectionView. cellForItem ( at: indexPath)
707
+
708
+ if let originalAttributes = controller. itemAttributes ( for: indexPath. itemPath, kind: . cell, at: . beforeUpdate) ,
709
+ let preferredAttributes = cell? . preferredLayoutAttributesFitting ( originalAttributes) ,
710
+ shouldInvalidateLayout ( forPreferredLayoutAttributes: preferredAttributes, withOriginalAttributes: originalAttributes) {
711
+ _ = invalidationContext ( forPreferredLayoutAttributes: preferredAttributes, withOriginalAttributes: originalAttributes)
712
+ }
713
+ }
714
+ reconfigureItemsIndexPaths = [ ]
715
+ }
716
+
693
717
super. prepare ( forCollectionViewUpdates: updateItems)
694
718
}
695
719
0 commit comments