NCPickerViewController.swift 7.4 KB

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