NCMenuAction.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. /// Copy files to pasteboard
  77. static func copyAction(selectOcId: [String], hudView: UIView, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  78. NCMenuAction(
  79. title: NSLocalizedString("_copy_file_", comment: ""),
  80. icon: NCUtility.shared.loadImage(named: "doc.on.doc"),
  81. order: order,
  82. action: { _ in
  83. NCActionCenter.shared.copyPasteboard(pasteboardOcIds: selectOcId, hudView: hudView)
  84. completion?()
  85. }
  86. )
  87. }
  88. /// Delete files either from cache or from Nextcloud
  89. static func deleteAction(selectedMetadatas: [tableMetadata], metadataFolder: tableMetadata? = nil, viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  90. var titleDelete = NSLocalizedString("_delete_", comment: "")
  91. if selectedMetadatas.count > 1 {
  92. titleDelete = NSLocalizedString("_delete_selected_files_", comment: "")
  93. } else if let metadata = selectedMetadatas.first {
  94. if NCManageDatabase.shared.isMetadataShareOrMounted(metadata: metadata, metadataFolder: metadataFolder) {
  95. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  96. } else if metadata.directory {
  97. titleDelete = NSLocalizedString("_delete_folder_", comment: "")
  98. } else {
  99. titleDelete = NSLocalizedString("_delete_file_", comment: "")
  100. }
  101. if let metadataFolder = metadataFolder {
  102. let isShare = metadata.permissions.contains(NCGlobal.shared.permissionShared) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionShared)
  103. let isMounted = metadata.permissions.contains(NCGlobal.shared.permissionMounted) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionMounted)
  104. if isShare || isMounted {
  105. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  106. }
  107. }
  108. } // else: no metadata selected
  109. let canDeleteServer = selectedMetadatas.allSatisfy { !$0.lock }
  110. var fileList = ""
  111. for (ix, metadata) in selectedMetadatas.enumerated() {
  112. guard ix < 3 else { fileList += "\n - ..."; break }
  113. fileList += "\n - " + metadata.fileNameView
  114. }
  115. return NCMenuAction(
  116. title: titleDelete,
  117. icon: NCUtility.shared.loadImage(named: "trash"),
  118. order: order,
  119. action: { _ in
  120. let alertController = UIAlertController(
  121. title: titleDelete,
  122. message: NSLocalizedString("_want_delete_", comment: "") + fileList,
  123. preferredStyle: .alert)
  124. if canDeleteServer {
  125. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  126. Task {
  127. var error = NKError()
  128. for metadata in selectedMetadatas where error == .success {
  129. error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false)
  130. }
  131. if error != .success {
  132. NCContentPresenter.shared.showError(error: error)
  133. }
  134. let ocId = selectedMetadatas.map { $0.ocId }
  135. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "error": error])
  136. }
  137. completion?()
  138. })
  139. }
  140. // NCMedia removes image from collection view if removed from cache
  141. if !(viewController is NCMedia) {
  142. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (_: UIAlertAction) in
  143. Task {
  144. var error = NKError()
  145. for metadata in selectedMetadatas where error == .success {
  146. error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: true)
  147. }
  148. if error != .success {
  149. NCContentPresenter.shared.showError(error: error)
  150. }
  151. let ocId = selectedMetadatas.map { $0.ocId }
  152. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "error": error])
  153. }
  154. completion?()
  155. })
  156. }
  157. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (_: UIAlertAction) in })
  158. viewController.present(alertController, animated: true, completion: nil)
  159. }
  160. )
  161. }
  162. /// Open "share view" (activity VC) to open files in another app
  163. static func openInAction(selectedMetadatas: [tableMetadata], viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  164. NCMenuAction(
  165. title: NSLocalizedString("_open_in_", comment: ""),
  166. icon: NCUtility.shared.loadImage(named: "square.and.arrow.up"),
  167. order: order,
  168. action: { _ in
  169. NCActionCenter.shared.openActivityViewController(selectedMetadata: selectedMetadatas)
  170. completion?()
  171. }
  172. )
  173. }
  174. /// Save selected files to user's photo library
  175. static func saveMediaAction(selectedMediaMetadatas: [tableMetadata], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  176. var title: String = NSLocalizedString("_save_selected_files_", comment: "")
  177. var icon = NCUtility.shared.loadImage(named: "square.and.arrow.down")
  178. if selectedMediaMetadatas.allSatisfy({ NCManageDatabase.shared.getMetadataLivePhoto(metadata: $0) != nil }) {
  179. title = NSLocalizedString("_livephoto_save_", comment: "")
  180. icon = NCUtility.shared.loadImage(named: "livephoto")
  181. }
  182. return NCMenuAction(
  183. title: title,
  184. icon: icon,
  185. order: order,
  186. action: { _ in
  187. for metadata in selectedMediaMetadatas {
  188. if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  189. NCActionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV)
  190. } else {
  191. if CCUtility.fileProviderStorageExists(metadata) {
  192. NCActionCenter.shared.saveAlbum(metadata: metadata)
  193. } else {
  194. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  195. }
  196. }
  197. }
  198. completion?()
  199. }
  200. )
  201. }
  202. /// Set (or remove) a file as *available offline*. Downloads the file if not downloaded already
  203. static func setAvailableOfflineAction(selectedMetadatas: [tableMetadata], isAnyOffline: Bool, viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  204. NCMenuAction(
  205. title: isAnyOffline ? NSLocalizedString("_remove_available_offline_", comment: "") : NSLocalizedString("_set_available_offline_", comment: ""),
  206. icon: NCUtility.shared.loadImage(named: "tray.and.arrow.down"),
  207. order: order,
  208. action: { _ in
  209. if !isAnyOffline, selectedMetadatas.count > 3 {
  210. let alert = UIAlertController(
  211. title: NSLocalizedString("_set_available_offline_", comment: ""),
  212. message: NSLocalizedString("_select_offline_warning_", comment: ""),
  213. preferredStyle: .alert)
  214. alert.addAction(UIAlertAction(title: NSLocalizedString("_continue_", comment: ""), style: .default, handler: { _ in
  215. selectedMetadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  216. completion?()
  217. }))
  218. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel))
  219. viewController.present(alert, animated: true)
  220. } else {
  221. selectedMetadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  222. completion?()
  223. }
  224. }
  225. )
  226. }
  227. /// Open view that lets the user move or copy the files within Nextcloud
  228. static func moveOrCopyAction(selectedMetadatas: [tableMetadata], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  229. NCMenuAction(
  230. title: NSLocalizedString("_move_or_copy_selected_files_", comment: ""),
  231. icon: NCUtility.shared.loadImage(named: "arrow.up.right.square"),
  232. order: order,
  233. action: { _ in
  234. NCActionCenter.shared.openSelectView(items: selectedMetadatas)
  235. completion?()
  236. }
  237. )
  238. }
  239. /// Open AirPrint view to print a single file
  240. static func printAction(metadata: tableMetadata, order: Int = 0) -> NCMenuAction {
  241. NCMenuAction(
  242. title: NSLocalizedString("_print_", comment: ""),
  243. icon: NCUtility.shared.loadImage(named: "printer"),
  244. order: order,
  245. action: { _ in
  246. if CCUtility.fileProviderStorageExists(metadata) {
  247. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile, userInfo: ["ocId": metadata.ocId, "selector": NCGlobal.shared.selectorPrint, "error": NKError(), "account": metadata.account])
  248. } else {
  249. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorPrint) { _, _ in }
  250. }
  251. }
  252. )
  253. }
  254. /// Lock or unlock a file using *files_lock*
  255. static func lockUnlockFiles(shouldLock: Bool, metadatas: [tableMetadata], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  256. let titleKey: String
  257. if metadatas.count == 1 {
  258. titleKey = shouldLock ? "_lock_file_" : "_unlock_file_"
  259. } else {
  260. titleKey = shouldLock ? "_lock_selected_files_" : "_unlock_selected_files_"
  261. }
  262. let imageName = !shouldLock ? "lock_open" : "lock"
  263. return NCMenuAction(
  264. title: NSLocalizedString(titleKey, comment: ""),
  265. icon: NCUtility.shared.loadImage(named: imageName),
  266. order: order,
  267. action: { _ in
  268. for metadata in metadatas where metadata.lock != shouldLock {
  269. NCNetworking.shared.lockUnlockFile(metadata, shoulLock: shouldLock)
  270. }
  271. completion?()
  272. }
  273. )
  274. }
  275. }