NCPhotosPickerViewController.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // NCPhotosPickerViewController.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 11/11/2018.
  6. // Copyright (c) 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import TLPhotoPicker
  25. class NCPhotosPickerViewController: NSObject {
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. var sourceViewController: UIViewController
  28. var maxSelectedAssets = 1
  29. var singleSelectedMode = false
  30. @objc init (_ viewController: UIViewController, maxSelectedAssets: Int, singleSelectedMode: Bool) {
  31. sourceViewController = viewController
  32. self.maxSelectedAssets = maxSelectedAssets
  33. self.singleSelectedMode = singleSelectedMode
  34. }
  35. @objc func openPhotosPickerViewController(phAssets: @escaping ([PHAsset]?) -> ()) {
  36. var selectedPhAssets: [PHAsset] = []
  37. var configure = TLPhotosPickerConfigure()
  38. configure.cancelTitle = NSLocalizedString("_cancel_", comment: "")
  39. configure.doneTitle = NSLocalizedString("_done_", comment: "")
  40. configure.emptyMessage = NSLocalizedString("_no_albums_", comment: "")
  41. configure.tapHereToChange = NSLocalizedString("_tap_here_to_change_", comment: "")
  42. configure.maxSelectedAssets = self.maxSelectedAssets
  43. configure.selectedColor = NCBrandColor.sharedInstance.brandElement
  44. configure.singleSelectedMode = singleSelectedMode
  45. let viewController = customPhotoPickerViewController(withTLPHAssets: { (assets) in
  46. for asset: TLPHAsset in assets {
  47. if asset.phAsset != nil {
  48. selectedPhAssets.append(asset.phAsset!)
  49. }
  50. }
  51. phAssets(selectedPhAssets)
  52. }, didCancel: nil)
  53. /*
  54. let viewController = customPhotoPickerViewController(withTLPHAssets: { (assets) in
  55. for asset: TLPHAsset in assets {
  56. if asset.phAsset != nil {
  57. asset.tempCopyMediaFile(videoRequestOptions: nil, imageRequestOptions: nil, livePhotoRequestOptions: nil, exportPreset: AVAssetExportPresetHighestQuality, convertLivePhotosToJPG: false, progressBlock: { (progress) in }) { (url, contentType) in
  58. selectedPhAssets.append(asset.phAsset!)
  59. selectedUrls.append(url)
  60. if asset == assets.last {
  61. phAssets(selectedPhAssets, selectedUrls)
  62. }
  63. }
  64. }
  65. }
  66. }) {
  67. phAssets(nil,nil)
  68. }
  69. */
  70. viewController.didExceedMaximumNumberOfSelection = { (picker) in
  71. NCContentPresenter.shared.messageNotification("_info_", description: "_limited_dimension_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
  72. }
  73. viewController.handleNoAlbumPermissions = { (picker) in
  74. NCContentPresenter.shared.messageNotification("_info_", description: "_denied_album_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
  75. }
  76. viewController.handleNoCameraPermissions = { (picker) in
  77. NCContentPresenter.shared.messageNotification("_info_", description: "_denied_camera_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
  78. }
  79. viewController.configure = configure
  80. sourceViewController.present(viewController, animated: true, completion: nil)
  81. }
  82. }
  83. class customPhotoPickerViewController: TLPhotosPickerViewController {
  84. override var preferredStatusBarStyle: UIStatusBarStyle {
  85. return .lightContent
  86. }
  87. override func makeUI() {
  88. super.makeUI()
  89. self.customNavItem.leftBarButtonItem?.tintColor = NCBrandColor.sharedInstance.brandElement
  90. self.customNavItem.rightBarButtonItem?.tintColor = NCBrandColor.sharedInstance.brandElement
  91. }
  92. }