Skip to content

Commit

Permalink
Merge pull request #370 from kf99916/features/InitSelectedItems
Browse files Browse the repository at this point in the history
Support preselected items
  • Loading branch information
NikKovIos committed Sep 17, 2019
2 parents ef4a012 + bc330b6 commit 7b67129
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Source/Configuration/YPImagePickerConfiguration.swift
Expand Up @@ -186,6 +186,9 @@ public struct YPConfigLibrary {

/// Allow to skip the selections gallery when selecting the multiple media items. Defaults to false.
public var skipSelectionsGallery = false

/// Allow to preselected media items
public var preselectedItems: [YPMediaItem]?
}

/// Encapsulates video specific settings.
Expand Down
4 changes: 4 additions & 0 deletions Source/Pages/Gallery/YPLibraryVC+CollectionView.swift
Expand Up @@ -139,6 +139,10 @@ extension YPLibraryVC: UICollectionViewDelegate {

// Set correct selection number
if let index = selection.firstIndex(where: { $0.assetIdentifier == asset.localIdentifier }) {
let currentSelection = selection[index]
if currentSelection.index < 0 {
selection[index] = YPLibrarySelection(index: indexPath.row, cropRect: currentSelection.cropRect, scrollViewContentOffset: currentSelection.scrollViewContentOffset, scrollViewZoomScale: currentSelection.scrollViewZoomScale, assetIdentifier: currentSelection.assetIdentifier)
}
cell.multipleSelectionIndicator.set(number: index + 1) // start at 1, not 0
} else {
cell.multipleSelectionIndicator.set(number: nil)
Expand Down
28 changes: 27 additions & 1 deletion Source/Pages/Gallery/YPLibraryVC.swift
Expand Up @@ -24,11 +24,15 @@ public class YPLibraryVC: UIViewController, YPPermissionCheckable {

// MARK: - Init

public required init() {
public required init(items: [YPMediaItem]?) {
super.init(nibName: nil, bundle: nil)
title = YPConfig.wordings.libraryTitle
}

public convenience init() {
self.init(items: nil)
}

public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Expand Down Expand Up @@ -62,6 +66,28 @@ public class YPLibraryVC: UIViewController, YPPermissionCheckable {
}
v.assetViewContainer.multipleSelectionButton.isHidden = !(YPConfig.library.maxNumberOfItems > 1)
v.maxNumberWarningLabel.text = String(format: YPConfig.wordings.warningMaxItemsLimit, YPConfig.library.maxNumberOfItems)

if let preselectedItems = YPConfig.library.preselectedItems {
selection = preselectedItems.compactMap { item -> YPLibrarySelection? in
var itemAsset: PHAsset?
switch item {
case .photo(let photo):
itemAsset = photo.asset
case .video(let video):
itemAsset = video.asset
}
guard let asset = itemAsset else {
return nil
}

// The negative index will be corrected in the collectionView:cellForItemAt:
return YPLibrarySelection(index: -1, assetIdentifier: asset.localIdentifier)
}

multipleSelectionEnabled = selection.count > 1
v.assetViewContainer.setMultipleSelectionMode(on: multipleSelectionEnabled)
v.collectionView.reloadData()
}
}

// MARK: - View Lifecycle
Expand Down
Expand Up @@ -169,6 +169,8 @@ class ExampleViewController: UIViewController {
//
//config.library.options = options

config.library.preselectedItems = selectedItems

let picker = YPImagePicker(configuration: config)

/* Change configuration directly */
Expand Down

0 comments on commit 7b67129

Please sign in to comment.