Skip to content

Commit

Permalink
Improve ExpandableCollectionsCollectionViewHandler data source handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mvasilak committed Jan 6, 2025
1 parent 079e494 commit 9b0a611
Showing 1 changed file with 4 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ final class ExpandableCollectionsCollectionViewHandler: NSObject {
collectionView.delegate = self
collectionView.dropDelegate = self
dataSource = createDataSource(for: collectionView)
collectionView.dataSource = dataSource
updateQueue.async { [weak self] in
guard let self else { return }
var snapshot = NSDiffableDataSourceSnapshot<Int, Collection>()
Expand Down Expand Up @@ -74,6 +75,7 @@ final class ExpandableCollectionsCollectionViewHandler: NSObject {
}
}

guard let dataSource = dataSource else { return }
let snapshot = dataSource.snapshot(for: collectionsSection)

if !snapshot.items.contains(where: { $0.identifier == collectionId }) {
Expand All @@ -89,7 +91,7 @@ final class ExpandableCollectionsCollectionViewHandler: NSObject {
return
}

if let index = dataSource.snapshot(for: collectionsSection).visibleItems.firstIndex(where: { $0.identifier == collectionId }) {
if let index = snapshot.visibleItems.firstIndex(where: { $0.identifier == collectionId }) {
let indexPath = IndexPath(item: index, section: 0)
guard selectedIndexPaths.first != indexPath else { return }
collectionView.selectItem(at: indexPath, animated: false, scrollPosition: scrollToPosition ? .centeredVertically : [])
Expand All @@ -99,6 +101,7 @@ final class ExpandableCollectionsCollectionViewHandler: NSObject {
}

func update(with tree: CollectionTree, selectedId: CollectionIdentifier, animated: Bool, completion: (() -> Void)? = nil) {
guard let dataSource = dataSource else { return }
let newSnapshot = tree.createSnapshot(selectedId: selectedId)

if dataSource.snapshot(for: collectionsSection).items.count == newSnapshot.items.count {
Expand Down

0 comments on commit 9b0a611

Please sign in to comment.