NCMenuAction.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. class NCMenuAction {
  10. let title: String
  11. let icon: UIImage
  12. let selectable: Bool
  13. var onTitle: String?
  14. var onIcon: UIImage?
  15. var selected: Bool = false
  16. var isOn: Bool = false
  17. var action: ((_ menuAction: NCMenuAction) -> Void)?
  18. init(title: String, icon: UIImage, action: ((_ menuAction: NCMenuAction) -> Void)?) {
  19. self.title = title
  20. self.icon = icon
  21. self.action = action
  22. self.selectable = false
  23. }
  24. init(title: String, icon: UIImage, onTitle: String? = nil, onIcon: UIImage? = nil, selected: Bool, on: Bool, action: ((_ menuAction: NCMenuAction) -> Void)?) {
  25. self.title = title
  26. self.icon = icon
  27. self.onTitle = onTitle ?? title
  28. self.onIcon = onIcon ?? icon
  29. self.action = action
  30. self.selected = selected
  31. self.isOn = on
  32. self.selectable = true
  33. }
  34. }
  35. // MARK: - Actons
  36. extension NCMenuAction {
  37. /// Select all items
  38. static func selectAllAction(action: @escaping () -> Void) -> NCMenuAction {
  39. NCMenuAction(
  40. title: NSLocalizedString("_select_all_", comment: ""),
  41. icon: NCUtility.shared.loadImage(named: "checkmark.circle.fill"),
  42. action: { _ in action() }
  43. )
  44. }
  45. /// Copy files to pasteboard
  46. static func copyAction(selectOcId: [String], completion: (() -> Void)? = nil) -> NCMenuAction {
  47. NCMenuAction(
  48. title: NSLocalizedString("_copy_file_", comment: ""),
  49. icon: NCUtility.shared.loadImage(named: "doc.on.doc"),
  50. action: { _ in
  51. NCFunctionCenter.shared.copyPasteboard(pasteboardOcIds: selectOcId)
  52. completion?()
  53. }
  54. )
  55. }
  56. /// Delete files either from cache or from Nextcloud
  57. static func deleteAction(selectedMetadatas: [tableMetadata], metadataFolder: tableMetadata? = nil, viewController: UIViewController, completion: (() -> Void)? = nil) -> NCMenuAction {
  58. var titleDelete = NSLocalizedString("_delete_", comment: "")
  59. if selectedMetadatas.count > 1 {
  60. titleDelete = NSLocalizedString("_delete_selected_files_", comment: "")
  61. } else if let metadata = selectedMetadatas.first {
  62. if NCManageDatabase.shared.isMetadataShareOrMounted(metadata: metadata, metadataFolder: metadataFolder) {
  63. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  64. } else if metadata.directory {
  65. titleDelete = NSLocalizedString("_delete_folder_", comment: "")
  66. } else {
  67. titleDelete = NSLocalizedString("_delete_file_", comment: "")
  68. }
  69. if let metadataFolder = metadataFolder {
  70. let isShare = metadata.permissions.contains(NCGlobal.shared.permissionShared) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionShared)
  71. let isMounted = metadata.permissions.contains(NCGlobal.shared.permissionMounted) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionMounted)
  72. if isShare || isMounted {
  73. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  74. }
  75. }
  76. } // else: no metadata selected
  77. var fileList = ""
  78. for (ix, metadata) in selectedMetadatas.enumerated() {
  79. guard ix < 3 else { fileList += "\n - ..."; break }
  80. fileList += "\n - " + metadata.fileName
  81. }
  82. return NCMenuAction(
  83. title: titleDelete,
  84. icon: NCUtility.shared.loadImage(named: "trash"),
  85. action: { _ in
  86. let alertController = UIAlertController(
  87. title: titleDelete,
  88. message: NSLocalizedString("_want_delete_", comment: "") + fileList,
  89. preferredStyle: .alert)
  90. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  91. selectedMetadatas.forEach({ NCOperationQueue.shared.delete(metadata: $0, onlyLocalCache: false) })
  92. completion?()
  93. })
  94. // NCMedia removes image from collection view if removed from cache
  95. if !(viewController is NCMedia) {
  96. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (_: UIAlertAction) in
  97. selectedMetadatas.forEach({ NCOperationQueue.shared.delete(metadata: $0, onlyLocalCache: true) })
  98. completion?()
  99. })
  100. }
  101. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (_: UIAlertAction) in })
  102. viewController.present(alertController, animated: true, completion: nil)
  103. }
  104. )
  105. }
  106. /// Open "share view" (activity VC) to open files iin another app
  107. static func openInAction(selectedMetadatas: [tableMetadata], viewController: UIViewController, completion: (() -> Void)? = nil) -> NCMenuAction {
  108. NCMenuAction(
  109. title: NSLocalizedString("_open_in_", comment: ""),
  110. icon: NCUtility.shared.loadImage(named: "square.and.arrow.up"),
  111. action: { _ in
  112. if viewController is NCFileViewInFolder {
  113. viewController.dismiss(animated: true) {
  114. NCFunctionCenter.shared.openActivityViewController(selectedMetadata: selectedMetadatas)
  115. }
  116. } else {
  117. NCFunctionCenter.shared.openActivityViewController(selectedMetadata: selectedMetadatas)
  118. }
  119. completion?()
  120. }
  121. )
  122. }
  123. /// Save selected files to user's prohot library
  124. static func saveMediaAction(selectedMediaMetadatas: [tableMetadata], completion: (() -> Void)? = nil) -> NCMenuAction {
  125. var title: String = NSLocalizedString("_save_selected_files_", comment: "")
  126. var icon = NCUtility.shared.loadImage(named: "square.and.arrow.down")
  127. if selectedMediaMetadatas.allSatisfy({ NCManageDatabase.shared.getMetadataLivePhoto(metadata: $0) != nil }) {
  128. title = NSLocalizedString("_livephoto_save_", comment: "")
  129. icon = NCUtility.shared.loadImage(named: "livephoto")
  130. }
  131. return NCMenuAction(
  132. title: title,
  133. icon: icon,
  134. action: { _ in
  135. for metadata in selectedMediaMetadatas {
  136. if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  137. NCFunctionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV)
  138. } else {
  139. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  140. NCFunctionCenter.shared.saveAlbum(metadata: metadata)
  141. } else {
  142. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  143. }
  144. }
  145. }
  146. completion?()
  147. }
  148. )
  149. }
  150. /// Open view that lets the user move or copy the files within Nextcloud
  151. static func moveOrCopyAction(selectedMetadatas: [tableMetadata], completion: (() -> Void)? = nil) -> NCMenuAction {
  152. NCMenuAction(
  153. title: NSLocalizedString("_move_or_copy_selected_files_", comment: ""),
  154. icon: NCUtility.shared.loadImage(named: "arrow.up.right.square"),
  155. action: { _ in
  156. NCFunctionCenter.shared.openSelectView(items: selectedMetadatas)
  157. completion?()
  158. }
  159. )
  160. }
  161. }