96 lines
3.4 KiB
Swift
96 lines
3.4 KiB
Swift
//
|
|
// AssetPickerViewController.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 1/1/20.
|
|
// Copyright © 2020 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Photos
|
|
|
|
protocol AssetPickerViewControllerDelegate: AnyObject {
|
|
func assetPicker(_ assetPicker: AssetPickerViewController, shouldAllowAssetOfType type: CompositionAttachmentData.AttachmentType) -> Bool
|
|
func assetPicker(_ assetPicker: AssetPickerViewController, didSelectAttachments attachments: [CompositionAttachmentData])
|
|
}
|
|
|
|
class AssetPickerViewController: UINavigationController {
|
|
|
|
weak var assetPickerDelegate: AssetPickerViewControllerDelegate?
|
|
|
|
var currentCollectionSelectedAssets: [CompositionAttachmentData] {
|
|
if let vc = visibleViewController as? AssetCollectionViewController {
|
|
return vc.selectedAssets.map { .asset($0) }
|
|
} else {
|
|
return []
|
|
}
|
|
}
|
|
|
|
init() {
|
|
super.init(navigationBarClass: nil, toolbarClass: nil)
|
|
}
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
let assetCollectionsList = AssetCollectionsListViewController()
|
|
assetCollectionsList.assetCollectionDelegate = self
|
|
let assetCollection = AssetCollectionViewController()
|
|
assetCollection.delegate = self
|
|
setViewControllers([assetCollectionsList, assetCollection], animated: false)
|
|
}
|
|
|
|
func presentImagePicker(animated: Bool) {
|
|
let imagePicker = UIImagePickerController()
|
|
imagePicker.delegate = self
|
|
imagePicker.sourceType = .camera
|
|
imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .camera)!
|
|
self.present(imagePicker, animated: true)
|
|
}
|
|
|
|
}
|
|
|
|
extension AssetPickerViewController: AssetCollectionViewControllerDelegate {
|
|
func shouldSelectAsset(_ asset: PHAsset) -> Bool {
|
|
guard let delegate = assetPickerDelegate else { return true }
|
|
guard let type = asset.attachmentType else { return false }
|
|
return delegate.assetPicker(self, shouldAllowAssetOfType: type)
|
|
}
|
|
func didSelectAssets(_ assets: [PHAsset]) {
|
|
assetPickerDelegate?.assetPicker(self, didSelectAttachments: assets.map { .asset($0) })
|
|
}
|
|
func captureFromCamera() {
|
|
presentImagePicker(animated: true)
|
|
}
|
|
}
|
|
|
|
extension AssetPickerViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
|
|
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
|
|
let attachment: CompositionAttachmentData
|
|
if let image = info[.originalImage] as? UIImage {
|
|
attachment = .image(image)
|
|
} else if let url = info[.mediaURL] as? URL {
|
|
attachment = .video(url)
|
|
} else {
|
|
return
|
|
}
|
|
|
|
if assetPickerDelegate?.assetPicker(self, shouldAllowAssetOfType: attachment.type) ?? true {
|
|
assetPickerDelegate?.assetPicker(self, didSelectAttachments: [attachment])
|
|
// dismiss image picker
|
|
dismiss(animated: true) {
|
|
// dismiss asset picker
|
|
self.dismiss(animated: true)
|
|
}
|
|
} else {
|
|
dismiss(animated: false) {
|
|
self.presentImagePicker(animated: false)
|
|
}
|
|
}
|
|
}
|
|
}
|