NCMenuAction.swift 9.9 KB

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