NCMenuAction.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. var fileList = ""
  84. for (ix, metadata) in selectedMetadatas.enumerated() {
  85. guard ix < 3 else { fileList += "\n - ..."; break }
  86. fileList += "\n - " + metadata.fileName
  87. }
  88. return NCMenuAction(
  89. title: titleDelete,
  90. icon: NCUtility.shared.loadImage(named: "trash"),
  91. action: { _ in
  92. let alertController = UIAlertController(
  93. title: titleDelete,
  94. message: NSLocalizedString("_want_delete_", comment: "") + fileList,
  95. preferredStyle: .alert)
  96. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  97. selectedMetadatas.forEach({ NCOperationQueue.shared.delete(metadata: $0, onlyLocalCache: false) })
  98. completion?()
  99. })
  100. // NCMedia removes image from collection view if removed from cache
  101. if !(viewController is NCMedia) {
  102. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (_: UIAlertAction) in
  103. selectedMetadatas.forEach({ NCOperationQueue.shared.delete(metadata: $0, onlyLocalCache: true) })
  104. completion?()
  105. })
  106. }
  107. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (_: UIAlertAction) in })
  108. viewController.present(alertController, animated: true, completion: nil)
  109. }
  110. )
  111. }
  112. /// Open "share view" (activity VC) to open files in another app
  113. static func openInAction(selectedMetadatas: [tableMetadata], viewController: UIViewController, completion: (() -> Void)? = nil) -> NCMenuAction {
  114. NCMenuAction(
  115. title: NSLocalizedString("_open_in_", comment: ""),
  116. icon: NCUtility.shared.loadImage(named: "square.and.arrow.up"),
  117. action: { _ in
  118. if viewController is NCFileViewInFolder {
  119. viewController.dismiss(animated: true) {
  120. NCFunctionCenter.shared.openActivityViewController(selectedMetadata: selectedMetadatas)
  121. }
  122. } else {
  123. NCFunctionCenter.shared.openActivityViewController(selectedMetadata: selectedMetadatas)
  124. }
  125. completion?()
  126. }
  127. )
  128. }
  129. /// Save selected files to user's photo library
  130. static func saveMediaAction(selectedMediaMetadatas: [tableMetadata], completion: (() -> Void)? = nil) -> NCMenuAction {
  131. var title: String = NSLocalizedString("_save_selected_files_", comment: "")
  132. var icon = NCUtility.shared.loadImage(named: "square.and.arrow.down")
  133. if selectedMediaMetadatas.allSatisfy({ NCManageDatabase.shared.getMetadataLivePhoto(metadata: $0) != nil }) {
  134. title = NSLocalizedString("_livephoto_save_", comment: "")
  135. icon = NCUtility.shared.loadImage(named: "livephoto")
  136. }
  137. return NCMenuAction(
  138. title: title,
  139. icon: icon,
  140. action: { _ in
  141. for metadata in selectedMediaMetadatas {
  142. if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  143. NCFunctionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV)
  144. } else {
  145. if CCUtility.fileProviderStorageExists(metadata) {
  146. NCFunctionCenter.shared.saveAlbum(metadata: metadata)
  147. } else {
  148. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  149. }
  150. }
  151. }
  152. completion?()
  153. }
  154. )
  155. }
  156. /// Set (or remove) a file as *available offline*. Downloads the file if not downloaded already
  157. static func setAvailableOfflineAction(selectedMetadatas: [tableMetadata], isAnyOffline: Bool, viewController: UIViewController, completion: (() -> Void)? = nil) -> NCMenuAction {
  158. NCMenuAction(
  159. title: isAnyOffline ? NSLocalizedString("_remove_available_offline_", comment: "") : NSLocalizedString("_set_available_offline_", comment: ""),
  160. icon: NCUtility.shared.loadImage(named: "tray.and.arrow.down"),
  161. action: { _ in
  162. if !isAnyOffline, selectedMetadatas.count > 3 {
  163. let alert = UIAlertController(
  164. title: NSLocalizedString("_set_available_offline_", comment: ""),
  165. message: NSLocalizedString("_select_offline_warning_", comment: ""),
  166. preferredStyle: .alert)
  167. alert.addAction(UIAlertAction(title: NSLocalizedString("_continue_", comment: ""), style: .default, handler: { _ in
  168. selectedMetadatas.forEach { NCFunctionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  169. completion?()
  170. }))
  171. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel))
  172. viewController.present(alert, animated: true)
  173. } else {
  174. selectedMetadatas.forEach { NCFunctionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  175. completion?()
  176. }
  177. }
  178. )
  179. }
  180. /// Open view that lets the user move or copy the files within Nextcloud
  181. static func moveOrCopyAction(selectedMetadatas: [tableMetadata], completion: (() -> Void)? = nil) -> NCMenuAction {
  182. NCMenuAction(
  183. title: NSLocalizedString("_move_or_copy_selected_files_", comment: ""),
  184. icon: NCUtility.shared.loadImage(named: "arrow.up.right.square"),
  185. action: { _ in
  186. NCFunctionCenter.shared.openSelectView(items: selectedMetadatas)
  187. completion?()
  188. }
  189. )
  190. }
  191. /// Open AirPrint view to print a single file
  192. static func printAction(metadata: tableMetadata) -> NCMenuAction {
  193. NCMenuAction(
  194. title: NSLocalizedString("_print_", comment: ""),
  195. icon: NCUtility.shared.loadImage(named: "printer"),
  196. action: { _ in
  197. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorPrint)
  198. }
  199. )
  200. }
  201. /// Lock or unlock a file using files_lock
  202. static func lockUnlockFiles(shouldLock: Bool, metadatas: [tableMetadata], completion: (() -> Void)? = nil) -> NCMenuAction {
  203. let titleKey: String
  204. if metadatas.count == 1 {
  205. titleKey = shouldLock ? "_lock_file_" : "_unlock_file_"
  206. } else {
  207. titleKey = shouldLock ? "_lock_selected_files_" : "_unlock_selected_files_"
  208. }
  209. let imageName = !shouldLock ? "lock.open" : "lock"
  210. return NCMenuAction(
  211. title: NSLocalizedString(titleKey, comment: ""),
  212. icon: NCUtility.shared.loadImage(named: imageName),
  213. action: { _ in
  214. for metadata in metadatas where metadata.lock != shouldLock {
  215. NCNetworking.shared.lockUnlockFile(metadata, shoulLock: shouldLock)
  216. }
  217. completion?()
  218. }
  219. )
  220. }
  221. }