NCMenuAction.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. return NCMenuAction(
  78. title: titleDelete,
  79. icon: NCUtility.shared.loadImage(named: "trash"),
  80. action: { _ in
  81. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  82. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  83. selectedMetadatas.forEach({ NCOperationQueue.shared.delete(metadata: $0, onlyLocalCache: false) })
  84. completion?()
  85. })
  86. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (_: UIAlertAction) in
  87. selectedMetadatas.forEach({ NCOperationQueue.shared.delete(metadata: $0, onlyLocalCache: true) })
  88. completion?()
  89. })
  90. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (_: UIAlertAction) in })
  91. viewController.present(alertController, animated: true, completion: nil)
  92. }
  93. )
  94. }
  95. /// Open "share view" (activity VC) to open files iin another app
  96. static func openInAction(selectedMetadatas: [tableMetadata], viewController: UIViewController, completion: (() -> Void)? = nil) -> NCMenuAction {
  97. NCMenuAction(
  98. title: NSLocalizedString("_open_in_", comment: ""),
  99. icon: NCUtility.shared.loadImage(named: "square.and.arrow.up"),
  100. action: { _ in
  101. if viewController is NCFileViewInFolder {
  102. viewController.dismiss(animated: true) {
  103. NCFunctionCenter.shared.openActivityViewController(selectedMetadata: selectedMetadatas)
  104. }
  105. } else {
  106. NCFunctionCenter.shared.openActivityViewController(selectedMetadata: selectedMetadatas)
  107. }
  108. completion?()
  109. }
  110. )
  111. }
  112. /// Save selected files to user's prohot library
  113. static func saveMediaAction(selectedMediaMetadatas: [tableMetadata], completion: (() -> Void)? = nil) -> NCMenuAction {
  114. var title: String = NSLocalizedString("_save_selected_files_", comment: "")
  115. var icon = NCUtility.shared.loadImage(named: "square.and.arrow.down")
  116. if selectedMediaMetadatas.allSatisfy({ NCManageDatabase.shared.getMetadataLivePhoto(metadata: $0) != nil }) {
  117. title = NSLocalizedString("_livephoto_save_", comment: "")
  118. icon = NCUtility.shared.loadImage(named: "livephoto")
  119. }
  120. return NCMenuAction(
  121. title: title,
  122. icon: icon,
  123. action: { _ in
  124. for metadata in selectedMediaMetadatas {
  125. if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  126. NCFunctionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV)
  127. } else {
  128. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  129. NCFunctionCenter.shared.saveAlbum(metadata: metadata)
  130. } else {
  131. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  132. }
  133. }
  134. }
  135. completion?()
  136. }
  137. )
  138. }
  139. /// Open view that lets the user move or copy the files within Nextcloud
  140. static func moveOrCopyAction(selectedMetadatas: [tableMetadata], completion: (() -> Void)? = nil) -> NCMenuAction {
  141. NCMenuAction(
  142. title: NSLocalizedString("_move_or_copy_selected_files_", comment: ""),
  143. icon: NCUtility.shared.loadImage(named: "arrow.up.right.square"),
  144. action: { _ in
  145. NCFunctionCenter.shared.openSelectView(items: selectedMetadatas)
  146. completion?()
  147. }
  148. )
  149. }
  150. }