Skip to content

Commit

Permalink
fix: Do not call changeAsset twice when multi-selection is enabled
Browse files Browse the repository at this point in the history
The problem was that we were calling changeAsset twice in rapid succession
when the cell was clicked, causing a problem with the preview if the first
of the videos was slower to load than the second one.
  • Loading branch information
iirovi-rs committed May 22, 2024
1 parent d35c945 commit 85bafee
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Source/Pages/Gallery/YPLibraryVC+CollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ extension YPLibraryVC: UICollectionViewDelegate {
let previouslySelectedIndexPath = IndexPath(row: currentlySelectedIndex, section: 0)
currentlySelectedIndex = indexPath.row

changeAsset(mediaManager.getAsset(at: indexPath.row))
var shouldChangeAsset = true
panGestureHelper.resetToOriginalState()

// Only scroll cell to top if preview is hidden.
Expand All @@ -196,9 +196,11 @@ extension YPLibraryVC: UICollectionViewDelegate {
let cellIsCurrentlySelected = previouslySelectedIndexPath.row == currentlySelectedIndex
if cellIsInTheSelectionPool {
if cellIsCurrentlySelected && !disableAutomaticCellSelection {
shouldChangeAsset = false
deselect(indexPath: indexPath)
}
} else if isLimitExceeded == false {
shouldChangeAsset = false
addToSelection(indexPath: indexPath)
}
if (cellIsCurrentlySelected && !cellIsInTheSelectionPool) || !cellIsCurrentlySelected || disableAutomaticCellSelection {
Expand All @@ -210,7 +212,8 @@ extension YPLibraryVC: UICollectionViewDelegate {
} else if previouslySelectedIndexPath != indexPath {
selectedItems.removeAll()
addToSelection(indexPath: indexPath)

shouldChangeAsset = false

// Force deseletion of previously selected cell.
// In the case where the previous cell was loaded from iCloud, a new image was fetched
// which triggered photoLibraryDidChange() and reloadItems() which breaks selection.
Expand All @@ -220,6 +223,9 @@ extension YPLibraryVC: UICollectionViewDelegate {
}
collectionView.reloadItems(at: [indexPath, previouslySelectedIndexPath])
}
if shouldChangeAsset {
changeAsset(mediaManager.getAsset(at: indexPath.row))
}
disableAutomaticCellSelection = false
}

Expand Down

0 comments on commit 85bafee

Please sign in to comment.