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

Introduced prevent selection delegate #55

Open
wants to merge 1 commit 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
14 changes: 14 additions & 0 deletions ImagePicker/ImagePickerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public protocol ImagePickerControllerDelegate : class {
/// to configure your cell based on asset media type, subtype, etc.
///
func imagePicker(controller: ImagePickerController, willDisplayAssetItem cell: ImagePickerAssetCell, asset: PHAsset)

///
/// Called right before an asset item collection view cell is selected.
/// Return false to prevent selection
///
func imagePicker(controller: ImagePickerController, shouldSelectCellAt: Int) -> Bool
}

//this will make sure all delegate methods are optional
Expand All @@ -63,6 +69,11 @@ extension ImagePickerControllerDelegate {
public func imagePicker(controller: ImagePickerController, didTake image: UIImage) {}
public func imagePicker(controller: ImagePickerController, willDisplayActionItem cell: UICollectionViewCell, at index: Int) {}
public func imagePicker(controller: ImagePickerController, willDisplayAssetItem cell: ImagePickerAssetCell, asset: PHAsset) {}
// public func imagePicker(controller: ImagePickerController, willDisplayAssetItem cell: ImagePickerAssetCell, asset: PHAsset) {}
public func imagePicker(controller: ImagePickerController, shouldSelectCellAt: Int) -> Bool {
// default: Do not prevent selection
return true
}
}


Expand Down Expand Up @@ -425,6 +436,9 @@ extension ImagePickerController: PHPhotoLibraryChangeObserver {
}

extension ImagePickerController : ImagePickerDelegateDelegate {
func imagePicker(delegate: ImagePickerDelegate, shouldSelectCellAt index: Int) -> Bool {
return self.delegate?.imagePicker(controller: self, shouldSelectCellAt: index) ?? true
}

func imagePicker(delegate: ImagePickerDelegate, didSelectActionItemAt index: Int) {
self.delegate?.imagePicker(controller: self, didSelectActionItemAt: index)
Expand Down
4 changes: 4 additions & 0 deletions ImagePicker/ImagePickerDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ protocol ImagePickerDelegateDelegate : class {

//func imagePicker(delegate: ImagePickerDelegate, didEndDisplayingAssetCell cell: ImagePickerAssetCell)
func imagePicker(delegate: ImagePickerDelegate, didScroll scrollView: UIScrollView)

// Called before selection happens
func imagePicker(delegate: ImagePickerDelegate, shouldSelectCellAt index: Int) -> Bool
}

final class ImagePickerDelegate : NSObject, UICollectionViewDelegateFlowLayout {
Expand Down Expand Up @@ -67,6 +70,7 @@ final class ImagePickerDelegate : NSObject, UICollectionViewDelegateFlowLayout {
}

func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
guard delegate?.imagePicker(delegate: self, shouldSelectCellAt: indexPath.item) ?? true else { return false }
guard let configuration = layout?.configuration else { return false }
return selectionPolicy.shouldSelectItem(atSection: indexPath.section, layoutConfiguration: configuration)
}
Expand Down