NCMenuAction.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. //
  2. // NCMenuAction.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 17.02.22.
  6. // Copyright © 2022 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Henrik Storch <henrik.storch@nextcloud.com>
  9. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  10. //
  11. // This program is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation, either version 3 of the License, or
  14. // (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. //
  24. import Foundation
  25. import UIKit
  26. import NextcloudKit
  27. class NCMenuAction {
  28. let title: String
  29. let details: String?
  30. let icon: UIImage
  31. let selectable: Bool
  32. var onTitle: String?
  33. var onIcon: UIImage?
  34. var selected: Bool = false
  35. var isOn: Bool = false
  36. var action: ((_ menuAction: NCMenuAction) -> Void)?
  37. var rowHeight: CGFloat { self.title == NCMenuAction.seperatorIdentifier ? NCMenuAction.seperatorHeight : self.details != nil ? 80 : 60 }
  38. var order: Int = 0
  39. init(title: String, details: String? = nil, icon: UIImage, order: Int = 0, action: ((_ menuAction: NCMenuAction) -> Void)?) {
  40. self.title = title
  41. self.details = details
  42. self.icon = icon
  43. self.action = action
  44. self.selectable = false
  45. self.order = order
  46. }
  47. init(title: String, details: String? = nil, icon: UIImage, onTitle: String? = nil, onIcon: UIImage? = nil, selected: Bool, on: Bool, order: Int = 0, action: ((_ menuAction: NCMenuAction) -> Void)?) {
  48. self.title = title
  49. self.details = details
  50. self.icon = icon
  51. self.onTitle = onTitle ?? title
  52. self.onIcon = onIcon ?? icon
  53. self.action = action
  54. self.selected = selected
  55. self.isOn = on
  56. self.selectable = true
  57. self.order = order
  58. }
  59. }
  60. // MARK: - Actions
  61. extension NCMenuAction {
  62. static let seperatorIdentifier = "NCMenuAction.SEPARATOR"
  63. static let seperatorHeight: CGFloat = 0.5
  64. /// A static seperator, with no actions, text, or icons
  65. static func seperator(order: Int = 0) -> NCMenuAction {
  66. return NCMenuAction(title: seperatorIdentifier, icon: UIImage(), order: order, action: nil)
  67. }
  68. /// Select all items
  69. static func selectAllAction(action: @escaping () -> Void) -> NCMenuAction {
  70. NCMenuAction(
  71. title: NSLocalizedString("_select_all_", comment: ""),
  72. icon: NCUtility.shared.loadImage(named: "checkmark.circle.fill"),
  73. action: { _ in action() }
  74. )
  75. }
  76. /// Cancel
  77. static func cancelAction(action: @escaping () -> Void) -> NCMenuAction {
  78. NCMenuAction(
  79. title: NSLocalizedString("_cancel_", comment: ""),
  80. icon: NCUtility.shared.loadImage(named: "xmark"),
  81. action: { _ in action() }
  82. )
  83. }
  84. /// Copy files to pasteboard
  85. static func copyAction(selectOcId: [String], hudView: UIView, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  86. NCMenuAction(
  87. title: NSLocalizedString("_copy_file_", comment: ""),
  88. icon: NCUtility.shared.loadImage(named: "doc.on.doc"),
  89. order: order,
  90. action: { _ in
  91. NCActionCenter.shared.copyPasteboard(pasteboardOcIds: selectOcId, hudView: hudView)
  92. completion?()
  93. }
  94. )
  95. }
  96. /// Delete files either from cache or from Nextcloud
  97. static func deleteAction(selectedMetadatas: [tableMetadata], metadataFolder: tableMetadata? = nil, viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  98. var titleDelete = NSLocalizedString("_delete_", comment: "")
  99. if selectedMetadatas.count > 1 {
  100. titleDelete = NSLocalizedString("_delete_selected_files_", comment: "")
  101. } else if let metadata = selectedMetadatas.first {
  102. if NCManageDatabase.shared.isMetadataShareOrMounted(metadata: metadata, metadataFolder: metadataFolder) {
  103. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  104. } else if metadata.directory {
  105. titleDelete = NSLocalizedString("_delete_folder_", comment: "")
  106. } else {
  107. titleDelete = NSLocalizedString("_delete_file_", comment: "")
  108. }
  109. if let metadataFolder = metadataFolder {
  110. let isShare = metadata.permissions.contains(NCGlobal.shared.permissionShared) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionShared)
  111. let isMounted = metadata.permissions.contains(NCGlobal.shared.permissionMounted) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionMounted)
  112. if isShare || isMounted {
  113. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  114. }
  115. }
  116. } // else: no metadata selected
  117. let canDeleteServer = selectedMetadatas.allSatisfy { !$0.lock }
  118. var fileList = ""
  119. for (ix, metadata) in selectedMetadatas.enumerated() {
  120. guard ix < 3 else { fileList += "\n - ..."; break }
  121. fileList += "\n - " + metadata.fileNameView
  122. }
  123. return NCMenuAction(
  124. title: titleDelete,
  125. icon: NCUtility.shared.loadImage(named: "trash"),
  126. order: order,
  127. action: { _ in
  128. let alertController = UIAlertController(
  129. title: titleDelete,
  130. message: NSLocalizedString("_want_delete_", comment: "") + fileList,
  131. preferredStyle: .alert)
  132. if canDeleteServer {
  133. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  134. Task {
  135. var error = NKError()
  136. var ocId: [String] = []
  137. let account = selectedMetadatas.first?.account ?? ""
  138. for metadata in selectedMetadatas where error == .success {
  139. error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false)
  140. if error == .success {
  141. ocId.append(metadata.ocId)
  142. }
  143. }
  144. if error != .success {
  145. NCContentPresenter.shared.showError(error: error)
  146. }
  147. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["account": account, "ocId": ocId, "error": error])
  148. }
  149. completion?()
  150. })
  151. }
  152. // NCMedia removes image from collection view if removed from cache
  153. if !(viewController is NCMedia) {
  154. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (_: UIAlertAction) in
  155. Task {
  156. var error = NKError()
  157. var ocId: [String] = []
  158. let account = selectedMetadatas.first?.account ?? ""
  159. for metadata in selectedMetadatas where error == .success {
  160. error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: true)
  161. if error == .success {
  162. ocId.append(metadata.ocId)
  163. }
  164. }
  165. if error != .success {
  166. NCContentPresenter.shared.showError(error: error)
  167. }
  168. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["account": account, "ocId": ocId, "error": error])
  169. }
  170. completion?()
  171. })
  172. }
  173. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (_: UIAlertAction) in })
  174. viewController.present(alertController, animated: true, completion: nil)
  175. }
  176. )
  177. }
  178. /// Open "share view" (activity VC) to open files in another app
  179. static func openInAction(selectedMetadatas: [tableMetadata], viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  180. NCMenuAction(
  181. title: NSLocalizedString("_open_in_", comment: ""),
  182. icon: NCUtility.shared.loadImage(named: "square.and.arrow.up"),
  183. order: order,
  184. action: { _ in
  185. NCActionCenter.shared.openActivityViewController(selectedMetadata: selectedMetadatas)
  186. completion?()
  187. }
  188. )
  189. }
  190. /// Save selected files to user's photo library
  191. static func saveMediaAction(selectedMediaMetadatas: [tableMetadata], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  192. var title: String = NSLocalizedString("_save_selected_files_", comment: "")
  193. var icon = NCUtility.shared.loadImage(named: "square.and.arrow.down")
  194. if selectedMediaMetadatas.allSatisfy({ NCManageDatabase.shared.getMetadataLivePhoto(metadata: $0) != nil }) {
  195. title = NSLocalizedString("_livephoto_save_", comment: "")
  196. icon = NCUtility.shared.loadImage(named: "livephoto")
  197. }
  198. return NCMenuAction(
  199. title: title,
  200. icon: icon,
  201. order: order,
  202. action: { _ in
  203. for metadata in selectedMediaMetadatas {
  204. if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  205. NCActionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV)
  206. } else {
  207. if CCUtility.fileProviderStorageExists(metadata) {
  208. NCActionCenter.shared.saveAlbum(metadata: metadata)
  209. } else {
  210. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  211. }
  212. }
  213. }
  214. completion?()
  215. }
  216. )
  217. }
  218. /// Set (or remove) a file as *available offline*. Downloads the file if not downloaded already
  219. static func setAvailableOfflineAction(selectedMetadatas: [tableMetadata], isAnyOffline: Bool, viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  220. NCMenuAction(
  221. title: isAnyOffline ? NSLocalizedString("_remove_available_offline_", comment: "") : NSLocalizedString("_set_available_offline_", comment: ""),
  222. icon: NCUtility.shared.loadImage(named: "tray.and.arrow.down"),
  223. order: order,
  224. action: { _ in
  225. if !isAnyOffline, selectedMetadatas.count > 3 {
  226. let alert = UIAlertController(
  227. title: NSLocalizedString("_set_available_offline_", comment: ""),
  228. message: NSLocalizedString("_select_offline_warning_", comment: ""),
  229. preferredStyle: .alert)
  230. alert.addAction(UIAlertAction(title: NSLocalizedString("_continue_", comment: ""), style: .default, handler: { _ in
  231. selectedMetadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  232. completion?()
  233. }))
  234. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel))
  235. viewController.present(alert, animated: true)
  236. } else {
  237. selectedMetadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  238. completion?()
  239. }
  240. }
  241. )
  242. }
  243. /// Open view that lets the user move or copy the files within Nextcloud
  244. static func moveOrCopyAction(selectedMetadatas: [tableMetadata], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  245. NCMenuAction(
  246. title: NSLocalizedString("_move_or_copy_selected_files_", comment: ""),
  247. icon: NCUtility.shared.loadImage(named: "arrow.up.right.square"),
  248. order: order,
  249. action: { _ in
  250. NCActionCenter.shared.openSelectView(items: selectedMetadatas)
  251. completion?()
  252. }
  253. )
  254. }
  255. /// Open AirPrint view to print a single file
  256. static func printAction(metadata: tableMetadata, order: Int = 0) -> NCMenuAction {
  257. NCMenuAction(
  258. title: NSLocalizedString("_print_", comment: ""),
  259. icon: NCUtility.shared.loadImage(named: "printer"),
  260. order: order,
  261. action: { _ in
  262. if CCUtility.fileProviderStorageExists(metadata) {
  263. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile, userInfo: ["ocId": metadata.ocId, "selector": NCGlobal.shared.selectorPrint, "error": NKError(), "account": metadata.account])
  264. } else {
  265. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorPrint) { _, _ in }
  266. }
  267. }
  268. )
  269. }
  270. /// Lock or unlock a file using *files_lock*
  271. static func lockUnlockFiles(shouldLock: Bool, metadatas: [tableMetadata], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  272. let titleKey: String
  273. if metadatas.count == 1 {
  274. titleKey = shouldLock ? "_lock_file_" : "_unlock_file_"
  275. } else {
  276. titleKey = shouldLock ? "_lock_selected_files_" : "_unlock_selected_files_"
  277. }
  278. let imageName = !shouldLock ? "lock_open" : "lock"
  279. return NCMenuAction(
  280. title: NSLocalizedString(titleKey, comment: ""),
  281. icon: NCUtility.shared.loadImage(named: imageName),
  282. order: order,
  283. action: { _ in
  284. for metadata in metadatas where metadata.lock != shouldLock {
  285. NCNetworking.shared.lockUnlockFile(metadata, shoulLock: shouldLock)
  286. }
  287. completion?()
  288. }
  289. )
  290. }
  291. }