NCMenuAction.swift 12 KB

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