ThreemaImagePickerControllerDefaultUIDelegate.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 ThreemaFramework
  22. @objc open class ThreemaImagePickerControllerDefaultUIDelegate: DKImagePickerControllerDefaultUIDelegate, MWPhotoBrowserDelegate {
  23. var photosArray = [MWPhoto]()
  24. var photoAssets = [DKAsset]()
  25. open var controller: DKImagePickerController?
  26. override open func imagePickerControllerCollectionViewBackgroundColor() -> UIColor {
  27. return Colors.background()
  28. }
  29. override open func imagePickerControllerGlobalTitleColor() -> UIColor? {
  30. return Colors.main()
  31. }
  32. override open func createDoneButtonIfNeeded() -> UIButton {
  33. if self.doneButton == nil {
  34. self.doneButton = super.createDoneButtonIfNeeded()
  35. self.doneButton!.removeTarget(self.imagePickerController, action: #selector(DKImagePickerController.done), for: UIControl.Event.touchUpInside)
  36. self.doneButton!.addTarget(self.imagePickerController, action: #selector(DKImagePickerController.doneWithoutDismiss), for: UIControl.Event.touchUpInside)
  37. }
  38. return self.doneButton!
  39. }
  40. override open func updateDoneButtonTitle(_ button: UIButton) {
  41. if self.imagePickerController.selectedAssets.count > 0 {
  42. button.setTitle(String(format: DKImageLocalizedStringWithKey("select"), self.imagePickerController.selectedAssets.count), for: .normal)
  43. button.isHidden = false
  44. } else {
  45. button.isHidden = true
  46. }
  47. button.sizeToFit()
  48. }
  49. @objc func updateButton() {
  50. updateDoneButtonTitle(self.createDoneButtonIfNeeded())
  51. }
  52. open override func imagePickerControllerCollectionImageCell() -> DKAssetGroupDetailBaseCell.Type {
  53. return CustomGroupDetailImageCell.self
  54. }
  55. open override func imagePickerControllerCollectionVideoCell() -> DKAssetGroupDetailBaseCell.Type {
  56. return CustomGroupDetailVideoCell.self
  57. }
  58. open override func imagePickerControllerLongPress(_ imagePickerController: DKImagePickerController, _ selectedRow: Int, _ assets: [DKAsset]) {
  59. self.controller = imagePickerController
  60. let photoBrowser = MWPhotoBrowser.init(delegate: self)
  61. photoBrowser?.displayActionButton = false
  62. photoBrowser?.displayDeleteButton = false
  63. photoBrowser?.enableGrid = false
  64. photoBrowser?.enableSwipeToDismiss = true
  65. photoBrowser?.displaySelectionButtons = true
  66. photoBrowser?.alwaysShowControls = true
  67. photoBrowser?.zoomPhotosToFill = false
  68. photoBrowser?.customImageSelectedIcon = StyleKit.check
  69. photosArray.removeAll()
  70. photoAssets = assets
  71. for asset:DKAsset in assets {
  72. let width = UIScreen.main.bounds.size.width * UIScreen.main.scale
  73. let height = UIScreen.main.bounds.size.height * UIScreen.main.scale
  74. let photo = MWPhoto(asset: asset.originalAsset, targetSize: CGSize(width: width, height: height))
  75. self.photosArray.append(photo!)
  76. }
  77. photoBrowser?.setCurrentPhotoIndex(UInt(selectedRow))
  78. imagePickerController.pushViewController(photoBrowser!, animated: true)
  79. }
  80. // MARK: MWPhotoBrowserDelegate
  81. public func numberOfPhotos(in photoBrowser: MWPhotoBrowser!) -> UInt {
  82. return UInt(self.photosArray.count)
  83. }
  84. open func photoBrowser(_ photoBrowser: MWPhotoBrowser!, photoAt index: UInt) -> MWPhotoProtocol! {
  85. return self.photosArray[Int(index)]
  86. }
  87. public func photoBrowser(_ photoBrowser: MWPhotoBrowser!, isPhotoSelectedAt index: UInt) -> Bool {
  88. if (controller?.selectedAssets.contains(self.photoAssets[Int(index)]))! {
  89. return true
  90. }
  91. return false
  92. }
  93. public func photoBrowser(_ photoBrowser: MWPhotoBrowser!, photoAt index: UInt, selectedChanged selected: Bool) {
  94. if selected {
  95. controller?.selectImage(self.photoAssets[Int(index)])
  96. } else {
  97. controller?.deselectImage(self.photoAssets[Int(index)])
  98. }
  99. controller?.refreshView()
  100. }
  101. public func mediaSelectionCount() -> UInt {
  102. if controller != nil {
  103. return UInt(controller!.selectedAssets.count)
  104. }
  105. return 0
  106. }
  107. }