From a8aae72aaaf01476b30fab91aedaf5d0cb6fc26f Mon Sep 17 00:00:00 2001 From: tilltue Date: Thu, 22 Aug 2019 00:24:18 +0900 Subject: [PATCH] added fetchCollectionOption --- README.md | 13 ++++++++ TLPhotoPicker.podspec | 2 +- TLPhotoPicker/Classes/TLPhotoLibrary.swift | 31 ++++++++++++++----- .../TLPhotosPickerViewController.swift | 22 ++++++++++++- 4 files changed, 59 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 05079472..b38edf2e 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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) diff --git a/TLPhotoPicker.podspec b/TLPhotoPicker.podspec index da8dbb8b..74e696d0 100644 --- a/TLPhotoPicker.podspec +++ b/TLPhotoPicker.podspec @@ -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. diff --git a/TLPhotoPicker/Classes/TLPhotoLibrary.swift b/TLPhotoPicker/Classes/TLPhotoLibrary.swift index 3911cf5b..6d859063 100644 --- a/TLPhotoPicker/Classes/TLPhotoLibrary.swift +++ b/TLPhotoPicker/Classes/TLPhotoLibrary.swift @@ -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 { @@ -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 { @@ -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 { @@ -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 @@ -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) diff --git a/TLPhotoPicker/Classes/TLPhotosPickerViewController.swift b/TLPhotoPicker/Classes/TLPhotosPickerViewController.swift index 0eda51b1..ff223c1a 100644 --- a/TLPhotoPicker/Classes/TLPhotosPickerViewController.swift +++ b/TLPhotoPicker/Classes/TLPhotosPickerViewController.swift @@ -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" @@ -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") @@ -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) }