NCContextMenu.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. let utilityFileSystem = NCUtilityFileSystem()
  29. let utility = NCUtility()
  30. func viewMenu(ocId: String, indexPath: IndexPath, viewController: UIViewController, image: UIImage?) -> UIMenu {
  31. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return UIMenu() }
  32. var downloadRequest: DownloadRequest?
  33. var titleDeleteConfirmFile = NSLocalizedString("_delete_file_", comment: "")
  34. var titleSave: String = NSLocalizedString("_save_selected_files_", comment: "")
  35. let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata)
  36. if metadata.directory { titleDeleteConfirmFile = NSLocalizedString("_delete_folder_", comment: "") }
  37. if metadataMOV != nil { titleSave = NSLocalizedString("_livephoto_save_", comment: "") }
  38. let hud = JGProgressHUD()
  39. hud.indicatorView = JGProgressHUDRingIndicatorView()
  40. hud.textLabel.text = NSLocalizedString("_downloading_", comment: "")
  41. hud.detailTextLabel.text = NSLocalizedString("_tap_to_cancel_", comment: "")
  42. if let indicatorView = hud.indicatorView as? JGProgressHUDRingIndicatorView { indicatorView.ringWidth = 1.5 }
  43. hud.tapOnHUDViewBlock = { _ in
  44. if let request = downloadRequest {
  45. request.cancel()
  46. }
  47. }
  48. // MENU ITEMS
  49. let detail = UIAction(title: NSLocalizedString("_details_", comment: ""),
  50. image: UIImage(systemName: "info")) { _ in
  51. NCActionCenter.shared.openShare(viewController: viewController, metadata: metadata, page: .activity)
  52. }
  53. let favorite = UIAction(title: metadata.favorite ?
  54. NSLocalizedString("_remove_favorites_", comment: "") :
  55. NSLocalizedString("_add_favorites_", comment: ""),
  56. image: utility.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite)) { _ in
  57. NCNetworking.shared.favoriteMetadata(metadata) { error in
  58. if error != .success {
  59. NCContentPresenter().showError(error: error)
  60. }
  61. }
  62. }
  63. let openIn = UIAction(title: NSLocalizedString("_open_in_", comment: ""),
  64. image: UIImage(systemName: "square.and.arrow.up") ) { _ in
  65. if self.utilityFileSystem.fileProviderStorageExists(metadata) {
  66. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile, userInfo: ["ocId": metadata.ocId, "selector": NCGlobal.shared.selectorOpenIn, "error": NKError(), "account": metadata.account])
  67. } else {
  68. hud.show(in: viewController.view)
  69. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorOpenIn, notificationCenterProgressTask: false) { request in
  70. downloadRequest = request
  71. } progressHandler: { progress in
  72. hud.progress = Float(progress.fractionCompleted)
  73. } completion: { afError, error in
  74. if error == .success || afError?.isExplicitlyCancelledError ?? false {
  75. hud.dismiss()
  76. } else {
  77. hud.indicatorView = JGProgressHUDErrorIndicatorView()
  78. hud.textLabel.text = error.description
  79. hud.dismiss(afterDelay: NCGlobal.shared.dismissAfterSecond)
  80. }
  81. }
  82. }
  83. }
  84. let viewInFolder = UIAction(title: NSLocalizedString("_view_in_folder_", comment: ""),
  85. image: UIImage(systemName: "questionmark.folder")) { _ in
  86. NCActionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileNameBlink: metadata.fileName, fileNameOpen: nil)
  87. }
  88. let save = UIAction(title: titleSave,
  89. image: UIImage(systemName: "square.and.arrow.down")) { _ in
  90. if let metadataMOV = metadataMOV {
  91. if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
  92. appDelegate.saveLivePhotoQueue.addOperation(NCOperationSaveLivePhoto(metadata: metadata, metadataMOV: metadataMOV))
  93. }
  94. } else {
  95. if self.utilityFileSystem.fileProviderStorageExists(metadata) {
  96. NCActionCenter.shared.saveAlbum(metadata: metadata)
  97. } else {
  98. hud.show(in: viewController.view)
  99. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum, notificationCenterProgressTask: false) { request in
  100. downloadRequest = request
  101. } progressHandler: { progress in
  102. hud.progress = Float(progress.fractionCompleted)
  103. } completion: { afError, error in
  104. if error == .success || afError?.isExplicitlyCancelledError ?? false {
  105. hud.dismiss()
  106. } else {
  107. hud.indicatorView = JGProgressHUDErrorIndicatorView()
  108. hud.textLabel.text = error.description
  109. hud.dismiss(afterDelay: NCGlobal.shared.dismissAfterSecond)
  110. }
  111. }
  112. }
  113. }
  114. }
  115. let modify = UIAction(title: NSLocalizedString("_modify_", comment: ""),
  116. image: UIImage(systemName: "pencil.tip.crop.circle")) { _ in
  117. if self.utilityFileSystem.fileProviderStorageExists(metadata) {
  118. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile, userInfo: ["ocId": metadata.ocId, "selector": NCGlobal.shared.selectorLoadFileQuickLook, "error": NKError(), "account": metadata.account])
  119. } else {
  120. hud.show(in: viewController.view)
  121. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook, notificationCenterProgressTask: false) { request in
  122. downloadRequest = request
  123. } progressHandler: { progress in
  124. hud.progress = Float(progress.fractionCompleted)
  125. } completion: { afError, error in
  126. if error == .success || afError?.isExplicitlyCancelledError ?? false {
  127. hud.dismiss()
  128. } else {
  129. hud.indicatorView = JGProgressHUDErrorIndicatorView()
  130. hud.textLabel.text = error.description
  131. hud.dismiss(afterDelay: NCGlobal.shared.dismissAfterSecond)
  132. }
  133. }
  134. }
  135. }
  136. let deleteConfirmFile = UIAction(title: titleDeleteConfirmFile,
  137. image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
  138. var alertStyle = UIAlertController.Style.actionSheet
  139. if UIDevice.current.userInterfaceIdiom == .pad {
  140. alertStyle = .alert
  141. }
  142. let alertController = UIAlertController(title: nil, message: nil, preferredStyle: alertStyle)
  143. alertController.addAction(UIAlertAction(title: NSLocalizedString("_delete_file_", comment: ""), style: .destructive) { _ in
  144. let hud = JGProgressHUD()
  145. hud.textLabel.text = NSLocalizedString("_deletion_progess_", comment: "")
  146. if let appDelegate = UIApplication.shared.delegate as? AppDelegate,
  147. let view = appDelegate.window?.rootViewController?.view {
  148. hud.show(in: view)
  149. }
  150. Task {
  151. var ocId: [String] = []
  152. let error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false)
  153. if error == .success {
  154. ocId.append(metadata.ocId)
  155. } else {
  156. NCContentPresenter().showError(error: error)
  157. }
  158. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "indexPath": [indexPath], "onlyLocalCache": false, "error": error, "hud": hud])
  159. }
  160. })
  161. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { _ in })
  162. viewController.present(alertController, animated: true, completion: nil)
  163. }
  164. let deleteConfirmLocal = UIAction(title: NSLocalizedString("_remove_local_file_", comment: ""),
  165. image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
  166. Task {
  167. var ocId: [String] = []
  168. let error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: true)
  169. if error == .success {
  170. ocId.append(metadata.ocId)
  171. } else {
  172. NCContentPresenter().showError(error: error)
  173. }
  174. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "indexPath": [indexPath], "onlyLocalCache": true, "error": error])
  175. }
  176. }
  177. let deleteSubMenu = UIMenu(title: NSLocalizedString("_delete_file_", comment: ""),
  178. image: UIImage(systemName: "trash"),
  179. options: .destructive,
  180. children: [deleteConfirmLocal, deleteConfirmFile])
  181. // ------ MENU -----
  182. var menu: [UIMenuElement] = []
  183. if metadata.directory {
  184. if metadata.isDirectoryE2EE || metadata.e2eEncrypted {
  185. menu.append(favorite)
  186. } else {
  187. menu.append(favorite)
  188. menu.append(deleteConfirmFile)
  189. }
  190. return UIMenu(title: "", children: [detail, UIMenu(title: "", options: .displayInline, children: menu)])
  191. } else {
  192. if metadata.lock {
  193. menu.append(favorite)
  194. if metadata.isDocumentViewableOnly {
  195. //
  196. } else {
  197. menu.append(openIn)
  198. // SAVE CAMERA ROLL
  199. menu.append(save)
  200. }
  201. } else {
  202. menu.append(favorite)
  203. if metadata.isDocumentViewableOnly {
  204. if viewController is NCMedia {
  205. menu.append(viewInFolder)
  206. }
  207. } else {
  208. menu.append(openIn)
  209. // SAVE CAMERA ROLL
  210. menu.append(save)
  211. if viewController is NCMedia {
  212. menu.append(viewInFolder)
  213. }
  214. // MODIFY WITH QUICK LOOK
  215. if metadata.isModifiableWithQuickLook {
  216. menu.append(modify)
  217. }
  218. }
  219. if viewController is NCMedia {
  220. menu.append(deleteConfirmFile)
  221. } else {
  222. menu.append(deleteSubMenu)
  223. }
  224. }
  225. return UIMenu(title: "", children: [detail, UIMenu(title: "", options: .displayInline, children: menu)])
  226. }
  227. }
  228. }