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

Possibility to deselect assets when displaying picker for second time. #69

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 10 additions & 1 deletion DKImagePickerController/DKImagePickerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public class DKImagePickerController: UINavigationController {
}
}
}
/// This allows to clear @selectedAssets without creating new picker each time
public var shouldDeselectAssets: Bool = false

/// The callback block is executed when user pressed the select button.
public var didSelectAssets: ((assets: [DKAsset]) -> Void)?
Expand Down Expand Up @@ -169,7 +171,14 @@ public class DKImagePickerController: UINavigationController {
self.setViewControllers([rootVC], animated: false)
rootVC.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: self.doneButton)
}
}
} else if shouldDeselectAssets {
if let collectionVC = viewControllers[0] as? DKAssetGroupDetailVC {
for asset in selectedAssets {
unselectedImage(asset)
}
collectionVC.collectionView?.reloadData()
}
}
}

private lazy var assetFetchOptions: PHFetchOptions = {
Expand Down
58 changes: 36 additions & 22 deletions DKImagePickerControllerDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import Photos

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate {
var player: MPMoviePlayerController?

var cachedPicker : DKImagePickerController?

@IBOutlet var previewView: UICollectionView?
var assets: [DKAsset]?

Expand All @@ -30,28 +31,40 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
allowMultipleType: Bool,
sourceType: DKImagePickerControllerSourceType = [.Camera, .Photo],
allowsLandscape: Bool,
singleSelect: Bool) {

let pickerController = DKImagePickerController()
pickerController.assetType = assetType
pickerController.allowsLandscape = allowsLandscape
pickerController.allowMultipleTypes = allowMultipleType
pickerController.sourceType = sourceType
pickerController.singleSelect = singleSelect
// pickerController.showsCancelButton = true
// pickerController.showsEmptyAlbums = false
// pickerController.defaultAssetGroup = PHAssetCollectionSubtype.SmartAlbumFavorites
pickerController.defaultSelectedAssets = self.assets

singleSelect: Bool,
shouldDeselect: Bool) {

let pickerController : DKImagePickerController

if let cachedPicker = cachedPicker where shouldDeselect == true {
pickerController = cachedPicker
pickerController.defaultSelectedAssets = nil
} else {
pickerController = DKImagePickerController()
if shouldDeselect {
cachedPicker = pickerController
}
pickerController.assetType = assetType
pickerController.allowsLandscape = allowsLandscape
pickerController.allowMultipleTypes = allowMultipleType
pickerController.sourceType = sourceType
pickerController.singleSelect = singleSelect
// pickerController.showsCancelButton = true
// pickerController.showsEmptyAlbums = false
// pickerController.defaultAssetGroup = PHAssetCollectionSubtype.SmartAlbumFavorites
pickerController.shouldDeselectAssets = shouldDeselect
pickerController.defaultSelectedAssets = self.assets
}

pickerController.didSelectAssets = { [unowned self] (assets: [DKAsset]) in
print("didSelectAssets")

self.assets = assets
self.previewView?.reloadData()
}
if UI_USER_INTERFACE_IDIOM() == .Pad {
pickerController.modalPresentationStyle = .FormSheet;

if UI_USER_INTERFACE_IDIOM() == .Pad {
pickerController.modalPresentationStyle = .FormSheet;
}

self.presentViewController(pickerController, animated: true) {}
Expand Down Expand Up @@ -87,13 +100,13 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega

struct Demo {
static let titles = [
["Pick All", "Pick photos only", "Pick videos only", "Pick All (only photos or videos)"],
["Pick All", "Pick photos only", "Pick videos only", "Pick All (only photos or videos)", "Pick All (reset selection)"],
["Take a picture"],
["Hides camera"],
["Allows landscape"],
["Single select"]
]
static let types: [DKImagePickerControllerAssetType] = [.AllAssets, .AllPhotos, .AllVideos, .AllAssets]
static let types: [DKImagePickerControllerAssetType] = [.AllAssets, .AllPhotos, .AllVideos, .AllAssets, .AllAssets]
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
Expand Down Expand Up @@ -121,13 +134,14 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
(indexPath.section == 2 ? .Photo : [.Camera, .Photo])
let allowsLandscape = indexPath.section == 3
let singleSelect = indexPath.section == 4

let shouldDeselect = indexPath.section == 0 && indexPath.row == 4
showImagePickerWithAssetType(
assetType,
allowMultipleType: allowMultipleType,
sourceType: sourceType,
allowsLandscape: allowsLandscape,
singleSelect: singleSelect
singleSelect: singleSelect,
shouldDeselect: shouldDeselect
)
}

Expand Down