NCMenuAction.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. import Foundation
  9. import UIKit
  10. class NCMenuAction {
  11. let title: String
  12. let icon: UIImage
  13. let selectable: Bool
  14. var onTitle: String?
  15. var onIcon: UIImage?
  16. var selected: Bool = false
  17. var isOn: Bool = false
  18. var action: ((_ menuAction: NCMenuAction) -> Void)?
  19. init(title: String, icon: UIImage, action: ((_ menuAction: NCMenuAction) -> Void)?) {
  20. self.title = title
  21. self.icon = icon
  22. self.action = action
  23. self.selectable = false
  24. }
  25. init(title: String, icon: UIImage, onTitle: String? = nil, onIcon: UIImage? = nil, selected: Bool, on: Bool, action: ((_ menuAction: NCMenuAction) -> Void)?) {
  26. self.title = title
  27. self.icon = icon
  28. self.onTitle = onTitle ?? title
  29. self.onIcon = onIcon ?? icon
  30. self.action = action
  31. self.selected = selected
  32. self.isOn = on
  33. self.selectable = true
  34. }
  35. }
  36. // MARK: - Actions
  37. extension NCMenuAction {
  38. static let seperatorIdentifier = "NCMenuAction.SEPERATOR"
  39. /// A static seperator, with no actions, text, or icons
  40. static var seperator: NCMenuAction {
  41. return NCMenuAction(title: seperatorIdentifier, icon: UIImage(), action: nil)
  42. }
  43. /// Select all items
  44. static func selectAllAction(action: @escaping () -> Void) -> NCMenuAction {
  45. NCMenuAction(
  46. title: NSLocalizedString("_select_all_", comment: ""),
  47. icon: NCUtility.shared.loadImage(named: "checkmark.circle.fill"),
  48. action: { _ in action() }
  49. )
  50. }
  51. /// Copy files to pasteboard
  52. static func copyAction(selectOcId: [String], hudView: UIView, completion: (() -> Void)? = nil) -> NCMenuAction {
  53. NCMenuAction(
  54. title: NSLocalizedString("_copy_file_", comment: ""),
  55. icon: NCUtility.shared.loadImage(named: "doc.on.doc"),
  56. action: { _ in
  57. NCFunctionCenter.shared.copyPasteboard(pasteboardOcIds: selectOcId, hudView: hudView)
  58. completion?()
  59. }
  60. )
  61. }
  62. /// Delete files either from cache or from Nextcloud
  63. static func deleteAction(selectedMetadatas: [tableMetadata], metadataFolder: tableMetadata? = nil, viewController: UIViewController, completion: (() -> Void)? = nil) -> NCMenuAction {
  64. var titleDelete = NSLocalizedString("_delete_", comment: "")
  65. if selectedMetadatas.count > 1 {
  66. titleDelete = NSLocalizedString("_delete_selected_files_", comment: "")
  67. } else if let metadata = selectedMetadatas.first {
  68. if NCManageDatabase.shared.isMetadataShareOrMounted(metadata: metadata, metadataFolder: metadataFolder) {
  69. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  70. } else if metadata.directory {
  71. titleDelete = NSLocalizedString("_delete_folder_", comment: "")
  72. } else {
  73. titleDelete = NSLocalizedString("_delete_file_", comment: "")
  74. }
  75. if let metadataFolder = metadataFolder {
  76. let isShare = metadata.permissions.contains(NCGlobal.shared.permissionShared) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionShared)
  77. let isMounted = metadata.permissions.contains(NCGlobal.shared.permissionMounted) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionMounted)
  78. if isShare || isMounted {
  79. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  80. }
  81. }
  82. } // else: no metadata selected
  83. let canDeleteServer = selectedMetadatas.contains(
  84. where: { $0.canUnlock(as: (UIApplication.shared.delegate as? AppDelegate)?.userId ?? "") })
  85. var fileList = ""
  86. for (ix, metadata) in selectedMetadatas.enumerated() {
  87. guard ix < 3 else { fileList += "\n - ..."; break }
  88. fileList += "\n - " + metadata.fileName
  89. }
  90. return NCMenuAction(
  91. title: titleDelete,
  92. icon: NCUtility.shared.loadImage(named: "trash"),
  93. action: { _ in
  94. let alertController = UIAlertController(
  95. title: titleDelete,
  96. message: NSLocalizedString("_want_delete_", comment: "") + fileList,
  97. preferredStyle: .alert)
  98. if canDeleteServer {
  99. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  100. selectedMetadatas.forEach({ NCOperationQueue.shared.delete(metadata: $0, onlyLocalCache: false) })
  101. completion?()
  102. })
  103. }
  104. // NCMedia removes image from collection view if removed from cache
  105. if !(viewController is NCMedia) {
  106. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (_: UIAlertAction) in
  107. selectedMetadatas.forEach({ NCOperationQueue.shared.delete(metadata: $0, onlyLocalCache: true) })
  108. completion?()
  109. })
  110. }
  111. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (_: UIAlertAction) in })
  112. viewController.present(alertController, animated: true, completion: nil)
  113. }
  114. )
  115. }
  116. /// Open "share view" (activity VC) to open files in another app
  117. static func openInAction(selectedMetadatas: [tableMetadata], viewController: UIViewController, completion: (() -> Void)? = nil) -> NCMenuAction {
  118. NCMenuAction(
  119. title: NSLocalizedString("_open_in_", comment: ""),
  120. icon: NCUtility.shared.loadImage(named: "square.and.arrow.up"),
  121. action: { _ in
  122. if viewController is NCFileViewInFolder {
  123. viewController.dismiss(animated: true) {
  124. NCFunctionCenter.shared.openActivityViewController(selectedMetadata: selectedMetadatas)
  125. }
  126. } else {
  127. NCFunctionCenter.shared.openActivityViewController(selectedMetadata: selectedMetadatas)
  128. }
  129. completion?()
  130. }
  131. )
  132. }
  133. /// Save selected files to user's photo library
  134. static func saveMediaAction(selectedMediaMetadatas: [tableMetadata], completion: (() -> Void)? = nil) -> NCMenuAction {
  135. var title: String = NSLocalizedString("_save_selected_files_", comment: "")
  136. var icon = NCUtility.shared.loadImage(named: "square.and.arrow.down")
  137. if selectedMediaMetadatas.allSatisfy({ NCManageDatabase.shared.getMetadataLivePhoto(metadata: $0) != nil }) {
  138. title = NSLocalizedString("_livephoto_save_", comment: "")
  139. icon = NCUtility.shared.loadImage(named: "livephoto")
  140. }
  141. return NCMenuAction(
  142. title: title,
  143. icon: icon,
  144. action: { _ in
  145. for metadata in selectedMediaMetadatas {
  146. if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  147. NCFunctionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV)
  148. } else {
  149. if CCUtility.fileProviderStorageExists(metadata) {
  150. NCFunctionCenter.shared.saveAlbum(metadata: metadata)
  151. } else {
  152. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  153. }
  154. }
  155. }
  156. completion?()
  157. }
  158. )
  159. }
  160. /// Set (or remove) a file as *available offline*. Downloads the file if not downloaded already
  161. static func setAvailableOfflineAction(selectedMetadatas: [tableMetadata], isAnyOffline: Bool, viewController: UIViewController, completion: (() -> Void)? = nil) -> NCMenuAction {
  162. NCMenuAction(
  163. title: isAnyOffline ? NSLocalizedString("_remove_available_offline_", comment: "") : NSLocalizedString("_set_available_offline_", comment: ""),
  164. icon: NCUtility.shared.loadImage(named: "tray.and.arrow.down"),
  165. action: { _ in
  166. if !isAnyOffline, selectedMetadatas.count > 3 {
  167. let alert = UIAlertController(
  168. title: NSLocalizedString("_set_available_offline_", comment: ""),
  169. message: NSLocalizedString("_select_offline_warning_", comment: ""),
  170. preferredStyle: .alert)
  171. alert.addAction(UIAlertAction(title: NSLocalizedString("_continue_", comment: ""), style: .default, handler: { _ in
  172. selectedMetadatas.forEach { NCFunctionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  173. completion?()
  174. }))
  175. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel))
  176. viewController.present(alert, animated: true)
  177. } else {
  178. selectedMetadatas.forEach { NCFunctionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  179. completion?()
  180. }
  181. }
  182. )
  183. }
  184. /// Open view that lets the user move or copy the files within Nextcloud
  185. static func moveOrCopyAction(selectedMetadatas: [tableMetadata], completion: (() -> Void)? = nil) -> NCMenuAction {
  186. NCMenuAction(
  187. title: NSLocalizedString("_move_or_copy_selected_files_", comment: ""),
  188. icon: NCUtility.shared.loadImage(named: "arrow.up.right.square"),
  189. action: { _ in
  190. NCFunctionCenter.shared.openSelectView(items: selectedMetadatas)
  191. completion?()
  192. }
  193. )
  194. }
  195. /// Open AirPrint view to print a single file
  196. static func printAction(metadata: tableMetadata) -> NCMenuAction {
  197. NCMenuAction(
  198. title: NSLocalizedString("_print_", comment: ""),
  199. icon: NCUtility.shared.loadImage(named: "printer"),
  200. action: { _ in
  201. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorPrint)
  202. }
  203. )
  204. }
  205. /// Lock or unlock a file using files_lock
  206. static func lockUnlockFiles(shouldLock: Bool, metadatas: [tableMetadata], completion: (() -> Void)? = nil) -> NCMenuAction {
  207. let titleKey: String
  208. if metadatas.count == 1 {
  209. titleKey = shouldLock ? "_lock_file_" : "_unlock_file_"
  210. } else {
  211. titleKey = shouldLock ? "_lock_selected_files_" : "_unlock_selected_files_"
  212. }
  213. let imageName = !shouldLock ? "lock.open" : "lock"
  214. return NCMenuAction(
  215. title: NSLocalizedString(titleKey, comment: ""),
  216. icon: NCUtility.shared.loadImage(named: imageName),
  217. action: { _ in
  218. for metadata in metadatas where metadata.lock != shouldLock {
  219. NCNetworking.shared.lockUnlockFile(metadata, shoulLock: shouldLock)
  220. }
  221. completion?()
  222. }
  223. )
  224. }
  225. }