// // AssetCollectionViewController.swift // PhotoRank // // Created by Shadowfacts on 8/3/19. // Copyright © 2019 Shadowfacts. All rights reserved. // import UIKit import Photos private let reuseIdentifier = "AssetCell" class AssetPickerViewController: UICollectionViewController { var continueButton: UIBarButtonItem! var flowLayout: UICollectionViewFlowLayout { return collectionViewLayout as! UICollectionViewFlowLayout } var availableWidth: CGFloat! var thumbnailSize: CGSize! var fetchResult: PHFetchResult! init() { super.init(collectionViewLayout: UICollectionViewFlowLayout()) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() collectionView.alwaysBounceVertical = true // Register cell classes self.collectionView!.register(UINib(nibName: "AssetCollectionViewCell", bundle: .main), forCellWithReuseIdentifier: reuseIdentifier) let options = PHFetchOptions() options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] // get all photos except live photos options.predicate = NSPredicate(format: "mediaType == %d && !((mediaSubtypes & %d) == %d)", PHAssetMediaType.image.rawValue, PHAssetMediaSubtype.photoLive.rawValue, PHAssetMediaSubtype.photoLive.rawValue) fetchResult = PHAsset.fetchAssets(with: options) collectionView.allowsMultipleSelection = true setEditing(true, animated: false) continueButton = UIBarButtonItem(title: "Continue", style: .done, target: self, action: #selector(continueButtonPressed)) updateItemsSelected() navigationItem.rightBarButtonItem = continueButton } override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() let availableWidth = view.bounds.inset(by: view.safeAreaInsets).width if self.availableWidth != availableWidth { self.availableWidth = availableWidth let size = (availableWidth - 8) / 3 flowLayout.itemSize = CGSize(width: size, height: size) flowLayout.minimumInteritemSpacing = 4 flowLayout.minimumLineSpacing = 4 } } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) let scale = UIScreen.main.scale let cellSize = flowLayout.itemSize thumbnailSize = CGSize(width: cellSize.width * scale, height: cellSize.height * scale) } func updateItemsSelected() { let selected = collectionView.indexPathsForSelectedItems?.count ?? 0 continueButton.isEnabled = selected >= 2 navigationItem.title = "\(selected) selected" } @objc func continueButtonPressed() { let indexPaths = collectionView.indexPathsForSelectedItems! let assets = fetchResult.objects(at: IndexSet(indexPaths.map { $0.item })) let comparison = AssetComparisonViewController(assets: assets) show(comparison, sender: nil) } // MARK: UICollectionViewDataSource override func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return fetchResult.count } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! AssetCollectionViewCell let asset = fetchResult.object(at: indexPath.row) cell.assetIdentifier = asset.localIdentifier PhotosHelper.requestThumbnail(for: asset, targetSize: thumbnailSize) { (image) in DispatchQueue.main.async { if cell.assetIdentifier == asset.localIdentifier { cell.thumbnailImage = image } } } return cell } // MARK: UICollectionViewDelegate override func collectionView(_ collectionView: UICollectionView, shouldBeginMultipleSelectionInteractionAt indexPath: IndexPath) -> Bool { return true } override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { updateItemsSelected() } override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) { updateItemsSelected() } /* // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool { return false } override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool { return false } override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) { } */ }