From bab5226f2aa4863056fbd20ca37a428278bfe1b5 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 22 Nov 2022 13:35:04 -0500 Subject: [PATCH] Fix albums in asset picker not being sorted by name --- .../AssetCollectionsListViewController.swift | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Tusker/Screens/Asset Picker/AssetCollectionsListViewController.swift b/Tusker/Screens/Asset Picker/AssetCollectionsListViewController.swift index 2201cb26..84239699 100644 --- a/Tusker/Screens/Asset Picker/AssetCollectionsListViewController.swift +++ b/Tusker/Screens/Asset Picker/AssetCollectionsListViewController.swift @@ -52,11 +52,13 @@ class AssetCollectionsListViewController: UITableViewController { let smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: nil) var smartAlbumItems = [Item]() smartAlbums.enumerateObjects { (collection, _, _) in - guard collection.assetCollectionSubtype != .smartAlbumAllHidden && collection.assetCollectionSubtype != .smartAlbumRecentlyAdded else { + guard collection.assetCollectionSubtype != .smartAlbumAllHidden else { return } 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) 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(sharedItems, toSection: .sharedAlbums) dataSource.apply(snapshot, animatingDifferences: false) - } // MARK: - Table view delegate @@ -125,6 +128,15 @@ extension AssetCollectionsListViewController { hasher.combine(collection.localIdentifier) } } + + var title: String { + switch self { + case .cameraRoll: + return "All Photos" + case .album(let collection): + return collection.localizedTitle ?? "" + } + } } class DataSource: UITableViewDiffableDataSource { override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {