NCContextMenu.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. NCNetworking.shared.saveLivePhotoQueue.addOperation(NCOperationSaveLivePhoto(metadata: metadata, metadataMOV: metadataMOV))
  92. } else {
  93. if self.utilityFileSystem.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 self.utilityFileSystem.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. Task {
  143. var ocId: [String] = []
  144. let error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false)
  145. if error == .success {
  146. ocId.append(metadata.ocId)
  147. } else {
  148. NCContentPresenter().showError(error: error)
  149. }
  150. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "indexPath": [indexPath], "onlyLocalCache": false, "error": error])
  151. }
  152. })
  153. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { _ in })
  154. viewController.present(alertController, animated: true, completion: nil)
  155. }
  156. let deleteConfirmLocal = UIAction(title: NSLocalizedString("_remove_local_file_", comment: ""),
  157. image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
  158. Task {
  159. var ocId: [String] = []
  160. let error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: true)
  161. if error == .success {
  162. ocId.append(metadata.ocId)
  163. } else {
  164. NCContentPresenter().showError(error: error)
  165. }
  166. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "indexPath": [indexPath], "onlyLocalCache": true, "error": error])
  167. }
  168. }
  169. let deleteSubMenu = UIMenu(title: NSLocalizedString("_delete_file_", comment: ""),
  170. image: UIImage(systemName: "trash"),
  171. options: .destructive,
  172. children: [deleteConfirmLocal, deleteConfirmFile])
  173. // ------ MENU -----
  174. var menu: [UIMenuElement] = []
  175. if metadata.directory {
  176. if metadata.isDirectoryE2EE || metadata.e2eEncrypted {
  177. menu.append(favorite)
  178. } else {
  179. menu.append(favorite)
  180. menu.append(deleteConfirmFile)
  181. }
  182. return UIMenu(title: "", children: [detail, UIMenu(title: "", options: .displayInline, children: menu)])
  183. } else {
  184. if metadata.lock {
  185. menu.append(favorite)
  186. if metadata.isDocumentViewableOnly {
  187. //
  188. } else {
  189. menu.append(openIn)
  190. // SAVE CAMERA ROLL
  191. menu.append(save)
  192. }
  193. } else {
  194. menu.append(favorite)
  195. if metadata.isDocumentViewableOnly {
  196. if viewController is NCMedia {
  197. menu.append(viewInFolder)
  198. }
  199. } else {
  200. menu.append(openIn)
  201. // SAVE CAMERA ROLL
  202. menu.append(save)
  203. if viewController is NCMedia {
  204. menu.append(viewInFolder)
  205. }
  206. // MODIFY WITH QUICK LOOK
  207. if metadata.isModifiableWithQuickLook {
  208. menu.append(modify)
  209. }
  210. }
  211. if viewController is NCMedia {
  212. menu.append(deleteConfirmFile)
  213. } else {
  214. menu.append(deleteSubMenu)
  215. }
  216. }
  217. return UIMenu(title: "", children: [detail, UIMenu(title: "", options: .displayInline, children: menu)])
  218. }
  219. }
  220. }