Skip to content

Commit

Permalink
Merge pull request #25 from rewardStyle/highlight-deselect
Browse files Browse the repository at this point in the history
fix: highlight not set correctly when deselecting item in multiple se…
  • Loading branch information
MikkoKuivanenRS authored Oct 24, 2023
2 parents 539572d + 8098ca1 commit a340074
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
19 changes: 19 additions & 0 deletions Source/Pages/Gallery/LibraryMediaManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,23 @@ public class LibraryMediaManager {
}
return (imageAsset, imageIndex)
}

func getAsset(with id: String?) -> PHAsset? {
guard let fetchResult, let id else { return nil }
var imageAsset: PHAsset?

// Try first with current fetchResult, doesn't return asset if not in currently selected album
fetchResult.enumerateObjects { (asset: PHAsset, _, stop: UnsafeMutablePointer<ObjCBool>) in
if asset.localIdentifier == id {
imageAsset = asset
stop.pointee = true
}
}

if imageAsset == nil {
imageAsset = PHAsset.fetchAssets(withLocalIdentifiers: [id], options: PHFetchOptions()).firstObject
}

return imageAsset
}
}
17 changes: 8 additions & 9 deletions Source/Pages/Gallery/YPLibraryVC+CollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,14 @@ extension YPLibraryVC {

// Refresh the numbers
let selectedIndexPaths = selectedItems.map { IndexPath(row: $0.index, section: 0) }
v.collectionView.reloadItems(at: selectedIndexPaths)

// Replace the current selected image with the previously selected one
if let previouslySelectedIndexPath = selectedIndexPaths.last {
v.collectionView.deselectItem(at: indexPath, animated: false)
v.collectionView.selectItem(at: previouslySelectedIndexPath, animated: false, scrollPosition: [])
currentlySelectedIndex = previouslySelectedIndexPath.row
changeAsset(mediaManager.getAsset(at: previouslySelectedIndexPath.row))
let asset = mediaManager.getAsset(with: selectedItems.last?.assetIdentifier)
changeAsset(asset)
}
v.collectionView.reloadItems(at: selectedIndexPaths)
checkLimit()
}
}
Expand Down Expand Up @@ -147,7 +145,7 @@ extension YPLibraryVC: UICollectionViewDelegate {
cell.durationLabel.isHidden = !isVideo
cell.durationLabel.text = isVideo ? YPHelper.formattedStrigFrom(asset.duration) : ""
cell.multipleSelectionIndicator.isHidden = !isMultipleSelectionEnabled || (isMultipleSelectionEnabled && isVideo)
cell.isSelected = currentlySelectedIndex == indexPath.row
cell.isSelected = !disableAutomaticCellSelection && currentlySelectedIndex == indexPath.row && selectedItems.contains(where: { $0.assetIdentifier == asset.localIdentifier })
cell.isUserInteractionEnabled = !(isMultipleSelectionEnabled && isVideo)

// Set correct selection number
Expand Down Expand Up @@ -189,14 +187,14 @@ extension YPLibraryVC: UICollectionViewDelegate {
let cellIsInTheSelectionPool = isInSelectionPool(indexPath: indexPath)
let cellIsCurrentlySelected = previouslySelectedIndexPath.row == currentlySelectedIndex
if cellIsInTheSelectionPool {
if cellIsCurrentlySelected {
if cellIsCurrentlySelected && !disableAutomaticCellSelection {
deselect(indexPath: indexPath)
}
} else if isLimitExceeded == false {
addToSelection(indexPath: indexPath)
}

if (cellIsCurrentlySelected && !cellIsInTheSelectionPool) || !cellIsCurrentlySelected {
if (cellIsCurrentlySelected && !cellIsInTheSelectionPool) || !cellIsCurrentlySelected || disableAutomaticCellSelection {
disableAutomaticCellSelection = false
collectionView.cellForItem(at: indexPath)?.isSelected = true
}

Expand All @@ -214,6 +212,7 @@ extension YPLibraryVC: UICollectionViewDelegate {
}
collectionView.reloadItems(at: [indexPath, previouslySelectedIndexPath])
}
disableAutomaticCellSelection = false
}

public func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
Expand Down
8 changes: 4 additions & 4 deletions Source/Pages/Gallery/YPLibraryVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public final class YPLibraryVC: UIViewController, YPPermissionCheckable {
public var currentlySelectedIndex: Int = 0
internal let panGestureHelper = PanGestureHelper()
internal var isInitialized = false
var disableAutomaticCellSelection = false

// MARK: - Init

Expand Down Expand Up @@ -91,6 +92,7 @@ public final class YPLibraryVC: UIViewController, YPPermissionCheckable {
}

func setAlbum(_ album: YPAlbum) {
disableAutomaticCellSelection = isMultipleSelectionEnabled
if YPConfig.showsLibraryButtonInTitle {
title = album.title
} else {
Expand Down Expand Up @@ -252,12 +254,10 @@ public final class YPLibraryVC: UIViewController, YPPermissionCheckable {
}

if mediaManager.hasResultItems,
let firstAsset = mediaManager.getAsset(at: 0) {
let firstAsset = mediaManager.getAsset(with: selectedItems.last?.assetIdentifier) ?? mediaManager.getAsset(at: 0) {
changeAsset(firstAsset)
v.collectionView.reloadData()
v.collectionView.selectItem(at: IndexPath(row: 0, section: 0),
animated: false,
scrollPosition: UICollectionView.ScrollPosition())

if !isMultipleSelectionEnabled && YPConfig.library.preSelectItemOnMultipleSelection {
addToSelection(indexPath: IndexPath(row: 0, section: 0))
}
Expand Down

0 comments on commit a340074

Please sign in to comment.