Skip to content

Commit

Permalink
Improve datasource reconfigure items logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mvasilak committed Jan 5, 2025
1 parent 54d3151 commit 108c60c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ final class PDFAnnotationsViewController: UIViewController {

func reconfigureSelectedCellIfAny() {
guard let key = viewModel.state.selectedAnnotationKey else { return }
var snapshot = dataSource.snapshot()
guard snapshot.itemIdentifiers.contains(where: { $0 == key }) else { return }
updateQueue.async { [weak self] in
guard let self else { return }
var snapshot = dataSource.snapshot()
snapshot.reconfigureItems([key])
dataSource.apply(snapshot, animatingDifferences: false) { [weak self] in
self?.focusSelectedCell()
Expand All @@ -164,7 +165,7 @@ final class PDFAnnotationsViewController: UIViewController {
var snapshot = NSDiffableDataSourceSnapshot<Int, PDFReaderState.AnnotationKey>()
snapshot.appendSections([0])
snapshot.appendItems(state.sortedKeys)
if let keys = state.updatedAnnotationKeys {
if let keys = state.updatedAnnotationKeys?.filter({ snapshot.itemIdentifiers.contains($0) }), !keys.isEmpty {
snapshot.reconfigureItems(keys)
}
dataSource.apply(snapshot, animatingDifferences: animatedDifferences, completion: completion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ class PDFThumbnailsViewController: UICollectionViewController {
return
}

if let index = state.loadedThumbnail, index < dataSource.snapshot().itemIdentifiers.count {
var snapshot = dataSource.snapshot()
if let index = state.loadedThumbnail, index < snapshot.itemIdentifiers.count {
updateQueue.async { [weak self] in
guard let self else { return }
var snapshot = dataSource.snapshot()
let label = dataSource.snapshot().itemIdentifiers[index]
snapshot.reconfigureItems([label])
dataSource.apply(snapshot)
Expand All @@ -119,7 +119,6 @@ class PDFThumbnailsViewController: UICollectionViewController {
}

// The following updates should be ignored if the collection hasn't loaded yet for the first time.
var snapshot = dataSource.snapshot()
guard snapshot.numberOfSections > 0 else { return }

if state.changes.contains(.userInterface) || state.changes.contains(.reload) {
Expand Down

0 comments on commit 108c60c

Please sign in to comment.