Optimize album ordering - remove unnecessary albums

pull/1/head
Michael Kirk 6 years ago
parent a50541009a
commit ff45070219

@ -288,11 +288,24 @@ class PhotoLibrary: NSObject, PHPhotoLibraryChangeObserver {
guard photoCollection.contents().assetCount > 0 else { guard photoCollection.contents().assetCount > 0 else {
return return
} }
collections.append(photoCollection) collections.append(photoCollection)
} }
let processPHAssetCollections: (PHFetchResult<PHAssetCollection>) -> Void = { (fetchResult) in let processPHAssetCollections: (PHFetchResult<PHAssetCollection>) -> Void = { (fetchResult) in
for index in 0..<fetchResult.count { // undocumented constant
processPHCollection(fetchResult.object(at: index))
fetchResult.enumerateObjects { (assetCollection, index, stop) in
// We're already sorting albums by last-updated. "Recently Added" is mostly redundant
guard assetCollection.assetCollectionSubtype != .smartAlbumRecentlyAdded else {
return
}
let kRecentlyDeletedAlbumSubtype = PHAssetCollectionSubtype(rawValue: 1000000201)
guard assetCollection.assetCollectionSubtype != kRecentlyDeletedAlbumSubtype else {
return
}
processPHCollection(assetCollection)
} }
} }
let processPHCollections: (PHFetchResult<PHCollection>) -> Void = { (fetchResult) in let processPHCollections: (PHFetchResult<PHCollection>) -> Void = { (fetchResult) in
@ -302,13 +315,19 @@ class PhotoLibrary: NSObject, PHPhotoLibraryChangeObserver {
} }
let fetchOptions = PHFetchOptions() let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "endDate", ascending: true)] fetchOptions.sortDescriptors = [NSSortDescriptor(key: "endDate", ascending: true)]
// Try to add "Camera Roll" first. // Try to add "Camera Roll" first.
processPHAssetCollections(PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumUserLibrary, options: fetchOptions)) processPHAssetCollections(PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumUserLibrary, options: fetchOptions))
// User-created albums.
processPHCollections(PHAssetCollection.fetchTopLevelUserCollections(with: fetchOptions)) // Favorites
processPHAssetCollections(PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumFavorites, options: fetchOptions))
// Smart albums. // Smart albums.
processPHAssetCollections(PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: fetchOptions)) processPHAssetCollections(PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: fetchOptions))
// User-created albums.
processPHCollections(PHAssetCollection.fetchTopLevelUserCollections(with: fetchOptions))
return collections return collections
} }
} }

Loading…
Cancel
Save