NCPickerViewController.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // NCPickerViewController.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 UIKit
  24. import TLPhotoPicker
  25. import MobileCoreServices
  26. //MARK: - Photo Picker
  27. class NCPhotosPickerViewController: NSObject {
  28. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  29. var sourceViewController: UIViewController
  30. var maxSelectedAssets = 1
  31. var singleSelectedMode = false
  32. @discardableResult
  33. init(viewController: UIViewController, maxSelectedAssets: Int, singleSelectedMode: Bool) {
  34. sourceViewController = viewController
  35. super.init()
  36. self.maxSelectedAssets = maxSelectedAssets
  37. self.singleSelectedMode = singleSelectedMode
  38. self.openPhotosPickerViewController { (assets) in
  39. guard let assets = assets else { return }
  40. if assets.count > 0 {
  41. let form = NCCreateFormUploadAssets.init(serverUrl: self.appDelegate.activeServerUrl, assets: assets, cryptated: false, session: NCNetworking.shared.sessionIdentifierBackground, delegate: nil)
  42. let navigationController = UINavigationController.init(rootViewController: form)
  43. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  44. viewController.present(navigationController, animated: true, completion: nil)
  45. }
  46. }
  47. }
  48. }
  49. private func openPhotosPickerViewController(completition: @escaping ([PHAsset]?) -> ()) {
  50. var selectedAssets: [PHAsset] = []
  51. var configure = TLPhotosPickerConfigure()
  52. configure.cancelTitle = NSLocalizedString("_cancel_", comment: "")
  53. configure.doneTitle = NSLocalizedString("_done_", comment: "")
  54. configure.emptyMessage = NSLocalizedString("_no_albums_", comment: "")
  55. configure.tapHereToChange = NSLocalizedString("_tap_here_to_change_", comment: "")
  56. if maxSelectedAssets > 0 {
  57. configure.maxSelectedAssets = maxSelectedAssets
  58. }
  59. configure.selectedColor = NCBrandColor.shared.brandElement
  60. configure.singleSelectedMode = singleSelectedMode
  61. configure.allowedAlbumCloudShared = true
  62. let viewController = customPhotoPickerViewController(withTLPHAssets: { (assets) in
  63. for asset: TLPHAsset in assets {
  64. if asset.phAsset != nil {
  65. selectedAssets.append(asset.phAsset!)
  66. }
  67. }
  68. completition(selectedAssets)
  69. }, didCancel: nil)
  70. viewController.didExceedMaximumNumberOfSelection = { (picker) in
  71. NCContentPresenter.shared.messageNotification("_info_", description: "_limited_dimension_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  72. }
  73. viewController.handleNoAlbumPermissions = { (picker) in
  74. NCContentPresenter.shared.messageNotification("_info_", description: "_denied_album_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  75. }
  76. viewController.handleNoCameraPermissions = { (picker) in
  77. NCContentPresenter.shared.messageNotification("_info_", description: "_denied_camera_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  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 = .systemBlue
  90. self.customNavItem.rightBarButtonItem?.tintColor = .systemBlue
  91. }
  92. }
  93. //MARK: - Document Picker
  94. class NCDocumentPickerViewController: NSObject, UIDocumentPickerDelegate {
  95. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  96. @discardableResult
  97. init (tabBarController: UITabBarController) {
  98. super.init()
  99. let documentProviderMenu = UIDocumentPickerViewController(documentTypes: ["public.data"], in: .import)
  100. documentProviderMenu.modalPresentationStyle = .formSheet
  101. documentProviderMenu.popoverPresentationController?.sourceView = tabBarController.tabBar
  102. documentProviderMenu.popoverPresentationController?.sourceRect = tabBarController.tabBar.bounds
  103. documentProviderMenu.delegate = self
  104. appDelegate.window?.rootViewController?.present(documentProviderMenu, animated: true, completion: nil)
  105. }
  106. func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
  107. for url in urls {
  108. let fileName = url.lastPathComponent
  109. let serverUrl = appDelegate.activeServerUrl
  110. let ocId = NSUUID().uuidString
  111. let atPath = url.path
  112. let toPath = CCUtility.getDirectoryProviderStorageOcId(ocId, fileNameView: fileName)!
  113. if NCUtilityFileSystem.shared.copyFile(atPath: atPath, toPath: toPath) {
  114. let metadataForUpload = NCManageDatabase.shared.createMetadata(account: appDelegate.account, user: appDelegate.user, userId: appDelegate.userId, fileName: fileName, fileNameView: fileName, ocId: ocId, serverUrl: serverUrl, urlBase: appDelegate.urlBase, url: "", contentType: "", livePhoto: false)
  115. metadataForUpload.session = NCNetworking.shared.sessionIdentifierBackground
  116. metadataForUpload.sessionSelector = NCGlobal.shared.selectorUploadFile
  117. metadataForUpload.size = NCUtilityFileSystem.shared.getFileSize(filePath: toPath)
  118. metadataForUpload.status = NCGlobal.shared.metadataStatusWaitUpload
  119. if NCManageDatabase.shared.getMetadataConflict(account: appDelegate.account, serverUrl: serverUrl, fileName: fileName) != nil {
  120. if let conflict = UIStoryboard.init(name: "NCCreateFormUploadConflict", bundle: nil).instantiateInitialViewController() as? NCCreateFormUploadConflict {
  121. conflict.serverUrl = serverUrl
  122. conflict.metadatasUploadInConflict = [metadataForUpload]
  123. appDelegate.window?.rootViewController?.present(conflict, animated: true, completion: nil)
  124. }
  125. } else {
  126. appDelegate.networkingProcessUpload?.createProcessUploads(metadatas: [metadataForUpload])
  127. }
  128. } else {
  129. NCContentPresenter.shared.messageNotification("_error_", description: "_read_file_error_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  130. }
  131. }
  132. }
  133. }