NCPickerViewController.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. import Photos
  27. import NextcloudKit
  28. // MARK: - Photo Picker
  29. class NCPhotosPickerViewController: NSObject {
  30. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. var sourceViewController: UIViewController
  32. var maxSelectedAssets = 1
  33. var singleSelectedMode = false
  34. @discardableResult
  35. init(viewController: UIViewController, maxSelectedAssets: Int, singleSelectedMode: Bool) {
  36. sourceViewController = viewController
  37. super.init()
  38. self.maxSelectedAssets = maxSelectedAssets
  39. self.singleSelectedMode = singleSelectedMode
  40. self.openPhotosPickerViewController { assets in
  41. if !assets.isEmpty {
  42. let vc = NCHostingUploadAssetsView().makeShipDetailsUI(assets: assets, serverUrl: self.appDelegate.activeServerUrl, userBaseUrl: self.appDelegate)
  43. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  44. viewController.present(vc, animated: true, completion: nil)
  45. }
  46. }
  47. }
  48. }
  49. private func openPhotosPickerViewController(completition: @escaping ([TLPHAsset]) -> Void) {
  50. var configure = TLPhotosPickerConfigure()
  51. configure.cancelTitle = NSLocalizedString("_cancel_", comment: "")
  52. configure.doneTitle = NSLocalizedString("_done_", comment: "")
  53. configure.emptyMessage = NSLocalizedString("_no_albums_", comment: "")
  54. configure.tapHereToChange = NSLocalizedString("_tap_here_to_change_", comment: "")
  55. if maxSelectedAssets > 0 {
  56. configure.maxSelectedAssets = maxSelectedAssets
  57. }
  58. configure.selectedColor = NCBrandColor.shared.brandElement
  59. configure.singleSelectedMode = singleSelectedMode
  60. configure.allowedAlbumCloudShared = true
  61. let viewController = customPhotoPickerViewController(withTLPHAssets: { assets in
  62. completition(assets)
  63. }, didCancel: nil)
  64. viewController.didExceedMaximumNumberOfSelection = { _ in
  65. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_limited_dimension_")
  66. NCContentPresenter.shared.showError(error: error)
  67. }
  68. viewController.handleNoAlbumPermissions = { _ in
  69. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_denied_album_")
  70. NCContentPresenter.shared.showError(error: error)
  71. }
  72. viewController.handleNoCameraPermissions = { _ in
  73. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_denied_camera_")
  74. NCContentPresenter.shared.showError(error: error)
  75. }
  76. viewController.configure = configure
  77. sourceViewController.present(viewController, animated: true, completion: nil)
  78. }
  79. }
  80. class customPhotoPickerViewController: TLPhotosPickerViewController {
  81. override var preferredStatusBarStyle: UIStatusBarStyle {
  82. return .lightContent
  83. }
  84. override func makeUI() {
  85. super.makeUI()
  86. self.customNavItem.leftBarButtonItem?.tintColor = .systemBlue
  87. self.customNavItem.rightBarButtonItem?.tintColor = .systemBlue
  88. }
  89. }
  90. // MARK: - Document Picker
  91. class NCDocumentPickerViewController: NSObject, UIDocumentPickerDelegate {
  92. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  93. @discardableResult
  94. init (tabBarController: UITabBarController) {
  95. super.init()
  96. let documentProviderMenu = UIDocumentPickerViewController(documentTypes: ["public.data"], in: .import)
  97. documentProviderMenu.modalPresentationStyle = .formSheet
  98. documentProviderMenu.allowsMultipleSelection = true
  99. documentProviderMenu.popoverPresentationController?.sourceView = tabBarController.tabBar
  100. documentProviderMenu.popoverPresentationController?.sourceRect = tabBarController.tabBar.bounds
  101. documentProviderMenu.delegate = self
  102. appDelegate.window?.rootViewController?.present(documentProviderMenu, animated: true, completion: nil)
  103. }
  104. func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
  105. for url in urls {
  106. let fileName = url.lastPathComponent
  107. let serverUrl = appDelegate.activeServerUrl
  108. let ocId = NSUUID().uuidString
  109. let atPath = url.path
  110. let toPath = CCUtility.getDirectoryProviderStorageOcId(ocId, fileNameView: fileName)!
  111. if NCUtilityFileSystem.shared.copyFile(atPath: atPath, toPath: toPath) {
  112. 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: "")
  113. metadataForUpload.session = NCNetworking.shared.sessionIdentifierBackground
  114. metadataForUpload.sessionSelector = NCGlobal.shared.selectorUploadFile
  115. metadataForUpload.size = NCUtilityFileSystem.shared.getFileSize(filePath: toPath)
  116. metadataForUpload.status = NCGlobal.shared.metadataStatusWaitUpload
  117. if NCManageDatabase.shared.getMetadataConflict(account: appDelegate.account, serverUrl: serverUrl, fileNameView: fileName) != nil {
  118. if let conflict = UIStoryboard(name: "NCCreateFormUploadConflict", bundle: nil).instantiateInitialViewController() as? NCCreateFormUploadConflict {
  119. conflict.delegate = appDelegate
  120. conflict.serverUrl = serverUrl
  121. conflict.metadatasUploadInConflict = [metadataForUpload]
  122. appDelegate.window?.rootViewController?.present(conflict, animated: true, completion: nil)
  123. }
  124. } else {
  125. NCNetworkingProcessUpload.shared.createProcessUploads(metadatas: [metadataForUpload], completion: { _ in })
  126. }
  127. } else {
  128. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_read_file_error_")
  129. NCContentPresenter.shared.showError(error: error)
  130. }
  131. }
  132. }
  133. }