Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable multiple selection if video is selected #749

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Source/Configuration/YPImagePickerConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ public struct YPConfigLibrary {

/// Pre-selects the current item on setting multiple selection
public var preSelectItemOnMultipleSelection = true

/// Whether to disable selecting multiple items when a video item is selected. Defaults to `false`.
public var shouldDisableMultipleSelectionForVideo = false

/// Anything superior than 1 will enable the multiple selection feature.
public var maxNumberOfItems = 1
Expand Down
39 changes: 25 additions & 14 deletions Source/Pages/Gallery/YPLibraryVC+CollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ extension YPLibraryVC {

/// When tapping on the cell with long press, clear all previously selected cells.
@objc func handleLongPress(longPressGR: UILongPressGestureRecognizer) {
if isMultipleSelectionEnabled || isProcessing || YPConfig.library.maxNumberOfItems <= 1 {
if isMultipleSelectionEnabled
|| isProcessing
|| YPConfig.library.maxNumberOfItems <= 1
|| disableMultipleSelectionForVideo {
return
}

Expand Down Expand Up @@ -167,17 +170,33 @@ extension YPLibraryVC: UICollectionViewDelegate {
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let previouslySelectedIndexPath = IndexPath(row: currentlySelectedIndex, section: 0)
currentlySelectedIndex = indexPath.row

changeAsset(mediaManager.getAsset(at: indexPath.row))
let asset = mediaManager.getAsset(at: indexPath.row)
changeAsset(asset)
panGestureHelper.resetToOriginalState()

// Only scroll cell to top if preview is hidden.
if !panGestureHelper.isImageShown {
collectionView.scrollToItem(at: indexPath, at: .top, animated: true)
}
v.refreshImageCurtainAlpha()

let singleSelection = { [unowned self] in
self.selectedItems.removeAll()
self.addToSelection(indexPath: indexPath)

if isMultipleSelectionEnabled {
// 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.
//
if let previousCell = collectionView.cellForItem(at: previouslySelectedIndexPath) as? YPLibraryViewCell {
previousCell.isSelected = false
}
}

if let asset = asset, asset.mediaType == .video, shouldDisableMultipleSelectionForVideo {
disableMultipleSelectionForVideo = true
singleSelection()
} else if isMultipleSelectionEnabled && !disableMultipleSelectionForVideo {
let cellIsInTheSelectionPool = isInSelectionPool(indexPath: indexPath)
let cellIsCurrentlySelected = previouslySelectedIndexPath.row == currentlySelectedIndex
if cellIsInTheSelectionPool {
Expand All @@ -190,16 +209,8 @@ extension YPLibraryVC: UICollectionViewDelegate {
collectionView.reloadItems(at: [indexPath])
collectionView.reloadItems(at: [previouslySelectedIndexPath])
} else {
selectedItems.removeAll()
addToSelection(indexPath: indexPath)

// 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.
//
if let previousCell = collectionView.cellForItem(at: previouslySelectedIndexPath) as? YPLibraryViewCell {
previousCell.isSelected = false
}
disableMultipleSelectionForVideo = false
singleSelection()
}
}

Expand Down
19 changes: 19 additions & 0 deletions Source/Pages/Gallery/YPLibraryVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ internal final class YPLibraryVC: UIViewController, YPPermissionCheckable {
internal var currentlySelectedIndex: Int = 0
internal let panGestureHelper = PanGestureHelper()
internal var isInitialized = false

internal var disableMultipleSelectionForVideo = YPConfig.library.shouldDisableMultipleSelectionForVideo {
didSet {
// bail out if the multiple selection for video cannot be enabled
guard shouldDisableMultipleSelectionForVideo else { return }
// proceed only if the multiple selection disabling capability is on for this controller
if disableMultipleSelectionForVideo {
isMultipleSelectionEnabled = false
v.assetViewContainer.setMultipleSelectionMode(on: false)
v.collectionView.reloadData()
delegate?.libraryViewDidToggleMultipleSelection(enabled: false)
}
}
}

internal var shouldDisableMultipleSelectionForVideo: Bool {
return YPConfig.library.shouldDisableMultipleSelectionForVideo
}

// MARK: - Init

Expand Down Expand Up @@ -162,6 +180,7 @@ internal final class YPLibraryVC: UIViewController, YPPermissionCheckable {
@objc
func multipleSelectionButtonTapped() {
// If no items, than preventing multiple selection
guard !disableMultipleSelectionForVideo else { return }
guard mediaManager.hasResultItems else {
if #available(iOS 14, *) {
PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self)
Expand Down