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

Configuration option to disable photos within the library #107

Merged
merged 2 commits into from
May 16, 2018
Merged
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
3 changes: 3 additions & 0 deletions Source/Configuration/YPImagePickerConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public struct YPImagePickerConfiguration {
/// Enables videos within the library and video taking. Defaults to false
@available(*, unavailable, renamed:"showsVideoInLibrary")
public var showsVideo = false

/// Disables photos within the library. Defaults to true
public var showsPhotosInLibrary = false

/// Enables videos within the library. Defaults to false
public var showsVideoInLibrary = false
Expand Down
37 changes: 25 additions & 12 deletions Source/Library/YPLibraryVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,13 @@ public class YPLibraryVC: UIViewController, PermissionCheckable {
}

func refreshMediaRequest() {

// Sorting condition
let options = PHFetchOptions()
options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

if let collection = self.mediaManager.collection {
if !configuration.showsVideoInLibrary {
options.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)
}

let options = buildPHFetchOptions()

if let collection = mediaManager.collection {
mediaManager.fetchResult = PHAsset.fetchAssets(in: collection, options: options)
} else {
mediaManager.fetchResult = configuration.showsVideoInLibrary
? PHAsset.fetchAssets(with: options)
: PHAsset.fetchAssets(with: PHAssetMediaType.image, options: options)
mediaManager.fetchResult = PHAsset.fetchAssets(with: options)
}

if mediaManager.fetchResult.count > 0 {
Expand All @@ -169,6 +162,26 @@ public class YPLibraryVC: UIViewController, PermissionCheckable {
}
scrollToTop()
}

func buildPHFetchOptions() -> PHFetchOptions {
// Sorting condition
let options = PHFetchOptions()
options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

// Create predicate based on configuration showPhotosInLibrary and showsVideoInLibrary
switch (configuration.showsPhotosInLibrary, configuration.showsVideoInLibrary) {
case (true, true):
options.predicate = NSPredicate(format: "mediaType = %d || mediaType = %d",
PHAssetMediaType.image.rawValue, PHAssetMediaType.video.rawValue)
case (true, false):
options.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)
case (false, true):
options.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.video.rawValue)
case (false, false): break // Show everything 🤔 perhaps an assertionFailure?
}

return options
}

func scrollToTop() {
tappedImage()
Expand Down