Fix albums in asset picker not being sorted by name

This commit is contained in:
Shadowfacts 2022-11-22 13:35:04 -05:00
parent 88cfbfb1f3
commit bab5226f2a
1 changed files with 14 additions and 2 deletions

View File

@ -52,11 +52,13 @@ class AssetCollectionsListViewController: UITableViewController {
let smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: nil) let smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: nil)
var smartAlbumItems = [Item]() var smartAlbumItems = [Item]()
smartAlbums.enumerateObjects { (collection, _, _) in smartAlbums.enumerateObjects { (collection, _, _) in
guard collection.assetCollectionSubtype != .smartAlbumAllHidden && collection.assetCollectionSubtype != .smartAlbumRecentlyAdded else { guard collection.assetCollectionSubtype != .smartAlbumAllHidden else {
return return
} }
smartAlbumItems.append(.album(collection)) smartAlbumItems.append(.album(collection))
} }
// sort these manually, using PHFetchOptions.sortDescriptors seems like it just doesn't work with fetchAssetCollections
smartAlbumItems.sort(by: { $0.title < $1.title })
snapshot.appendItems(smartAlbumItems, toSection: .smartAlbums) snapshot.appendItems(smartAlbumItems, toSection: .smartAlbums)
let albums = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil) let albums = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil)
@ -71,11 +73,12 @@ class AssetCollectionsListViewController: UITableViewController {
} }
} }
} }
albumItems.sort(by: { $0.title < $1.title })
sharedItems.sort(by: { $0.title < $1.title })
snapshot.appendItems(albumItems, toSection: .albums) snapshot.appendItems(albumItems, toSection: .albums)
snapshot.appendItems(sharedItems, toSection: .sharedAlbums) snapshot.appendItems(sharedItems, toSection: .sharedAlbums)
dataSource.apply(snapshot, animatingDifferences: false) dataSource.apply(snapshot, animatingDifferences: false)
} }
// MARK: - Table view delegate // MARK: - Table view delegate
@ -125,6 +128,15 @@ extension AssetCollectionsListViewController {
hasher.combine(collection.localIdentifier) hasher.combine(collection.localIdentifier)
} }
} }
var title: String {
switch self {
case .cameraRoll:
return "All Photos"
case .album(let collection):
return collection.localizedTitle ?? ""
}
}
} }
class DataSource: UITableViewDiffableDataSource<Section, Item> { class DataSource: UITableViewDiffableDataSource<Section, Item> {
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {