NCMenuAction.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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", colors: [NCBrandColor.shared.iconImageColor]),
  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", colors: [NCBrandColor.shared.iconImageColor]),
  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. var color = NCBrandColor.shared.iconImageColor
  98. let permissions = NCPermissions()
  99. if selectedMetadatas.count > 1 {
  100. titleDelete = NSLocalizedString("_delete_selected_files_", comment: "")
  101. destructive = true
  102. } else if let metadata = selectedMetadatas.first {
  103. if NCManageDatabase.shared.isMetadataShareOrMounted(metadata: metadata, metadataFolder: metadataFolder) {
  104. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  105. message = NSLocalizedString("_want_leave_share_", comment: "")
  106. icon = "person.2.slash"
  107. } else if metadata.directory {
  108. titleDelete = NSLocalizedString("_delete_folder_", comment: "")
  109. destructive = true
  110. } else {
  111. titleDelete = NSLocalizedString("_delete_file_", comment: "")
  112. destructive = true
  113. }
  114. if let metadataFolder = metadataFolder {
  115. let isShare = metadata.permissions.contains(permissions.permissionShared) && !metadataFolder.permissions.contains(permissions.permissionShared)
  116. let isMounted = metadata.permissions.contains(permissions.permissionMounted) && !metadataFolder.permissions.contains(permissions.permissionMounted)
  117. if isShare || isMounted {
  118. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  119. icon = "person.2.slash"
  120. }
  121. }
  122. } // else: no metadata selected
  123. let canDeleteServer = selectedMetadatas.allSatisfy { !$0.lock }
  124. var fileList = ""
  125. for (ix, metadata) in selectedMetadatas.enumerated() {
  126. guard ix < 3 else { fileList += "\n - ..."; break }
  127. fileList += "\n - " + metadata.fileNameView
  128. }
  129. if destructive { color = .red }
  130. return NCMenuAction(
  131. title: titleDelete,
  132. destructive: destructive,
  133. icon: NCUtility().loadImage(named: icon, colors: [color]),
  134. order: order,
  135. action: { _ in
  136. let alertController = UIAlertController.deleteFileOrFolder(titleString: titleDelete + "?", message: message + fileList, canDeleteServer: canDeleteServer, selectedMetadatas: selectedMetadatas) { _ in
  137. completion?()
  138. }
  139. viewController.present(alertController, animated: true, completion: nil)
  140. })
  141. }
  142. /// Open "share view" (activity VC) to open files in another app
  143. static func share(selectedMetadatas: [tableMetadata], viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  144. NCMenuAction(
  145. title: NSLocalizedString("_share_", comment: ""),
  146. icon: NCUtility().loadImage(named: "square.and.arrow.up", colors: [NCBrandColor.shared.iconImageColor]),
  147. order: order,
  148. action: { _ in
  149. let controller = viewController.tabBarController as? NCMainTabBarController
  150. NCActionCenter.shared.openActivityViewController(selectedMetadata: selectedMetadatas, controller: controller)
  151. completion?()
  152. }
  153. )
  154. }
  155. /// Set (or remove) a file as *available offline*. Downloads the file if not downloaded already
  156. static func setAvailableOfflineAction(selectedMetadatas: [tableMetadata], isAnyOffline: Bool, viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  157. NCMenuAction(
  158. title: isAnyOffline ? NSLocalizedString("_remove_available_offline_", comment: "") : NSLocalizedString("_set_available_offline_", comment: ""),
  159. icon: NCUtility().loadImage(named: "icloud.and.arrow.down", colors: [NCBrandColor.shared.iconImageColor]),
  160. order: order,
  161. action: { _ in
  162. if !isAnyOffline, selectedMetadatas.count > 3 {
  163. let alert = UIAlertController(
  164. title: NSLocalizedString("_set_available_offline_", comment: ""),
  165. message: NSLocalizedString("_select_offline_warning_", comment: ""),
  166. preferredStyle: .alert)
  167. alert.addAction(UIAlertAction(title: NSLocalizedString("_continue_", comment: ""), style: .default, handler: { _ in
  168. selectedMetadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  169. completion?()
  170. }))
  171. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel))
  172. viewController.present(alert, animated: true)
  173. } else {
  174. selectedMetadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  175. completion?()
  176. }
  177. }
  178. )
  179. }
  180. /// Open view that lets the user move or copy the files within Nextcloud
  181. static func moveOrCopyAction(selectedMetadatas: [tableMetadata], viewController: UIViewController, indexPath: [IndexPath], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  182. NCMenuAction(
  183. title: NSLocalizedString("_move_or_copy_", comment: ""),
  184. icon: NCUtility().loadImage(named: "rectangle.portrait.and.arrow.right", colors: [NCBrandColor.shared.iconImageColor]),
  185. order: order,
  186. action: { _ in
  187. let controller = viewController.tabBarController as? NCMainTabBarController
  188. NCActionCenter.shared.openSelectView(items: selectedMetadatas, controller: controller)
  189. completion?()
  190. }
  191. )
  192. }
  193. /// Lock or unlock a file using *files_lock*
  194. static func lockUnlockFiles(shouldLock: Bool, metadatas: [tableMetadata], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  195. let titleKey: String
  196. if metadatas.count == 1 {
  197. titleKey = shouldLock ? "_lock_file_" : "_unlock_file_"
  198. } else {
  199. titleKey = shouldLock ? "_lock_selected_files_" : "_unlock_selected_files_"
  200. }
  201. let imageName = !shouldLock ? "lock_open" : "lock"
  202. return NCMenuAction(
  203. title: NSLocalizedString(titleKey, comment: ""),
  204. icon: NCUtility().loadImage(named: imageName, colors: [NCBrandColor.shared.iconImageColor]),
  205. order: order,
  206. action: { _ in
  207. for metadata in metadatas where metadata.lock != shouldLock {
  208. NCNetworking.shared.lockUnlockFile(metadata, shoulLock: shouldLock)
  209. }
  210. completion?()
  211. }
  212. )
  213. }
  214. }