session-ios/Signal/src/ViewControllers/Photos/PhotoCollectionPickerContro...

142 lines
4.7 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: class {
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 let previousPhotoCollection: PhotoCollection
private var photoCollections: [PhotoCollection]
required init(library: PhotoLibrary,
2018-11-15 19:00:14 +01:00
previousPhotoCollection: PhotoCollection,
collectionDelegate: PhotoCollectionPickerDelegate) {
self.library = library
2018-11-15 19:00:14 +01:00
self.previousPhotoCollection = previousPhotoCollection
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()
view.backgroundColor = Theme.darkThemeBackgroundColor
tableView.backgroundColor = Theme.darkThemeBackgroundColor
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) },
customRowHeight: UITableViewAutomaticDimension,
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()
cell.backgroundColor = Theme.darkThemeBackgroundColor
2018-11-28 01:48:14 +01:00
cell.contentView.backgroundColor = Theme.darkThemeBackgroundColor
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()
titleLabel.font = UIFont.ows_dynamicTypeBody
titleLabel.textColor = Theme.darkThemePrimaryColor
let countLabel = UILabel()
countLabel.text = numberFormatter.string(for: contents.assetCount)
countLabel.font = UIFont.ows_dynamicTypeCaption1
countLabel.textColor = Theme.darkThemePrimaryColor
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
hStackView.spacing = 11
let photoMediaSize = PhotoMediaSize(thumbnailSize: CGSize(width: kImageSize, height: kImageSize))
if let assetItem = contents.lastAssetItem(photoMediaSize: photoMediaSize) {
2018-11-28 00:05:03 +01:00
imageView.image = 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()
}
}