NCSelectableNavigationView.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // NCSelectableNavigationView.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 27.01.22.
  6. // Copyright © 2022 Henrik Storch. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. // Author Henrik Storch <henrik.storch@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 NCCommunication
  26. import Realm
  27. extension RealmSwiftObject {
  28. var primaryKeyValue: String? {
  29. guard let primaryKeyName = self.objectSchema.primaryKeyProperty?.name else { return nil }
  30. return value(forKey: primaryKeyName) as? String
  31. }
  32. }
  33. protocol NCSelectableNavigationView: AnyObject {
  34. var appDelegate: AppDelegate { get }
  35. var selectableDataSource: [RealmSwiftObject] { get }
  36. var collectionView: UICollectionView! { get set }
  37. var isEditMode: Bool { get set }
  38. var selectOcId: [String] { get set }
  39. var titleCurrentFolder: String { get }
  40. var navigationItem: UINavigationItem { get }
  41. func tapSelectMenu()
  42. func tapSelect()
  43. func setNavigationItem()
  44. }
  45. extension NCSelectableNavigationView {
  46. func setNavigationItem() { setNavigationHeader() }
  47. func setNavigationHeader() {
  48. if isEditMode {
  49. navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "navigationMore"), style: .plain, action: tapSelectMenu)
  50. navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: .plain, action: tapSelect)
  51. navigationItem.title = NSLocalizedString("_selected_", comment: "") + " : \(selectOcId.count)" + " / \(selectableDataSource.count)"
  52. } else {
  53. navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_select_", comment: ""), style: UIBarButtonItem.Style.plain, action: tapSelect)
  54. navigationItem.leftBarButtonItem = nil
  55. navigationItem.title = titleCurrentFolder
  56. }
  57. }
  58. func tapSelect() {
  59. isEditMode = !isEditMode
  60. selectOcId.removeAll()
  61. self.setNavigationItem()
  62. self.collectionView.reloadData()
  63. }
  64. func collectionViewSelectAll() {
  65. selectOcId = selectableDataSource.compactMap({ $0.primaryKeyValue })
  66. navigationItem.title = NSLocalizedString("_selected_", comment: "") + " : \(selectOcId.count)" + " / \(selectableDataSource.count)"
  67. collectionView.reloadData()
  68. }
  69. }
  70. extension NCSelectableNavigationView where Self: UIViewController {
  71. func tapSelectMenu() {
  72. var actions = [NCMenuAction]()
  73. //
  74. // SELECT ALL
  75. //
  76. actions.append(
  77. NCMenuAction(
  78. title: NSLocalizedString("_select_all_", comment: ""),
  79. icon: NCUtility.shared.loadImage(named: "checkmark.circle.fill"),
  80. action: { _ in
  81. self.collectionViewSelectAll()
  82. }
  83. )
  84. )
  85. if let trash = self as? NCTrash {
  86. actions.append(
  87. NCMenuAction(
  88. title: NSLocalizedString("_trash_restore_selected_", comment: ""),
  89. icon: NCUtility.shared.loadImage(named: "restore"),
  90. action: { _ in
  91. self.selectOcId.forEach(trash.restoreItem)
  92. self.tapSelect()
  93. }
  94. )
  95. )
  96. actions.append(
  97. NCMenuAction(
  98. title: NSLocalizedString("_trash_delete_selected_", comment: ""),
  99. icon: NCUtility.shared.loadImage(named: "trash"),
  100. action: { _ in
  101. let alert = UIAlertController(title: NSLocalizedString("_trash_delete_selected_", comment: ""), message: "", preferredStyle: .alert)
  102. alert.addAction(UIAlertAction(title: NSLocalizedString("_delete_", comment: ""), style: .destructive, handler: { _ in
  103. self.selectOcId.forEach(trash.deleteItem)
  104. self.tapSelect()
  105. }))
  106. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { _ in }))
  107. self.present(alert, animated: true, completion: nil)
  108. }
  109. )
  110. )
  111. return presentMenu(with: actions)
  112. }
  113. //
  114. // OPEN IN
  115. //
  116. actions.append(
  117. NCMenuAction(
  118. title: NSLocalizedString("_open_in_", comment: ""),
  119. icon: NCUtility.shared.loadImage(named: "square.and.arrow.up"),
  120. action: { _ in
  121. NCFunctionCenter.shared.openActivityViewController(selectOcId: self.selectOcId)
  122. self.tapSelect()
  123. }
  124. )
  125. )
  126. //
  127. // SAVE TO PHOTO GALLERY
  128. //
  129. actions.append(
  130. NCMenuAction(
  131. title: NSLocalizedString("_save_selected_files_", comment: ""),
  132. icon: NCUtility.shared.loadImage(named: "square.and.arrow.down"),
  133. action: { _ in
  134. self.selectOcId
  135. .compactMap(NCManageDatabase.shared.getMetadataFromOcId)
  136. .filter({ $0.classFile == NCCommunicationCommon.typeClassFile.image.rawValue || $0.classFile == NCCommunicationCommon.typeClassFile.video.rawValue })
  137. .forEach { metadata in
  138. if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  139. NCFunctionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV)
  140. } else {
  141. if CCUtility.fileProviderStorageExists(metadata) {
  142. NCFunctionCenter.shared.saveAlbum(metadata: metadata)
  143. } else {
  144. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  145. }
  146. }
  147. }
  148. self.tapSelect()
  149. }
  150. )
  151. )
  152. //
  153. // COPY - MOVE
  154. //
  155. actions.append(
  156. NCMenuAction(
  157. title: NSLocalizedString("_move_or_copy_selected_files_", comment: ""),
  158. icon: NCUtility.shared.loadImage(named: "arrow.up.right.square"),
  159. action: { _ in
  160. let meradatasSelect = self.selectOcId.compactMap(NCManageDatabase.shared.getMetadataFromOcId)
  161. if !meradatasSelect.isEmpty {
  162. NCFunctionCenter.shared.openSelectView(items: meradatasSelect, viewController: self)
  163. }
  164. self.tapSelect()
  165. }
  166. )
  167. )
  168. //
  169. // COPY
  170. //
  171. actions.append(
  172. NCMenuAction(
  173. title: NSLocalizedString("_copy_file_", comment: ""),
  174. icon: NCUtility.shared.loadImage(named: "doc.on.doc"),
  175. action: { _ in
  176. NCFunctionCenter.shared.copyPasteboard(pasteboardOcIds: self.selectOcId, hudView: self.view)
  177. self.tapSelect()
  178. }
  179. )
  180. )
  181. //
  182. // DELETE
  183. //
  184. actions.append(
  185. NCMenuAction(
  186. title: NSLocalizedString("_delete_selected_files_", comment: ""),
  187. icon: NCUtility.shared.loadImage(named: "trash"),
  188. action: { _ in
  189. let meradatasSelect = self.selectOcId.compactMap(NCManageDatabase.shared.getMetadataFromOcId)
  190. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  191. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  192. meradatasSelect.forEach({ NCOperationQueue.shared.delete(metadata: $0, onlyLocalCache: false) })
  193. self.tapSelect()
  194. })
  195. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (_: UIAlertAction) in
  196. meradatasSelect.forEach({ NCOperationQueue.shared.delete(metadata: $0, onlyLocalCache: true) })
  197. self.tapSelect()
  198. })
  199. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (_: UIAlertAction) in })
  200. self.present(alertController, animated: true, completion: nil)
  201. }
  202. )
  203. )
  204. presentMenu(with: actions)
  205. }
  206. }