Skip to content

Commit

Permalink
Improvements in reloading cells when using UICollectionViewDiffableDa…
Browse files Browse the repository at this point in the history
…taSource
  • Loading branch information
ekazaev committed May 17, 2024
1 parent ae5783a commit ff10477
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 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 = '2.0.8'
s.version = '2.0.9'
s.summary = 'Chat UI Library. It uses custom UICollectionViewLayout to provide you full control over the presentation.'
s.swift_version = '5.8'

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/Model/ChangeItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ enum ChangeItem: Equatable {
self = .itemDelete(itemIndexPath: indexPath)
}
case .reload:
guard let indexPath = indexPathAfterUpdate else {
guard let indexPath = indexPathBeforeUpdate else {
assertionFailure("`indexPathAfterUpdate` cannot be `nil` for a `.reload` update action.")
return nil
}
Expand Down
28 changes: 14 additions & 14 deletions ChatLayout/Classes/Core/Model/StateController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,6 @@ final class StateController<Layout: ChatLayoutRepresentation> {

reloadedSectionsIndexesArray.append(sectionIndex)
case let .itemReload(itemIndexPath: indexPath):
reloadedIndexes.insert(indexPath)

reloadedItemsIndexesArray.append(indexPath)
case let .itemReconfigure(itemIndexPath: indexPath):
reconfiguredItemsIndexesArray.append(indexPath)
Expand Down Expand Up @@ -741,18 +739,6 @@ final class StateController<Layout: ChatLayoutRepresentation> {
}
}

for indexPath in reloadedItemsIndexesArray {
guard var item = item(for: indexPath.itemPath, kind: .cell, at: .beforeUpdate) else {
assertionFailure("Item at index path (\(indexPath.section) - \(indexPath.item)) does not exist.")
continue
}
let oldHeight = item.frame.height
let configuration = layoutRepresentation.configuration(for: .cell, at: indexPath)
applyConfiguration(configuration, to: &item)
afterUpdateModel.replaceItem(item, at: indexPath)
visibleBoundsBeforeUpdate.offsettingBy(dx: 0, dy: item.frame.height - oldHeight)
}

for indexPath in deletedItemsIndexesArray {
guard let itemId = itemIdentifier(for: indexPath.itemPath, kind: .cell, at: .beforeUpdate) else {
assertionFailure("Item at index path (\(indexPath.section) - \(indexPath.item)) does not exist.")
Expand Down Expand Up @@ -795,6 +781,20 @@ final class StateController<Layout: ChatLayoutRepresentation> {
}
}

for indexPath in reloadedItemsIndexesArray {
guard var item = item(for: indexPath.itemPath, kind: .cell, at: .beforeUpdate),
let indexPathAfterUpdate = afterUpdateModel.itemPath(by: item.id, kind: .cell)?.indexPath else {
assertionFailure("Item at index path (\(indexPath.section) - \(indexPath.item)) does not exist.")
continue
}
reloadedIndexes.insert(indexPathAfterUpdate)
let oldHeight = item.frame.height
let configuration = layoutRepresentation.configuration(for: .cell, at: indexPathAfterUpdate)
applyConfiguration(configuration, to: &item)
afterUpdateModel.replaceItem(item, at: indexPathAfterUpdate)
visibleBoundsBeforeUpdate.offsettingBy(dx: 0, dy: item.frame.height - oldHeight)
}

for indexPath in reconfiguredItemsIndexesArray {
guard var item = item(for: indexPath.itemPath, kind: .cell, at: .beforeUpdate),
let indexPathAfterUpdate = afterUpdateModel.itemPath(by: item.id, kind: .cell)?.indexPath else {
Expand Down

0 comments on commit ff10477

Please sign in to comment.