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