NCContextMenu.swift 12 KB

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