NCContextMenu.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
  90. appDelegate.saveLivePhotoQueue.addOperation(NCOperationSaveLivePhoto(metadata: metadata, metadataMOV: metadataMOV))
  91. }
  92. } else {
  93. if CCUtility.fileProviderStorageExists(metadata) {
  94. NCActionCenter.shared.saveAlbum(metadata: metadata)
  95. } else {
  96. hud.show(in: viewController.view)
  97. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum, notificationCenterProgressTask: false) { request in
  98. downloadRequest = request
  99. } progressHandler: { progress in
  100. hud.progress = Float(progress.fractionCompleted)
  101. } completion: { afError, error in
  102. if error == .success || afError?.isExplicitlyCancelledError ?? false {
  103. hud.dismiss()
  104. } else {
  105. hud.indicatorView = JGProgressHUDErrorIndicatorView()
  106. hud.textLabel.text = error.description
  107. hud.dismiss(afterDelay: NCGlobal.shared.dismissAfterSecond)
  108. }
  109. }
  110. }
  111. }
  112. }
  113. let modify = UIAction(title: NSLocalizedString("_modify_", comment: ""),
  114. image: UIImage(systemName: "pencil.tip.crop.circle")) { _ in
  115. if CCUtility.fileProviderStorageExists(metadata) {
  116. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile, userInfo: ["ocId": metadata.ocId, "selector": NCGlobal.shared.selectorLoadFileQuickLook, "error": NKError(), "account": metadata.account])
  117. } else {
  118. hud.show(in: viewController.view)
  119. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook, notificationCenterProgressTask: false) { request in
  120. downloadRequest = request
  121. } progressHandler: { progress in
  122. hud.progress = Float(progress.fractionCompleted)
  123. } completion: { afError, error in
  124. if error == .success || afError?.isExplicitlyCancelledError ?? false {
  125. hud.dismiss()
  126. } else {
  127. hud.indicatorView = JGProgressHUDErrorIndicatorView()
  128. hud.textLabel.text = error.description
  129. hud.dismiss(afterDelay: NCGlobal.shared.dismissAfterSecond)
  130. }
  131. }
  132. }
  133. }
  134. let deleteConfirmFile = UIAction(title: titleDeleteConfirmFile,
  135. image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
  136. var alertStyle = UIAlertController.Style.actionSheet
  137. if UIDevice.current.userInterfaceIdiom == .pad {
  138. alertStyle = .alert
  139. }
  140. let alertController = UIAlertController(title: nil, message: nil, preferredStyle: alertStyle)
  141. alertController.addAction(UIAlertAction(title: NSLocalizedString("_delete_file_", comment: ""), style: .destructive) { _ in
  142. let hud = JGProgressHUD()
  143. hud.textLabel.text = NSLocalizedString("_deletion_progess_", comment: "")
  144. if let appDelegate = UIApplication.shared.delegate as? AppDelegate,
  145. let view = appDelegate.window?.rootViewController?.view {
  146. hud.show(in: view)
  147. }
  148. Task {
  149. var ocId: [String] = []
  150. let error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false)
  151. if error == .success {
  152. ocId.append(metadata.ocId)
  153. } else {
  154. NCContentPresenter.shared.showError(error: error)
  155. }
  156. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "indexPath": [indexPath], "onlyLocalCache": false, "error": error, "hud": hud])
  157. }
  158. })
  159. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { _ in })
  160. viewController.present(alertController, animated: true, completion: nil)
  161. }
  162. let deleteConfirmLocal = UIAction(title: NSLocalizedString("_remove_local_file_", comment: ""),
  163. image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
  164. Task {
  165. var ocId: [String] = []
  166. let error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: true)
  167. if error == .success {
  168. ocId.append(metadata.ocId)
  169. } else {
  170. NCContentPresenter.shared.showError(error: error)
  171. }
  172. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "indexPath": [indexPath], "onlyLocalCache": true, "error": error])
  173. }
  174. }
  175. let deleteSubMenu = UIMenu(title: NSLocalizedString("_delete_file_", comment: ""),
  176. image: UIImage(systemName: "trash"),
  177. options: .destructive,
  178. children: [deleteConfirmLocal, deleteConfirmFile])
  179. // ------ MENU -----
  180. var menu: [UIMenuElement] = []
  181. if metadata.directory {
  182. if metadata.isDirectoryE2EE || metadata.e2eEncrypted {
  183. menu.append(favorite)
  184. } else {
  185. menu.append(favorite)
  186. menu.append(deleteConfirmFile)
  187. }
  188. return UIMenu(title: "", children: [detail, UIMenu(title: "", options: .displayInline, children: menu)])
  189. } else {
  190. if metadata.lock {
  191. menu.append(favorite)
  192. if metadata.isDocumentViewableOnly {
  193. //
  194. } else {
  195. menu.append(openIn)
  196. // SAVE CAMERA ROLL
  197. menu.append(save)
  198. }
  199. } else {
  200. menu.append(favorite)
  201. if metadata.isDocumentViewableOnly {
  202. if viewController is NCMedia {
  203. menu.append(viewInFolder)
  204. }
  205. } else {
  206. menu.append(openIn)
  207. // SAVE CAMERA ROLL
  208. menu.append(save)
  209. if viewController is NCMedia {
  210. menu.append(viewInFolder)
  211. }
  212. // MODIFY WITH QUICK LOOK
  213. if metadata.isModifiableWithQuickLook {
  214. menu.append(modify)
  215. }
  216. }
  217. if viewController is NCMedia {
  218. menu.append(deleteConfirmFile)
  219. } else {
  220. menu.append(deleteSubMenu)
  221. }
  222. }
  223. return UIMenu(title: "", children: [detail, UIMenu(title: "", options: .displayInline, children: menu)])
  224. }
  225. }
  226. }