Skip to content

Commit 4be6c80

Browse files
committed
Added helper method to support reconfigureItems.
1 parent 4a08615 commit 4be6c80

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

ChatLayout.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'ChatLayout'
3-
s.version = '1.3.3'
3+
s.version = '1.3.4'
44
s.summary = 'Chat UI Library. It uses custom UICollectionViewLayout to provide you full control over the presentation.'
55
s.swift_version = '5.8'
66

ChatLayout/Classes/Core/CollectionViewChatLayout.swift

+25-1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ public final class CollectionViewChatLayout: UICollectionViewLayout {
205205

206206
private let _flipsHorizontallyInOppositeLayoutDirection: Bool
207207

208+
private var reconfigureItemsIndexPaths: [IndexPath] = []
209+
208210
// MARK: IOS 15.1 fix flags
209211

210212
private var needsIOS15_1IssueFix: Bool {
@@ -529,7 +531,7 @@ public final class CollectionViewChatLayout: UICollectionViewLayout {
529531
let newItemSize = itemSize(with: preferredMessageAttributes)
530532
let newInterItemSpacing = interItemSpacing(for: preferredMessageAttributes.kind, at: preferredMessageAttributes.indexPath)
531533
let newItemAlignment: ChatItemAlignment
532-
if controller.reloadedIndexes.contains(preferredMessageAttributes.indexPath) {
534+
if controller.reloadedIndexes.contains(preferredMessageAttributes.indexPath) || reconfigureItemsIndexPaths.contains(preferredMessageAttributes.indexPath) {
533535
newItemAlignment = alignment(for: preferredMessageAttributes.kind, at: preferredMessageAttributes.indexPath)
534536
} else {
535537
newItemAlignment = preferredMessageAttributes.alignment
@@ -604,6 +606,12 @@ public final class CollectionViewChatLayout: UICollectionViewLayout {
604606
return invalidationContext
605607
}
606608

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+
607615
/// Invalidates the current layout using the information in the provided context object.
608616
public override func invalidateLayout(with context: UICollectionViewLayoutInvalidationContext) {
609617
guard let collectionView else {
@@ -690,6 +698,22 @@ public final class CollectionViewChatLayout: UICollectionViewLayout {
690698
controller.process(changeItems: changeItems)
691699
state = .afterUpdate
692700
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+
693717
super.prepare(forCollectionViewUpdates: updateItems)
694718
}
695719

0 commit comments

Comments
 (0)