PPAssetsActionHelper.swift 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2017-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. import UIKit
  22. import CoreLocation
  23. import Photos
  24. @objc public protocol PPAssetsActionHelperDelegate: class {
  25. /**
  26. Called when a user dismisses a picker by tapping cancel or background.
  27. Implementation is optional.
  28. - Parameter picker: Picker Controller that was dismissed.
  29. */
  30. func assetsActionHelperDidCancel(_ picker: PPAssetsActionHelper)
  31. /**
  32. Called when a user selects previews and presses send button.
  33. Assets Picker is not dismissed automatically when the delegate method is called.
  34. Implementation is optional.
  35. - Parameters:
  36. - picker: Current picker controller.
  37. - assets: Assets that were selected with Preview Picker.
  38. */
  39. func assetsActionHelper(_ picker: PPAssetsActionHelper, didFinishPicking assets: [Any])
  40. /**
  41. Called when a user click the own option
  42. Assets Picker is not dismissed automatically when the delegate method is called.
  43. Implementation is optional.
  44. - Parameter picker: Picker Controller that was dismissed.
  45. */
  46. func assetActionHelperDidSelectOwnOption(_ picker: PPAssetsActionHelper, didFinishPicking assets: [Any])
  47. /**
  48. Called when a user click the own snap button
  49. Assets Picker is not dismissed automatically when the delegate method is called.
  50. Implementation is optional.
  51. - Parameter picker: Picker Controller that was dismissed.
  52. - assets: Assets that were selected with Preview Picker.
  53. */
  54. func assetsActionHelperDidSelectOwnSnapButton(_ picker: PPAssetsActionHelper, didFinishPicking assets: [Any])
  55. /**
  56. Called when a user click the live camera cell
  57. Assets Picker is not dismissed automatically when the delegate method is called.
  58. Implementation is optional.
  59. - Parameter picker: Picker Controller.
  60. */
  61. func assetsActionHelperDidSelectLiveCameraCell(_ picker: PPAssetsActionHelper)
  62. func assetsActionHelperDidSelectLocation(_ picker: PPAssetsActionHelper)
  63. func assetsActionHelperDidSelectRecordAudio(_ picker: PPAssetsActionHelper)
  64. func assetsActionHelperDidSelectCreateBallot(_ picker: PPAssetsActionHelper)
  65. func assetsActionHelperDidSelectShareFile(_ picker: PPAssetsActionHelper)
  66. }
  67. /**
  68. Default implementation for delegate methods to make them optional.
  69. */
  70. extension PPAssetsActionHelperDelegate {
  71. func assetsActionHelperDidCancel(_ picker: PPAssetsActionHelper) {}
  72. func assetsActionHelper(_ picker: PPAssetsActionHelper, didFinishPicking assets: [Any]) {}
  73. func assetActionHelperDidSelectOwnOption(_ picker: PPAssetsActionHelper, didFinishPicking assets: [Any]) {}
  74. func assetsActionHelperDidSelectOwnSnapButton(_ picker: PPAssetsActionHelper, didFinishPicking assets: [Any]) {}
  75. func assetsActionHelperDidSelectLiveCameraCell(_ picker: PPAssetsActionHelper) {}
  76. func assetsActionHelperDiassetsPickerDidSelectLiveCameraCelldSelectLocation(_ picker: PPAssetsActionHelper) {}
  77. func assetsActionHelperDidSelectRecordAudio(_ picker: PPAssetsActionHelper) {}
  78. func assetsActionHelperDidSelectCreateBallot(_ picker: PPAssetsActionHelper) {}
  79. func assetsActionHelperDidSelectShareFile(_ picker: PPAssetsActionHelper) {}
  80. }
  81. public class PPAssetsActionHelper: NSObject {
  82. @objc open weak var delegate: PPAssetsActionHelperDelegate?
  83. required public override init() {
  84. super.init()
  85. }
  86. enum Heights: CGFloat {
  87. case Inches_3_5 = 480
  88. case Inches_4 = 568
  89. case Inches_4_7 = 667
  90. case Inches_5_5 = 736
  91. }
  92. internal func isPhone() -> Bool {
  93. return UIDevice.current.userInterfaceIdiom == .phone
  94. }
  95. internal func isSizeOrLarger(height: Heights) -> Bool {
  96. return UIScreen.main.bounds.size.height >= height.rawValue
  97. }
  98. internal func IS_4_7_INCHES_OR_LARGER() -> Bool {
  99. return isPhone() && isSizeOrLarger(height: .Inches_4_7)
  100. }
  101. @objc public func buildAction() -> PPAssetsActionController {
  102. var options = Array<PPOption>()
  103. if CLLocationManager.locationServicesEnabled() {
  104. let shareLocation = PPOption(withTitle: NSLocalizedString("share_location", comment: ""), withIcon: UIImage(named: "ActionLocation", in: Colors.main())) {
  105. self.delegate?.assetsActionHelperDidSelectLocation(self)
  106. }
  107. options.append(shareLocation)
  108. }
  109. if PlayRecordAudioViewController.canRecordAudio() {
  110. let recordAudio = PPOption(withTitle: NSLocalizedString("record_audio", comment: ""), withIcon: UIImage(named: "ActionMicrophone", in: Colors.main())) {
  111. self.delegate?.assetsActionHelperDidSelectRecordAudio(self)
  112. }
  113. options.append(recordAudio)
  114. }
  115. let ballotCreate = PPOption(withTitle: NSLocalizedString("ballot_create", tableName: "Ballot", bundle: Bundle.main, value: "", comment: ""), withIcon: UIImage(named: "ActionBallot", in: Colors.main())) {
  116. self.delegate?.assetsActionHelperDidSelectCreateBallot(self)
  117. }
  118. options.append(ballotCreate)
  119. let shareFile = PPOption(withTitle: NSLocalizedString("share_file", comment: ""), withIcon: UIImage(named: "ActionFile", in: Colors.main())) {
  120. self.delegate?.assetsActionHelperDidSelectShareFile(self)
  121. }
  122. options.append(shareFile)
  123. var config = PPAssetsActionConfig()
  124. config.textAlignment = .left
  125. config.showOptionsWhenAssetIsSelected = false
  126. config.showLiveCameraCell = true
  127. config.showVideos = true
  128. config.showGalleryPreview = UserSettings.shared().showGalleryPreview
  129. config.assetsPreviewExpandedHeight = 260.0
  130. config.inset = 10.0
  131. config.cornerRadius = 10.0
  132. config.sectionSpacing = 8.0
  133. config.fetchLimit = Int(UserSettings.shared().previewLimit)
  134. config.buttonHeight = IS_4_7_INCHES_OR_LARGER() ? 50.0 : 42.0
  135. config.backgroundColor = UIColor(red:0.0 , green: 0.0, blue: 0.0, alpha: 0.5)
  136. config.maxSelectableAssets = 10
  137. config.useOwnSnapButton = true
  138. config.ownSnapButtonText = NSLocalizedString("choose_existing", comment: "")
  139. config.ownSnapButtonIcon = UIImage(named: "ActionPhoto", in: Colors.main())
  140. config.previewReplacementText = NSLocalizedString("take_photo_or_video", comment: "")
  141. config.previewReplacementIcon = UIImage(named: "ActionCamera", in: Colors.main())
  142. config.tintColor = Colors.main()
  143. config.tableBackground = Colors.background()
  144. let assetsPicker = PPAssetsActionController(with: options, aConfig: config)
  145. assetsPicker.delegate = self
  146. return assetsPicker
  147. }
  148. }
  149. extension PPAssetsActionHelper: PPAssetsActionControllerDelegate {
  150. public func assetsPickerDidCancel(_ picker: PPAssetsActionController) {
  151. delegate?.assetsActionHelperDidCancel(self)
  152. print("assetsPickerDidCancel")
  153. }
  154. public func assetsPicker(_ picker: PPAssetsActionController, didFinishPicking media: [MediaProvider]) {
  155. var mediaArray = [Any]()
  156. for m in media {
  157. if m.image() != nil {
  158. mediaArray.append(m.image()!)
  159. }
  160. if m.video() != nil {
  161. mediaArray.append(m.video()!)
  162. }
  163. if m.phasset() != nil {
  164. mediaArray.append(m.phasset()!)
  165. }
  166. }
  167. delegate?.assetsActionHelper(self, didFinishPicking: mediaArray)
  168. print("assetsPicker didFinishPicking with \(media)")
  169. }
  170. public func assetsPicker(_ picker: PPAssetsActionController, didSnapImage image: UIImage) {
  171. }
  172. public func assetsPicker(_ picker: PPAssetsActionController, didSnapVideo videoURL: URL) {
  173. }
  174. public func assetsPickerDidSelectOwnOption(_ picker: PPAssetsActionController, didFinishPicking media: [MediaProvider]) {
  175. var mediaArray = [Any]()
  176. for m in media {
  177. if m.image() != nil {
  178. mediaArray.append(m.image()!)
  179. }
  180. if m.video() != nil {
  181. mediaArray.append(m.video()!)
  182. }
  183. if m.phasset() != nil {
  184. mediaArray.append(m.phasset()!)
  185. }
  186. }
  187. delegate?.assetActionHelperDidSelectOwnOption(self, didFinishPicking: mediaArray)
  188. print("assetsPicker didSelectOwnOption")
  189. }
  190. public func assetsPickerDidSelectOwnSnapButton(_ picker: PPAssetsActionController, didFinishPicking media: [MediaProvider]) {
  191. var mediaArray = [Any]()
  192. for m in media {
  193. if m.image() != nil {
  194. mediaArray.append(m.image()!)
  195. }
  196. if m.video() != nil {
  197. mediaArray.append(m.video()!)
  198. }
  199. if m.phasset() != nil {
  200. mediaArray.append(m.phasset()!)
  201. }
  202. }
  203. delegate?.assetsActionHelperDidSelectOwnSnapButton(self, didFinishPicking: mediaArray)
  204. print("assetsPicker didSelectOwnSnapButton")
  205. }
  206. public func assetsPickerDidSelectLiveCameraCell(_ picker: PPAssetsActionController) {
  207. delegate?.assetsActionHelperDidSelectLiveCameraCell(self)
  208. }
  209. }