Skip to content

Commit ff10477

Browse files
committed
Improvements in reloading cells when using UICollectionViewDiffableDataSource
1 parent ae5783a commit ff10477

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
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 = '2.0.8'
3+
s.version = '2.0.9'
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/Model/ChangeItem.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ enum ChangeItem: Equatable {
8383
self = .itemDelete(itemIndexPath: indexPath)
8484
}
8585
case .reload:
86-
guard let indexPath = indexPathAfterUpdate else {
86+
guard let indexPath = indexPathBeforeUpdate else {
8787
assertionFailure("`indexPathAfterUpdate` cannot be `nil` for a `.reload` update action.")
8888
return nil
8989
}

ChatLayout/Classes/Core/Model/StateController.swift

+14-14
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,6 @@ final class StateController<Layout: ChatLayoutRepresentation> {
559559

560560
reloadedSectionsIndexesArray.append(sectionIndex)
561561
case let .itemReload(itemIndexPath: indexPath):
562-
reloadedIndexes.insert(indexPath)
563-
564562
reloadedItemsIndexesArray.append(indexPath)
565563
case let .itemReconfigure(itemIndexPath: indexPath):
566564
reconfiguredItemsIndexesArray.append(indexPath)
@@ -741,18 +739,6 @@ final class StateController<Layout: ChatLayoutRepresentation> {
741739
}
742740
}
743741

744-
for indexPath in reloadedItemsIndexesArray {
745-
guard var item = item(for: indexPath.itemPath, kind: .cell, at: .beforeUpdate) else {
746-
assertionFailure("Item at index path (\(indexPath.section) - \(indexPath.item)) does not exist.")
747-
continue
748-
}
749-
let oldHeight = item.frame.height
750-
let configuration = layoutRepresentation.configuration(for: .cell, at: indexPath)
751-
applyConfiguration(configuration, to: &item)
752-
afterUpdateModel.replaceItem(item, at: indexPath)
753-
visibleBoundsBeforeUpdate.offsettingBy(dx: 0, dy: item.frame.height - oldHeight)
754-
}
755-
756742
for indexPath in deletedItemsIndexesArray {
757743
guard let itemId = itemIdentifier(for: indexPath.itemPath, kind: .cell, at: .beforeUpdate) else {
758744
assertionFailure("Item at index path (\(indexPath.section) - \(indexPath.item)) does not exist.")
@@ -795,6 +781,20 @@ final class StateController<Layout: ChatLayoutRepresentation> {
795781
}
796782
}
797783

784+
for indexPath in reloadedItemsIndexesArray {
785+
guard var item = item(for: indexPath.itemPath, kind: .cell, at: .beforeUpdate),
786+
let indexPathAfterUpdate = afterUpdateModel.itemPath(by: item.id, kind: .cell)?.indexPath else {
787+
assertionFailure("Item at index path (\(indexPath.section) - \(indexPath.item)) does not exist.")
788+
continue
789+
}
790+
reloadedIndexes.insert(indexPathAfterUpdate)
791+
let oldHeight = item.frame.height
792+
let configuration = layoutRepresentation.configuration(for: .cell, at: indexPathAfterUpdate)
793+
applyConfiguration(configuration, to: &item)
794+
afterUpdateModel.replaceItem(item, at: indexPathAfterUpdate)
795+
visibleBoundsBeforeUpdate.offsettingBy(dx: 0, dy: item.frame.height - oldHeight)
796+
}
797+
798798
for indexPath in reconfiguredItemsIndexesArray {
799799
guard var item = item(for: indexPath.itemPath, kind: .cell, at: .beforeUpdate),
800800
let indexPathAfterUpdate = afterUpdateModel.itemPath(by: item.id, kind: .cell)?.indexPath else {

0 commit comments

Comments
 (0)