NCContextMenu.swift 12 KB

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