Separate Shared Albums section in asset picker

Closes #244
This commit is contained in:
Shadowfacts 2022-11-21 23:21:21 -05:00
parent 47b838a386
commit 45c844b065
1 changed files with 15 additions and 6 deletions

View File

@ -46,7 +46,7 @@ class AssetCollectionsListViewController: UITableViewController {
})
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.system, .albums, .smartAlbums])
snapshot.appendSections([.system, .albums, .sharedAlbums, .smartAlbums])
snapshot.appendItems([.cameraRoll], toSection: .system)
let smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: nil)
@ -61,12 +61,18 @@ class AssetCollectionsListViewController: UITableViewController {
let albums = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil)
var albumItems = [Item]()
var sharedItems = [Item]()
albums.enumerateObjects { (collection, _, _) in
if collection.estimatedAssetCount > 0 {
albumItems.append(.album(collection))
if collection.assetCollectionSubtype == .albumCloudShared {
sharedItems.append(.album(collection))
} else {
albumItems.append(.album(collection))
}
}
}
snapshot.appendItems(albumItems, toSection: .albums)
snapshot.appendItems(sharedItems, toSection: .sharedAlbums)
dataSource.apply(snapshot, animatingDifferences: false)
@ -103,6 +109,7 @@ extension AssetCollectionsListViewController {
enum Section {
case system
case albums
case sharedAlbums
case smartAlbums
}
enum Item: Hashable {
@ -121,12 +128,14 @@ extension AssetCollectionsListViewController {
}
class DataSource: UITableViewDiffableDataSource<Section, Item> {
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let currentSnapshot = snapshot()
if currentSnapshot.indexOfSection(.albums) == section {
switch sectionIdentifier(for: section) {
case .albums:
return NSLocalizedString("Albums", comment: "albums section title")
} else if currentSnapshot.indexOfSection(.smartAlbums) == section {
case .sharedAlbums:
return NSLocalizedString("Shared Albums", comment: "shared albums section title")
case .smartAlbums:
return NSLocalizedString("Smart Albums", comment: "smart albums section title")
} else {
default:
return nil
}
}