NCPhotosPickerViewController.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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]?, [URL]?) -> ()) {
  36. var selectedPhAssets = [PHAsset]()
  37. var selectedUrls = [URL]()
  38. var configure = TLPhotosPickerConfigure()
  39. configure.cancelTitle = NSLocalizedString("_cancel_", comment: "")
  40. configure.doneTitle = NSLocalizedString("_done_", comment: "")
  41. configure.emptyMessage = NSLocalizedString("_no_albums_", comment: "")
  42. configure.tapHereToChange = NSLocalizedString("_tap_here_to_change_", comment: "")
  43. configure.maxSelectedAssets = self.maxSelectedAssets
  44. configure.selectedColor = NCBrandColor.sharedInstance.brand
  45. configure.singleSelectedMode = singleSelectedMode
  46. let viewController = customPhotoPickerViewController(withTLPHAssets: { (assets) in
  47. for asset: TLPHAsset in assets {
  48. if asset.phAsset != nil {
  49. asset.tempCopyMediaFile(videoRequestOptions: nil, imageRequestOptions: nil, livePhotoRequestOptions: nil, exportPreset: AVAssetExportPresetHighestQuality, convertLivePhotosToJPG: false, progressBlock: { (progress) in }) { (url, contentType) in
  50. selectedPhAssets.append(asset.phAsset!)
  51. selectedUrls.append(url)
  52. if asset == assets.last {
  53. phAssets(selectedPhAssets, selectedUrls)
  54. }
  55. }
  56. }
  57. }
  58. }) {
  59. phAssets(nil,nil)
  60. }
  61. viewController.didExceedMaximumNumberOfSelection = { (picker) in
  62. NCContentPresenter.shared.messageNotification("_info_", description: "_limited_dimension_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
  63. }
  64. viewController.handleNoAlbumPermissions = { (picker) in
  65. NCContentPresenter.shared.messageNotification("_info_", description: "_denied_album_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
  66. }
  67. viewController.handleNoCameraPermissions = { (picker) in
  68. NCContentPresenter.shared.messageNotification("_info_", description: "_denied_camera_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
  69. }
  70. viewController.configure = configure
  71. sourceViewController.present(viewController, animated: true, completion: nil)
  72. }
  73. }
  74. class customPhotoPickerViewController: TLPhotosPickerViewController {
  75. override var preferredStatusBarStyle: UIStatusBarStyle {
  76. return .lightContent
  77. }
  78. override func makeUI() {
  79. super.makeUI()
  80. self.customNavItem.leftBarButtonItem?.tintColor = NCBrandColor.sharedInstance.brand
  81. self.customNavItem.rightBarButtonItem?.tintColor = NCBrandColor.sharedInstance.brand
  82. /*
  83. self.titleLabel.textColor = NCBrandColor.sharedInstance.brand
  84. self.subTitleLabel.textColor = NCBrandColor.sharedInstance.brand
  85. self.subTitleArrowImageView.image = CCGraphics.changeThemingColorImage(self.subTitleArrowImageView.image, width: 100, height: 100, color: NCBrandColor.sharedInstance.brand)
  86. */
  87. }
  88. }