NCMenuAction.swift 11 KB

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