Skip to content

Commit

Permalink
Improved updates processing
Browse files Browse the repository at this point in the history
  • Loading branch information
ekazaev committed Oct 10, 2023
1 parent 894651e commit 56b8aee
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 253 deletions.
91 changes: 5 additions & 86 deletions ChatLayout/Classes/Core/CollectionViewChatLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -538,18 +538,8 @@ open class CollectionViewChatLayout: UICollectionViewLayout {
let layoutAttributesForPendingAnimation = attributesForPendingAnimations[preferredMessageAttributes.kind]?[preferredAttributesItemPath]

let newItemSize = itemSize(with: preferredMessageAttributes)
let newInterItemSpacing: CGFloat
let newItemAlignment: ChatItemAlignment
// if controller.reloadedIndexes.contains(preferredMessageAttributes.indexPath) || controller.reconfiguredIndexes.contains(preferredMessageAttributes.indexPath),
// controller.insertedIndexes.contains(preferredMessageAttributes.indexPath) || controller.insertedSectionsIndexes.contains(preferredMessageAttributes.indexPath.section) {
newItemAlignment = alignment(for: preferredMessageAttributes.kind, at: preferredMessageAttributes.indexPath)
newInterItemSpacing = interItemSpacing(for: preferredMessageAttributes.kind, at: preferredMessageAttributes.indexPath)
print("A: \(preferredMessageAttributes.indexPath.item) \(newItemAlignment) - \(newInterItemSpacing)")
// } else {
// newItemAlignment = preferredMessageAttributes.alignment
// newInterItemSpacing = preferredMessageAttributes.interItemSpacing
// print("b: \(preferredMessageAttributes.indexPath.item) \(newItemAlignment) - \(newInterItemSpacing)")
// }
let newItemAlignment = alignment(for: preferredMessageAttributes.kind, at: preferredMessageAttributes.indexPath)
let newInterItemSpacing = interItemSpacing(for: preferredMessageAttributes.kind, at: preferredMessageAttributes.indexPath)
controller.update(preferredSize: newItemSize,
alignment: newItemAlignment,
interItemSpacing: newInterItemSpacing,
Expand Down Expand Up @@ -700,55 +690,9 @@ open class CollectionViewChatLayout: UICollectionViewLayout {

// MARK: Responding to Collection View Updates

private class MockUICollectionViewUpdateItem: UICollectionViewUpdateItem {

// swiftlint:disable identifier_name
var _indexPathBeforeUpdate: IndexPath?
var _indexPathAfterUpdate: IndexPath?
var _updateAction: Action
// swiftlint:enable identifier_name

init(indexPathBeforeUpdate: IndexPath?, indexPathAfterUpdate: IndexPath?, action: Action) {
_indexPathBeforeUpdate = indexPathBeforeUpdate
_indexPathAfterUpdate = indexPathAfterUpdate
_updateAction = action
super.init()
}

override var indexPathBeforeUpdate: IndexPath? {
_indexPathBeforeUpdate
}

override var indexPathAfterUpdate: IndexPath? {
_indexPathAfterUpdate
}

override var updateAction: Action {
_updateAction
}

}

/// Notifies the layout object that the contents of the collection view are about to change.
open override func prepare(forCollectionViewUpdates updateItems: [UICollectionViewUpdateItem]) {
print("\(updateItems)")
// let updateItems = updateItems.reduce(into: [UICollectionViewUpdateItem](), { result, item in
// switch item.updateAction {
// case .delete:
// result.append(MockUICollectionViewUpdateItem(indexPathBeforeUpdate: item.indexPathBeforeUpdate, indexPathAfterUpdate: item.indexPathAfterUpdate, action: .delete))
// case .insert:
// result.append(MockUICollectionViewUpdateItem(indexPathBeforeUpdate: item.indexPathBeforeUpdate, indexPathAfterUpdate: item.indexPathAfterUpdate, action: .insert))
// case .reload:
// result.append(MockUICollectionViewUpdateItem(indexPathBeforeUpdate: item.indexPathBeforeUpdate, indexPathAfterUpdate: item.indexPathAfterUpdate, action: .reload))
// case .move:
// result.append(MockUICollectionViewUpdateItem(indexPathBeforeUpdate: item.indexPathBeforeUpdate, indexPathAfterUpdate: nil, action: .delete))
// result.append(MockUICollectionViewUpdateItem(indexPathBeforeUpdate: nil, indexPathAfterUpdate: item.indexPathAfterUpdate, action: .insert))
// case .none:
// break
// @unknown default:
// break
// }
// })
print("\(#function) \(updateItems)")
var changeItems = updateItems.compactMap { ChangeItem(with: $0) }
changeItems.append(contentsOf: reconfigureItemsIndexPaths.map { .itemReconfigure(itemIndexPath: $0) })
controller.process(changeItems: changeItems)
Expand Down Expand Up @@ -838,6 +782,7 @@ open class CollectionViewChatLayout: UICollectionViewLayout {
attributes = controller.itemAttributes(for: itemPath, kind: .cell, at: .beforeUpdate)
}

print("\(#function) \(attributes)")
return attributes
}

Expand Down Expand Up @@ -887,6 +832,7 @@ open class CollectionViewChatLayout: UICollectionViewLayout {
attributes = controller.itemAttributes(for: itemPath, kind: .cell, at: .beforeUpdate)
}

print("\(#function) \(attributes)")
return attributes
}

Expand Down Expand Up @@ -1109,30 +1055,3 @@ extension CollectionViewChatLayout {
}

}

extension UICollectionViewUpdateItem: Comparable {
public static func < (lhs: UICollectionViewUpdateItem, rhs: UICollectionViewUpdateItem) -> Bool {
switch (lhs.updateAction, rhs.updateAction) {
case (.delete, .delete):
if let lIndexPathBeforeUpdate = lhs.indexPathBeforeUpdate,
let rIndexPathBeforeUpdate = rhs.indexPathBeforeUpdate {
if lIndexPathBeforeUpdate.item == NSNotFound, rIndexPathBeforeUpdate.item == NSNotFound {
return lIndexPathBeforeUpdate.section > rIndexPathBeforeUpdate.section
} else {
return lIndexPathBeforeUpdate > rIndexPathBeforeUpdate
}
}
default:
if let lIndexPathBeforeUpdate = lhs.indexPathBeforeUpdate,
let rIndexPathBeforeUpdate = rhs.indexPathBeforeUpdate {
if lIndexPathBeforeUpdate.item == NSNotFound, rIndexPathBeforeUpdate.item == NSNotFound {
return lIndexPathBeforeUpdate.section < rIndexPathBeforeUpdate.section
} else {
return lIndexPathBeforeUpdate < rIndexPathBeforeUpdate
}
}
}
return (lhs.indexPathBeforeUpdate ?? IndexPath(item: 0, section: 0)) < (rhs.indexPathBeforeUpdate ?? IndexPath(item: 0, section: 0))
}

}
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/Model/LayoutModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class LayoutModel<Layout: ChatLayoutRepresentation> {
for sectionIndex in 0..<directlyMutableSections.count {
sectionIndexByIdentifierCache[directlyMutableSections[sectionIndex].id] = sectionIndex
directlyMutableSections[sectionIndex].offsetY = offsetY
offsetY += directlyMutableSections[sectionIndex].height + (sectionIndex != directlyMutableSections.count - 1 ? directlyMutableSections[sectionIndex].interSectionSpacing : 0)
offsetY += directlyMutableSections[sectionIndex].height + (sectionIndex < directlyMutableSections.count - 1 ? directlyMutableSections[sectionIndex].interSectionSpacing : 0)
if let header = directlyMutableSections[sectionIndex].header {
let key = ItemUUIDKey(kind: .header, id: header.id)
itemPathByIdentifierCache[key] = ItemPath(item: 0, section: sectionIndex)
Expand Down
3 changes: 2 additions & 1 deletion ChatLayout/Classes/Core/Model/SectionModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ struct SectionModel<Layout: ChatLayoutRepresentation> {
items.withUnsafeMutableBufferPointer { directlyMutableItems in
for rowIndex in 0..<directlyMutableItems.count {
directlyMutableItems[rowIndex].offsetY = offsetY
offsetY += directlyMutableItems[rowIndex].size.height + (rowIndex != directlyMutableItems.count - 1 ? directlyMutableItems[rowIndex].interItemSpacing : 0)
let offset: CGFloat = rowIndex < directlyMutableItems.count - 1 ? directlyMutableItems[rowIndex].interItemSpacing : 0
offsetY += directlyMutableItems[rowIndex].size.height + offset
}
}

Expand Down
Loading

0 comments on commit 56b8aee

Please sign in to comment.