123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- // _____ _
- // |_ _| |_ _ _ ___ ___ _ __ __ _
- // | | | ' \| '_/ -_) -_) ' \/ _` |_
- // |_| |_||_|_| \___\___|_|_|_\__,_(_)
- //
- // Threema iOS Client
- // Copyright (c) 2017-2020 Threema GmbH
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Affero General Public License, version 3,
- // as published by the Free Software Foundation.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with this program. If not, see <https://www.gnu.org/licenses/>.
- import Foundation
- import UIKit
- import CoreLocation
- import Photos
- @objc public protocol PPAssetsActionHelperDelegate: class {
- /**
- Called when a user dismisses a picker by tapping cancel or background.
- Implementation is optional.
- - Parameter picker: Picker Controller that was dismissed.
- */
- func assetsActionHelperDidCancel(_ picker: PPAssetsActionHelper)
-
- /**
- Called when a user selects previews and presses send button.
- Assets Picker is not dismissed automatically when the delegate method is called.
- Implementation is optional.
- - Parameters:
- - picker: Current picker controller.
- - assets: Assets that were selected with Preview Picker.
- */
- func assetsActionHelper(_ picker: PPAssetsActionHelper, didFinishPicking assets: [Any])
-
- /**
- Called when a user click the own option
- Assets Picker is not dismissed automatically when the delegate method is called.
- Implementation is optional.
- - Parameter picker: Picker Controller that was dismissed.
- */
- func assetActionHelperDidSelectOwnOption(_ picker: PPAssetsActionHelper, didFinishPicking assets: [Any])
-
- /**
- Called when a user click the own snap button
- Assets Picker is not dismissed automatically when the delegate method is called.
- Implementation is optional.
- - Parameter picker: Picker Controller that was dismissed.
- - assets: Assets that were selected with Preview Picker.
- */
- func assetsActionHelperDidSelectOwnSnapButton(_ picker: PPAssetsActionHelper, didFinishPicking assets: [Any])
-
- /**
- Called when a user click the live camera cell
- Assets Picker is not dismissed automatically when the delegate method is called.
- Implementation is optional.
- - Parameter picker: Picker Controller.
- */
- func assetsActionHelperDidSelectLiveCameraCell(_ picker: PPAssetsActionHelper)
-
- func assetsActionHelperDidSelectLocation(_ picker: PPAssetsActionHelper)
- func assetsActionHelperDidSelectRecordAudio(_ picker: PPAssetsActionHelper)
- func assetsActionHelperDidSelectCreateBallot(_ picker: PPAssetsActionHelper)
- func assetsActionHelperDidSelectShareFile(_ picker: PPAssetsActionHelper)
- }
- /**
- Default implementation for delegate methods to make them optional.
- */
- extension PPAssetsActionHelperDelegate {
- func assetsActionHelperDidCancel(_ picker: PPAssetsActionHelper) {}
- func assetsActionHelper(_ picker: PPAssetsActionHelper, didFinishPicking assets: [Any]) {}
- func assetActionHelperDidSelectOwnOption(_ picker: PPAssetsActionHelper, didFinishPicking assets: [Any]) {}
- func assetsActionHelperDidSelectOwnSnapButton(_ picker: PPAssetsActionHelper, didFinishPicking assets: [Any]) {}
- func assetsActionHelperDidSelectLiveCameraCell(_ picker: PPAssetsActionHelper) {}
- func assetsActionHelperDiassetsPickerDidSelectLiveCameraCelldSelectLocation(_ picker: PPAssetsActionHelper) {}
- func assetsActionHelperDidSelectRecordAudio(_ picker: PPAssetsActionHelper) {}
- func assetsActionHelperDidSelectCreateBallot(_ picker: PPAssetsActionHelper) {}
- func assetsActionHelperDidSelectShareFile(_ picker: PPAssetsActionHelper) {}
- }
- public class PPAssetsActionHelper: NSObject {
-
- @objc open weak var delegate: PPAssetsActionHelperDelegate?
-
- required public override init() {
- super.init()
- }
-
- enum Heights: CGFloat {
- case Inches_3_5 = 480
- case Inches_4 = 568
- case Inches_4_7 = 667
- case Inches_5_5 = 736
- }
-
- internal func isPhone() -> Bool {
- return UIDevice.current.userInterfaceIdiom == .phone
- }
-
- internal func isSizeOrLarger(height: Heights) -> Bool {
- return UIScreen.main.bounds.size.height >= height.rawValue
- }
-
- internal func IS_4_7_INCHES_OR_LARGER() -> Bool {
- return isPhone() && isSizeOrLarger(height: .Inches_4_7)
- }
-
- @objc public func buildAction() -> PPAssetsActionController {
- var options = Array<PPOption>()
-
- if CLLocationManager.locationServicesEnabled() {
- let shareLocation = PPOption(withTitle: NSLocalizedString("share_location", comment: ""), withIcon: UIImage(named: "ActionLocation", in: Colors.main())) {
- self.delegate?.assetsActionHelperDidSelectLocation(self)
- }
- options.append(shareLocation)
- }
-
- if PlayRecordAudioViewController.canRecordAudio() {
- let recordAudio = PPOption(withTitle: NSLocalizedString("record_audio", comment: ""), withIcon: UIImage(named: "ActionMicrophone", in: Colors.main())) {
- self.delegate?.assetsActionHelperDidSelectRecordAudio(self)
- }
- options.append(recordAudio)
- }
-
- let ballotCreate = PPOption(withTitle: NSLocalizedString("ballot_create", tableName: "Ballot", bundle: Bundle.main, value: "", comment: ""), withIcon: UIImage(named: "ActionBallot", in: Colors.main())) {
- self.delegate?.assetsActionHelperDidSelectCreateBallot(self)
- }
- options.append(ballotCreate)
- let shareFile = PPOption(withTitle: NSLocalizedString("share_file", comment: ""), withIcon: UIImage(named: "ActionFile", in: Colors.main())) {
- self.delegate?.assetsActionHelperDidSelectShareFile(self)
- }
- options.append(shareFile)
- var config = PPAssetsActionConfig()
- config.textAlignment = .left
- config.showOptionsWhenAssetIsSelected = false
-
- config.showLiveCameraCell = true
- config.showVideos = true
-
- config.showGalleryPreview = UserSettings.shared().showGalleryPreview
-
- config.assetsPreviewExpandedHeight = 260.0
- config.inset = 10.0
- config.cornerRadius = 10.0
- config.sectionSpacing = 8.0
-
- config.fetchLimit = Int(UserSettings.shared().previewLimit)
-
- config.buttonHeight = IS_4_7_INCHES_OR_LARGER() ? 50.0 : 42.0
-
- config.backgroundColor = UIColor(red:0.0 , green: 0.0, blue: 0.0, alpha: 0.5)
- config.maxSelectableAssets = 10
-
- config.useOwnSnapButton = true
- config.ownSnapButtonText = NSLocalizedString("choose_existing", comment: "")
- config.ownSnapButtonIcon = UIImage(named: "ActionPhoto", in: Colors.main())
- config.previewReplacementText = NSLocalizedString("take_photo_or_video", comment: "")
- config.previewReplacementIcon = UIImage(named: "ActionCamera", in: Colors.main())
-
- config.tintColor = Colors.main()
- config.tableBackground = Colors.background()
-
- let assetsPicker = PPAssetsActionController(with: options, aConfig: config)
- assetsPicker.delegate = self
- return assetsPicker
- }
- }
- extension PPAssetsActionHelper: PPAssetsActionControllerDelegate {
- public func assetsPickerDidCancel(_ picker: PPAssetsActionController) {
- delegate?.assetsActionHelperDidCancel(self)
- print("assetsPickerDidCancel")
- }
-
- public func assetsPicker(_ picker: PPAssetsActionController, didFinishPicking media: [MediaProvider]) {
- var mediaArray = [Any]()
-
- for m in media {
- if m.image() != nil {
- mediaArray.append(m.image()!)
- }
- if m.video() != nil {
- mediaArray.append(m.video()!)
- }
- if m.phasset() != nil {
- mediaArray.append(m.phasset()!)
- }
- }
-
- delegate?.assetsActionHelper(self, didFinishPicking: mediaArray)
- print("assetsPicker didFinishPicking with \(media)")
- }
-
- public func assetsPicker(_ picker: PPAssetsActionController, didSnapImage image: UIImage) {
- }
-
- public func assetsPicker(_ picker: PPAssetsActionController, didSnapVideo videoURL: URL) {
- }
-
- public func assetsPickerDidSelectOwnOption(_ picker: PPAssetsActionController, didFinishPicking media: [MediaProvider]) {
- var mediaArray = [Any]()
-
- for m in media {
- if m.image() != nil {
- mediaArray.append(m.image()!)
- }
- if m.video() != nil {
- mediaArray.append(m.video()!)
- }
- if m.phasset() != nil {
- mediaArray.append(m.phasset()!)
- }
- }
-
- delegate?.assetActionHelperDidSelectOwnOption(self, didFinishPicking: mediaArray)
- print("assetsPicker didSelectOwnOption")
- }
-
- public func assetsPickerDidSelectOwnSnapButton(_ picker: PPAssetsActionController, didFinishPicking media: [MediaProvider]) {
- var mediaArray = [Any]()
-
- for m in media {
- if m.image() != nil {
- mediaArray.append(m.image()!)
- }
- if m.video() != nil {
- mediaArray.append(m.video()!)
- }
- if m.phasset() != nil {
- mediaArray.append(m.phasset()!)
- }
- }
-
- delegate?.assetsActionHelperDidSelectOwnSnapButton(self, didFinishPicking: mediaArray)
- print("assetsPicker didSelectOwnSnapButton")
- }
-
- public func assetsPickerDidSelectLiveCameraCell(_ picker: PPAssetsActionController) {
- delegate?.assetsActionHelperDidSelectLiveCameraCell(self)
- }
- }
|