Skip to content

Commit

Permalink
Added helper method to support reconfigureItems.
Browse files Browse the repository at this point in the history
  • Loading branch information
ekazaev committed Aug 15, 2023
1 parent 4a08615 commit 4be6c80
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ChatLayout.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'ChatLayout'
s.version = '1.3.3'
s.version = '1.3.4'
s.summary = 'Chat UI Library. It uses custom UICollectionViewLayout to provide you full control over the presentation.'
s.swift_version = '5.8'

Expand Down
26 changes: 25 additions & 1 deletion ChatLayout/Classes/Core/CollectionViewChatLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ public final class CollectionViewChatLayout: UICollectionViewLayout {

private let _flipsHorizontallyInOppositeLayoutDirection: Bool

private var reconfigureItemsIndexPaths: [IndexPath] = []

// MARK: IOS 15.1 fix flags

private var needsIOS15_1IssueFix: Bool {
Expand Down Expand Up @@ -529,7 +531,7 @@ public final class CollectionViewChatLayout: UICollectionViewLayout {
let newItemSize = itemSize(with: preferredMessageAttributes)
let newInterItemSpacing = interItemSpacing(for: preferredMessageAttributes.kind, at: preferredMessageAttributes.indexPath)
let newItemAlignment: ChatItemAlignment
if controller.reloadedIndexes.contains(preferredMessageAttributes.indexPath) {
if controller.reloadedIndexes.contains(preferredMessageAttributes.indexPath) || reconfigureItemsIndexPaths.contains(preferredMessageAttributes.indexPath) {
newItemAlignment = alignment(for: preferredMessageAttributes.kind, at: preferredMessageAttributes.indexPath)
} else {
newItemAlignment = preferredMessageAttributes.alignment
Expand Down Expand Up @@ -604,6 +606,12 @@ public final class CollectionViewChatLayout: UICollectionViewLayout {
return invalidationContext
}

/// If you want to use new `UICollectionView.reconfigureItems(..)` api and expect the reconfiguration to happen animated as well
// - you must call this method next to the `UICollectionView` one. `UIKit` in its classic way uses private API to process it.
public func reconfigureItems(at indexPaths: [IndexPath]) {
reconfigureItemsIndexPaths = indexPaths
}

/// Invalidates the current layout using the information in the provided context object.
public override func invalidateLayout(with context: UICollectionViewLayoutInvalidationContext) {
guard let collectionView else {
Expand Down Expand Up @@ -690,6 +698,22 @@ public final class CollectionViewChatLayout: UICollectionViewLayout {
controller.process(changeItems: changeItems)
state = .afterUpdate
dontReturnAttributes = false

if let collectionView,
!reconfigureItemsIndexPaths.isEmpty {
reconfigureItemsIndexPaths.filter { collectionView.indexPathsForVisibleItems.contains($0) }.forEach { indexPath in

let cell = collectionView.cellForItem(at: indexPath)

if let originalAttributes = controller.itemAttributes(for: indexPath.itemPath, kind: .cell, at: .beforeUpdate),
let preferredAttributes = cell?.preferredLayoutAttributesFitting(originalAttributes),
shouldInvalidateLayout(forPreferredLayoutAttributes: preferredAttributes, withOriginalAttributes: originalAttributes) {
_ = invalidationContext(forPreferredLayoutAttributes: preferredAttributes, withOriginalAttributes: originalAttributes)
}
}
reconfigureItemsIndexPaths = []
}

super.prepare(forCollectionViewUpdates: updateItems)
}

Expand Down

0 comments on commit 4be6c80

Please sign in to comment.