NCMenuAction.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. import NextcloudKit
  27. import JGProgressHUD
  28. class NCMenuAction {
  29. let title: String
  30. let boldTitle: Bool
  31. let details: String?
  32. let icon: UIImage
  33. let selectable: Bool
  34. var onTitle: String?
  35. var onIcon: UIImage?
  36. let destructive: Bool
  37. var selected: Bool = false
  38. var isOn: Bool = false
  39. var action: ((_ menuAction: NCMenuAction) -> Void)?
  40. var rowHeight: CGFloat { self.title == NCMenuAction.seperatorIdentifier ? NCMenuAction.seperatorHeight : self.details != nil ? 76 : 56 }
  41. var order: Int = 0
  42. init(title: String, boldTitle: Bool = false, destructive: Bool = false, details: String? = nil, icon: UIImage, order: Int = 0, action: ((_ menuAction: NCMenuAction) -> Void)?) {
  43. self.title = title
  44. self.boldTitle = boldTitle
  45. self.destructive = destructive
  46. self.details = details
  47. self.icon = icon
  48. self.action = action
  49. self.selectable = false
  50. self.order = order
  51. }
  52. init(title: String, boldTitle: Bool = false, destructive: Bool = false, details: String? = nil, icon: UIImage, onTitle: String? = nil, onIcon: UIImage? = nil, selected: Bool, on: Bool, order: Int = 0, action: ((_ menuAction: NCMenuAction) -> Void)?) {
  53. self.title = title
  54. self.boldTitle = boldTitle
  55. self.destructive = destructive
  56. self.details = details
  57. self.icon = icon
  58. self.onTitle = onTitle ?? title
  59. self.onIcon = onIcon ?? icon
  60. self.action = action
  61. self.selected = selected
  62. self.isOn = on
  63. self.selectable = true
  64. self.order = order
  65. }
  66. }
  67. // MARK: - Actions
  68. extension NCMenuAction {
  69. static let seperatorIdentifier = "NCMenuAction.SEPARATOR"
  70. static let seperatorHeight: CGFloat = 0.5
  71. /// A static seperator, with no actions, text, or icons
  72. static func seperator(order: Int = 0) -> NCMenuAction {
  73. return NCMenuAction(title: seperatorIdentifier, icon: UIImage(), order: order, action: nil)
  74. }
  75. /// Select all items
  76. static func selectAllAction(action: @escaping () -> Void) -> NCMenuAction {
  77. NCMenuAction(
  78. title: NSLocalizedString("_select_all_", comment: ""),
  79. icon: NCUtility().loadImage(named: "checkmark.circle.fill"),
  80. action: { _ in action() }
  81. )
  82. }
  83. /// Cancel
  84. static func cancelAction(action: @escaping () -> Void) -> NCMenuAction {
  85. NCMenuAction(
  86. title: NSLocalizedString("_cancel_", comment: ""),
  87. icon: NCUtility().loadImage(named: "xmark"),
  88. action: { _ in action() }
  89. )
  90. }
  91. /// Delete files either from cache or from Nextcloud
  92. static func deleteAction(selectedMetadatas: [tableMetadata], indexPaths: [IndexPath], metadataFolder: tableMetadata? = nil, viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  93. var titleDelete = NSLocalizedString("_delete_", comment: "")
  94. var message = NSLocalizedString("_want_delete_", comment: "")
  95. var icon = "trash"
  96. var destructive = false
  97. if selectedMetadatas.count > 1 {
  98. titleDelete = NSLocalizedString("_delete_selected_files_", comment: "")
  99. destructive = true
  100. } else if let metadata = selectedMetadatas.first {
  101. if NCManageDatabase.shared.isMetadataShareOrMounted(metadata: metadata, metadataFolder: metadataFolder) {
  102. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  103. message = NSLocalizedString("_want_leave_share_", comment: "")
  104. icon = "person.2.slash"
  105. } else if metadata.directory {
  106. titleDelete = NSLocalizedString("_delete_folder_", comment: "")
  107. destructive = true
  108. } else {
  109. titleDelete = NSLocalizedString("_delete_file_", comment: "")
  110. destructive = true
  111. }
  112. if let metadataFolder = metadataFolder {
  113. let isShare = metadata.permissions.contains(NCGlobal.shared.permissionShared) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionShared)
  114. let isMounted = metadata.permissions.contains(NCGlobal.shared.permissionMounted) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionMounted)
  115. if isShare || isMounted {
  116. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  117. icon = "person.2.slash"
  118. }
  119. }
  120. } // else: no metadata selected
  121. let canDeleteServer = selectedMetadatas.allSatisfy { !$0.lock }
  122. var fileList = ""
  123. for (ix, metadata) in selectedMetadatas.enumerated() {
  124. guard ix < 3 else { fileList += "\n - ..."; break }
  125. fileList += "\n - " + metadata.fileNameView
  126. }
  127. return NCMenuAction(
  128. title: titleDelete,
  129. destructive: destructive,
  130. icon: NCUtility().loadImage(named: icon),
  131. order: order,
  132. action: { _ in
  133. let alertController = UIAlertController.deleteFileOrFolder(titleString: titleDelete + "?", message: message + fileList, canDeleteServer: canDeleteServer, selectedMetadatas: selectedMetadatas) { _ in
  134. completion?()
  135. }
  136. viewController.present(alertController, animated: true, completion: nil)
  137. })
  138. }
  139. /// Open "share view" (activity VC) to open files in another app
  140. static func share(selectedMetadatas: [tableMetadata], viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  141. NCMenuAction(
  142. title: NSLocalizedString("_share_", comment: ""),
  143. icon: NCUtility().loadImage(named: "square.and.arrow.up"),
  144. order: order,
  145. action: { _ in
  146. NCActionCenter.shared.openActivityViewController(selectedMetadata: selectedMetadatas)
  147. completion?()
  148. }
  149. )
  150. }
  151. /// Save selected files to user's photo library
  152. static func saveMediaAction(selectedMediaMetadatas: [tableMetadata], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  153. var title: String = NSLocalizedString("_save_selected_files_", comment: "")
  154. var icon = NCUtility().loadImage(named: "square.and.arrow.down")
  155. if selectedMediaMetadatas.allSatisfy({ NCManageDatabase.shared.getMetadataLivePhoto(metadata: $0) != nil }) {
  156. title = NSLocalizedString("_livephoto_save_", comment: "")
  157. icon = NCUtility().loadImage(named: "livephoto")
  158. }
  159. return NCMenuAction(
  160. title: title,
  161. icon: icon,
  162. order: order,
  163. action: { _ in
  164. for metadata in selectedMediaMetadatas {
  165. if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  166. NCNetworking.shared.saveLivePhotoQueue.addOperation(NCOperationSaveLivePhoto(metadata: metadata, metadataMOV: metadataMOV))
  167. } else {
  168. if NCUtilityFileSystem().fileProviderStorageExists(metadata) {
  169. NCActionCenter.shared.saveAlbum(metadata: metadata)
  170. } else {
  171. if NCNetworking.shared.downloadQueue.operations.filter({ ($0 as? NCOperationDownload)?.metadata.ocId == metadata.ocId }).isEmpty {
  172. NCNetworking.shared.downloadQueue.addOperation(NCOperationDownload(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum))
  173. }
  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().loadImage(named: "icloud.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 { NCActionCenter.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 { NCActionCenter.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], indexPath: [IndexPath], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  208. NCMenuAction(
  209. title: NSLocalizedString("_move_or_copy_", comment: ""),
  210. icon: NCUtility().loadImage(named: "rectangle.portrait.and.arrow.right"),
  211. order: order,
  212. action: { _ in
  213. NCActionCenter.shared.openSelectView(items: selectedMetadatas)
  214. completion?()
  215. }
  216. )
  217. }
  218. /// Lock or unlock a file using *files_lock*
  219. static func lockUnlockFiles(shouldLock: Bool, metadatas: [tableMetadata], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  220. let titleKey: String
  221. if metadatas.count == 1 {
  222. titleKey = shouldLock ? "_lock_file_" : "_unlock_file_"
  223. } else {
  224. titleKey = shouldLock ? "_lock_selected_files_" : "_unlock_selected_files_"
  225. }
  226. let imageName = !shouldLock ? "lock_open" : "lock"
  227. return NCMenuAction(
  228. title: NSLocalizedString(titleKey, comment: ""),
  229. icon: NCUtility().loadImage(named: imageName),
  230. order: order,
  231. action: { _ in
  232. for metadata in metadatas where metadata.lock != shouldLock {
  233. NCNetworking.shared.lockUnlockFile(metadata, shoulLock: shouldLock)
  234. }
  235. completion?()
  236. }
  237. )
  238. }
  239. }