NCMenuAction.swift 11 KB

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