Skip to content

Commit

Permalink
added fetchCollectionOption
Browse files Browse the repository at this point in the history
  • Loading branch information
tilltue committed Aug 21, 2019
1 parent ce21a10 commit a8aae72
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ public struct TLPhotosPickerConfigure {
public var singleSelectedMode = false
public var maxSelectedAssets: Int? = nil //default: inf
public var fetchOption: PHFetchOptions? = nil //default: creationDate
public var fetchCollectionOption: [FetchCollectionType: PHFetchOptions] = [:]
public var singleSelectedMode = false
public var selectedColor = UIColor(red: 88/255, green: 144/255, blue: 255/255, alpha: 1.0)
public var cameraBgColor = UIColor(red: 221/255, green: 223/255, blue: 226/255, alpha: 1)
Expand All @@ -304,6 +305,18 @@ public struct TLPhotosPickerConfigure {
}
}

//Related issue: https://github.com/tilltue/TLPhotoPicker/issues/201
//e.g.
//let option = PHFetchOptions()
//configure.fetchCollectionOption[.assetCollections(.smartAlbum)] = option
//configure.fetchCollectionOption[.assetCollections(.album)] = option
//configure.fetchCollectionOption[.topLevelUserCollections] = option

public enum FetchCollectionType {
case assetCollections(PHAssetCollectionType)
case topLevelUserCollections
}

public enum PopupConfigure {
//Popup album view animation duration
case animation(TimeInterval)
Expand Down
2 changes: 1 addition & 1 deletion TLPhotoPicker.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'TLPhotoPicker'
s.version = '1.9.6'
s.version = '1.9.7'
s.summary = 'multiple phassets picker for iOS lib. like facebook'

# This description is used to generate tags and improve search results.
Expand Down
31 changes: 24 additions & 7 deletions TLPhotoPicker/Classes/TLPhotoLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,13 @@ extension TLPhotoLibrary {
func fetchCollection(configure: TLPhotosPickerConfigure) {
let useCameraButton = configure.usedCameraButton
let options = getOption(configure: configure)
let fetchCollectionOption = configure.fetchCollectionOption

func getAlbum(subType: PHAssetCollectionSubtype, result: inout [TLAssetsCollection]) {
let fetchCollection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: subType, options: nil)
let collectionOption = fetchCollectionOption[.assetCollections(.album)]
let fetchCollection = PHAssetCollection.fetchAssetCollections(with: .album,
subtype: subType,
options: collectionOption)
var collections = [PHAssetCollection]()
fetchCollection.enumerateObjects { (collection, index, _) in
if configure.allowedAlbumCloudShared == false && collection.assetCollectionSubtype == .albumCloudShared {
Expand All @@ -184,9 +188,19 @@ extension TLPhotoLibrary {
}

@discardableResult
func getSmartAlbum(subType: PHAssetCollectionSubtype, useCameraButton: Bool = false, result: inout [TLAssetsCollection]) -> TLAssetsCollection? {
let fetchCollection = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: subType, options: nil)
if let collection = fetchCollection.firstObject, !result.contains(where: { $0.localIdentifier == collection.localIdentifier }) {
func getSmartAlbum(subType: PHAssetCollectionSubtype,
useCameraButton: Bool = false,
result: inout [TLAssetsCollection])
-> TLAssetsCollection?
{
let collectionOption = fetchCollectionOption[.assetCollections(.smartAlbum)]
let fetchCollection = PHAssetCollection.fetchAssetCollections(with: .smartAlbum,
subtype: subType,
options: collectionOption)
if
let collection = fetchCollection.firstObject,
result.contains(where: { $0.localIdentifier == collection.localIdentifier }) == false
{
var assetsCollection = TLAssetsCollection(collection: collection)
assetsCollection.fetchResult = PHAsset.fetchAssets(in: collection, options: options)
if assetsCollection.count > 0 || useCameraButton {
Expand All @@ -196,7 +210,7 @@ extension TLPhotoLibrary {
}
return nil
}
if let fetchCollectionTypes: [(PHAssetCollectionType,PHAssetCollectionSubtype)] = configure.fetchCollectionTypes {
if let fetchCollectionTypes = configure.fetchCollectionTypes {
DispatchQueue.global(qos: .userInteractive).async { [weak self] in
var assetCollections = [TLAssetsCollection]()
for (type,subType) in fetchCollectionTypes {
Expand All @@ -214,7 +228,9 @@ extension TLPhotoLibrary {
DispatchQueue.global(qos: .userInteractive).async { [weak self] in
var assetCollections = [TLAssetsCollection]()
//Camera Roll
let camerarollCollection = getSmartAlbum(subType: .smartAlbumUserLibrary, useCameraButton: useCameraButton, result: &assetCollections)
let camerarollCollection = getSmartAlbum(subType: .smartAlbumUserLibrary,
useCameraButton: useCameraButton,
result: &assetCollections)
if var cameraRoll = camerarollCollection {
cameraRoll.useCameraButton = useCameraButton
assetCollections[0] = cameraRoll
Expand All @@ -237,7 +253,8 @@ extension TLPhotoLibrary {
getSmartAlbum(subType: .smartAlbumVideos, result: &assetCollections)
}
//Album
let albumsResult = PHCollectionList.fetchTopLevelUserCollections(with: nil)
let collectionOption = fetchCollectionOption[.topLevelUserCollections]
let albumsResult = PHCollectionList.fetchTopLevelUserCollections(with: collectionOption)
albumsResult.enumerateObjects({ (collection, index, stop) -> Void in
guard let collection = collection as? PHAssetCollection else { return }
var assetsCollection = TLAssetsCollection(collection: collection)
Expand Down
22 changes: 21 additions & 1 deletion TLPhotoPicker/Classes/TLPhotosPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ extension TLPhotosPickerLogDelegate {
func selectedAlbum(picker: TLPhotosPickerViewController, collections: [TLAssetsCollection], at: Int) { }
}


public struct TLPhotosPickerConfigure {
public var defaultCameraRollTitle = "Camera Roll"
public var tapHereToChange = "Tap here to change"
Expand All @@ -72,6 +71,7 @@ public struct TLPhotosPickerConfigure {
public var singleSelectedMode = false
public var maxSelectedAssets: Int? = nil
public var fetchOption: PHFetchOptions? = nil
public var fetchCollectionOption: [FetchCollectionType: PHFetchOptions] = [:]
public var selectedColor = UIColor(red: 88/255, green: 144/255, blue: 255/255, alpha: 1.0)
public var cameraBgColor = UIColor(red: 221/255, green: 223/255, blue: 226/255, alpha: 1)
public var cameraIcon = TLBundle.podBundleImage(named: "camera")
Expand All @@ -88,6 +88,26 @@ public struct TLPhotosPickerConfigure {
}
}

public enum FetchCollectionType {
case assetCollections(PHAssetCollectionType)
case topLevelUserCollections
}

extension FetchCollectionType: Hashable {
private var identifier: String {
switch self {
case let .assetCollections(collectionType):
return "assetCollections\(collectionType.rawValue)"
case .topLevelUserCollections:
return "topLevelUserCollections"
}
}

public func hash(into hasher: inout Hasher) {
hasher.combine(self.identifier)
}
}

public enum PopupConfigure {
case animation(TimeInterval)
}
Expand Down

0 comments on commit a8aae72

Please sign in to comment.