NCMenuAction.swift 11 KB

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