session-ios/Session/Media Viewing & Editing/PhotoCollectionPickerContro...

139 lines
4.5 KiB
Swift
Raw Normal View History

//
2019-03-08 06:05:58 +01:00
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
import Foundation
import Photos
import PromiseKit
protocol PhotoCollectionPickerDelegate: AnyObject {
func photoCollectionPicker(_ photoCollectionPicker: PhotoCollectionPickerController, didPickCollection collection: PhotoCollection)
}
class PhotoCollectionPickerController: OWSTableViewController, PhotoLibraryDelegate {
private weak var collectionDelegate: PhotoCollectionPickerDelegate?
private let library: PhotoLibrary
2018-11-15 19:00:14 +01:00
private var photoCollections: [PhotoCollection]
required init(library: PhotoLibrary,
collectionDelegate: PhotoCollectionPickerDelegate) {
self.library = library
self.photoCollections = library.allPhotoCollections()
self.collectionDelegate = collectionDelegate
super.init()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
2020-01-20 04:44:51 +01:00
view.backgroundColor = .white
tableView.backgroundColor = .white
2018-11-28 01:48:14 +01:00
tableView.separatorColor = .clear
library.add(delegate: self)
updateContents()
}
2018-11-28 00:05:03 +01:00
// MARK: -
private func updateContents() {
photoCollections = library.allPhotoCollections()
2018-11-28 00:05:03 +01:00
let sectionItems = photoCollections.map { collection in
return OWSTableItem(customCellBlock: { self.buildTableCell(collection: collection) },
2019-03-30 14:22:31 +01:00
customRowHeight: UITableView.automaticDimension,
2018-11-28 00:05:03 +01:00
actionBlock: { [weak self] in
guard let strongSelf = self else { return }
strongSelf.didSelectCollection(collection: collection)
})
}
2018-11-28 00:05:03 +01:00
let section = OWSTableSection(title: nil, items: sectionItems)
let contents = OWSTableContents()
contents.addSection(section)
self.contents = contents
}
2018-11-28 01:48:14 +01:00
private let numberFormatter: NumberFormatter = NumberFormatter()
private func buildTableCell(collection: PhotoCollection) -> UITableViewCell {
2018-11-28 00:05:03 +01:00
let cell = OWSTableItem.newCell()
2020-01-20 04:44:51 +01:00
cell.backgroundColor = .white
cell.contentView.backgroundColor = .white
2018-11-28 00:05:03 +01:00
cell.selectedBackgroundView?.backgroundColor = UIColor(white: 0.2, alpha: 1)
2018-11-28 01:48:14 +01:00
let contents = collection.contents()
let titleLabel = UILabel()
titleLabel.text = collection.localizedTitle()
2020-01-20 04:44:51 +01:00
titleLabel.font = .systemFont(ofSize: Values.mediumFontSize)
titleLabel.textColor = .black
2018-11-28 01:48:14 +01:00
let countLabel = UILabel()
countLabel.text = numberFormatter.string(for: contents.assetCount)
2020-01-20 04:44:51 +01:00
countLabel.font = .systemFont(ofSize: Values.smallFontSize)
countLabel.textColor = .black
2018-11-28 01:48:14 +01:00
let textStack = UIStackView(arrangedSubviews: [titleLabel, countLabel])
textStack.axis = .vertical
textStack.alignment = .leading
textStack.spacing = 2
2018-11-28 00:05:03 +01:00
let imageView = UIImageView()
2018-12-10 19:57:14 +01:00
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
2018-11-28 01:48:14 +01:00
let kImageSize = 80
2018-11-28 00:05:03 +01:00
imageView.autoSetDimensions(to: CGSize(width: kImageSize, height: kImageSize))
2018-11-28 01:48:14 +01:00
let hStackView = UIStackView(arrangedSubviews: [imageView, textStack])
hStackView.axis = .horizontal
hStackView.alignment = .center
2020-01-20 04:44:51 +01:00
hStackView.spacing = Values.mediumSpacing
let photoMediaSize = PhotoMediaSize(thumbnailSize: CGSize(width: kImageSize, height: kImageSize))
if let assetItem = contents.lastAssetItem(photoMediaSize: photoMediaSize) {
assetItem.asyncThumbnail { [weak imageView] image in
2018-11-28 01:48:14 +01:00
AssertIsOnMainThread()
guard let imageView = imageView else {
2018-11-28 00:05:03 +01:00
return
}
2018-11-28 01:48:14 +01:00
2018-11-28 00:05:03 +01:00
guard let image = image else {
owsFailDebug("image was unexpectedly nil")
2018-11-28 00:05:03 +01:00
return
}
2018-11-28 01:48:14 +01:00
imageView.image = image
2018-11-28 00:05:03 +01:00
}
}
2018-11-28 01:48:14 +01:00
cell.contentView.addSubview(hStackView)
hStackView.ows_autoPinToSuperviewMargins()
2018-11-28 00:05:03 +01:00
return cell
}
// MARK: Actions
func didSelectCollection(collection: PhotoCollection) {
collectionDelegate?.photoCollectionPicker(self, didPickCollection: collection)
}
// MARK: PhotoLibraryDelegate
func photoLibraryDidChange(_ photoLibrary: PhotoLibrary) {
updateContents()
}
}