NCContextMenu.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // NCContextMenu.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 10/01/23.
  6. // Copyright © 2023 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 Alamofire
  25. import NextcloudKit
  26. import JGProgressHUD
  27. class NCContextMenu: NSObject {
  28. func viewMenu(ocId: String, viewController: UIViewController, image: UIImage?) -> UIMenu {
  29. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return UIMenu() }
  30. var downloadRequest: DownloadRequest?
  31. var titleDeleteConfirmFile = NSLocalizedString("_delete_file_", comment: "")
  32. var titleSave: String = NSLocalizedString("_save_selected_files_", comment: "")
  33. let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata)
  34. if metadata.directory { titleDeleteConfirmFile = NSLocalizedString("_delete_folder_", comment: "") }
  35. if metadataMOV != nil { titleSave = NSLocalizedString("_livephoto_save_", comment: "") }
  36. let hud = JGProgressHUD()
  37. hud.indicatorView = JGProgressHUDRingIndicatorView()
  38. hud.detailTextLabel.text = NSLocalizedString("_tap_to_cancel_", comment: "")
  39. if let indicatorView = hud.indicatorView as? JGProgressHUDRingIndicatorView { indicatorView.ringWidth = 1.5 }
  40. hud.tapOnHUDViewBlock = { _ in
  41. if let request = downloadRequest {
  42. request.cancel()
  43. }
  44. }
  45. // MENU ITEMS
  46. let detail = UIAction(title: NSLocalizedString("_details_", comment: ""),
  47. image: UIImage(systemName: "info")) { _ in
  48. NCActionCenter.shared.openShare(viewController: viewController, metadata: metadata, indexPage: .activity)
  49. }
  50. let favorite = UIAction(title: metadata.favorite ?
  51. NSLocalizedString("_remove_favorites_", comment: "") :
  52. NSLocalizedString("_add_favorites_", comment: ""),
  53. image: NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite)) { _ in
  54. NCNetworking.shared.favoriteMetadata(metadata) { error in
  55. if error != .success {
  56. NCContentPresenter.shared.showError(error: error)
  57. }
  58. }
  59. }
  60. let openIn = UIAction(title: NSLocalizedString("_open_in_", comment: ""),
  61. image: UIImage(systemName: "square.and.arrow.up") ) { _ in
  62. if CCUtility.fileProviderStorageExists(metadata) {
  63. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile, userInfo: ["ocId": metadata.ocId, "selector": NCGlobal.shared.selectorOpenIn, "error": NKError(), "account": metadata.account])
  64. } else {
  65. hud.show(in: viewController.view)
  66. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorOpenIn, notificationCenterProgressTask: false) { request in
  67. downloadRequest = request
  68. } progressHandler: { progress in
  69. hud.progress = Float(progress.fractionCompleted)
  70. } completion: { afError, error in
  71. if error == .success || afError?.isExplicitlyCancelledError ?? false {
  72. hud.dismiss()
  73. } else {
  74. hud.indicatorView = JGProgressHUDErrorIndicatorView()
  75. hud.textLabel.text = error.description
  76. hud.dismiss(afterDelay: NCGlobal.shared.dismissAfterSecond)
  77. }
  78. }
  79. }
  80. }
  81. let viewInFolder = UIAction(title: NSLocalizedString("_view_in_folder_", comment: ""),
  82. image: UIImage(systemName: "arrow.forward.square")) { _ in
  83. NCActionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileNameBlink: metadata.fileName, fileNameOpen: nil)
  84. }
  85. let save = UIAction(title: titleSave,
  86. image: UIImage(systemName: "square.and.arrow.down")) { _ in
  87. if let metadataMOV = metadataMOV {
  88. NCActionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV)
  89. } else {
  90. if CCUtility.fileProviderStorageExists(metadata) {
  91. NCActionCenter.shared.saveAlbum(metadata: metadata)
  92. } else {
  93. hud.show(in: viewController.view)
  94. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum, notificationCenterProgressTask: false) { request in
  95. downloadRequest = request
  96. } progressHandler: { progress in
  97. hud.progress = Float(progress.fractionCompleted)
  98. } completion: { afError, error in
  99. if error == .success || afError?.isExplicitlyCancelledError ?? false {
  100. hud.dismiss()
  101. } else {
  102. hud.indicatorView = JGProgressHUDErrorIndicatorView()
  103. hud.textLabel.text = error.description
  104. hud.dismiss(afterDelay: NCGlobal.shared.dismissAfterSecond)
  105. }
  106. }
  107. }
  108. }
  109. }
  110. let copy = UIAction(title: NSLocalizedString("_copy_file_", comment: ""),
  111. image: UIImage(systemName: "doc.on.doc")) { _ in
  112. NCActionCenter.shared.copyPasteboard(pasteboardOcIds: [metadata.ocId], hudView: viewController.view)
  113. }
  114. let modify = UIAction(title: NSLocalizedString("_modify_", comment: ""),
  115. image: UIImage(systemName: "pencil.tip.crop.circle")) { _ in
  116. if CCUtility.fileProviderStorageExists(metadata) {
  117. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile, userInfo: ["ocId": metadata.ocId, "selector": NCGlobal.shared.selectorLoadFileQuickLook, "error": NKError(), "account": metadata.account])
  118. } else {
  119. hud.show(in: viewController.view)
  120. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook, notificationCenterProgressTask: false) { request in
  121. downloadRequest = request
  122. } progressHandler: { progress in
  123. hud.progress = Float(progress.fractionCompleted)
  124. } completion: { afError, error in
  125. if error == .success || afError?.isExplicitlyCancelledError ?? false {
  126. hud.dismiss()
  127. } else {
  128. hud.indicatorView = JGProgressHUDErrorIndicatorView()
  129. hud.textLabel.text = error.description
  130. hud.dismiss(afterDelay: NCGlobal.shared.dismissAfterSecond)
  131. }
  132. }
  133. }
  134. }
  135. let deleteConfirmFile = UIAction(title: titleDeleteConfirmFile,
  136. image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
  137. let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  138. alertController.addAction(UIAlertAction(title: NSLocalizedString("_delete_file_", comment: ""), style: .destructive) { _ in
  139. NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false) { error in
  140. if error != .success {
  141. NCContentPresenter.shared.showError(error: error)
  142. }
  143. }
  144. })
  145. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { _ in })
  146. viewController.present(alertController, animated: true, completion: nil)
  147. }
  148. let deleteConfirmLocal = UIAction(title: NSLocalizedString("_remove_local_file_", comment: ""),
  149. image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
  150. NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: true) { _ in }
  151. }
  152. let deleteSubMenu = UIMenu(title: NSLocalizedString("_delete_file_", comment: ""),
  153. image: UIImage(systemName: "trash"),
  154. options: .destructive,
  155. children: [deleteConfirmLocal, deleteConfirmFile])
  156. // ------ MENU -----
  157. var menu: [UIMenuElement] = []
  158. if metadata.directory {
  159. if metadata.isDirectoryE2EE || metadata.e2eEncrypted {
  160. menu.append(favorite)
  161. } else {
  162. menu.append(favorite)
  163. menu.append(deleteConfirmFile)
  164. }
  165. return UIMenu(title: "", children: [detail, UIMenu(title: "", options: .displayInline, children: menu)])
  166. } else {
  167. if metadata.lock {
  168. menu.append(favorite)
  169. if metadata.isDocumentViewableOnly {
  170. //
  171. } else {
  172. menu.append(openIn)
  173. // SAVE CAMERA ROLL
  174. menu.append(save)
  175. }
  176. } else {
  177. menu.append(favorite)
  178. if metadata.isDocumentViewableOnly {
  179. if viewController is NCMedia {
  180. menu.append(viewInFolder)
  181. }
  182. } else {
  183. menu.append(openIn)
  184. // SAVE CAMERA ROLL
  185. menu.append(save)
  186. if viewController is NCMedia {
  187. menu.append(viewInFolder)
  188. }
  189. // MODIFY WITH QUICK LOOK
  190. if metadata.isModifiableWithQuickLook {
  191. menu.append(modify)
  192. }
  193. }
  194. if viewController is NCMedia {
  195. menu.append(deleteConfirmFile)
  196. } else {
  197. menu.append(deleteSubMenu)
  198. }
  199. }
  200. return UIMenu(title: "", children: [detail, UIMenu(title: "", options: .displayInline, children: menu)])
  201. }
  202. }
  203. }