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