NCSelectableNavigationView.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 NCCommunication
  25. import Realm
  26. import UIKit
  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. var selectActions: [NCMenuAction] { get }
  45. }
  46. extension NCSelectableNavigationView {
  47. func setNavigationItem() { setNavigationHeader() }
  48. func setNavigationHeader() {
  49. if isEditMode {
  50. navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "navigationMore"), style: .plain, action: tapSelectMenu)
  51. navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: .plain, action: tapSelect)
  52. navigationItem.title = NSLocalizedString("_selected_", comment: "") + " : \(selectOcId.count)" + " / \(selectableDataSource.count)"
  53. } else {
  54. navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_select_", comment: ""), style: UIBarButtonItem.Style.plain, action: tapSelect)
  55. navigationItem.leftBarButtonItem = nil
  56. navigationItem.title = titleCurrentFolder
  57. }
  58. }
  59. func tapSelect() {
  60. isEditMode = !isEditMode
  61. selectOcId.removeAll()
  62. self.setNavigationItem()
  63. self.collectionView.reloadData()
  64. }
  65. func collectionViewSelectAll() {
  66. selectOcId = selectableDataSource.compactMap({ $0.primaryKeyValue })
  67. navigationItem.title = NSLocalizedString("_selected_", comment: "") + " : \(selectOcId.count)" + " / \(selectableDataSource.count)"
  68. collectionView.reloadData()
  69. }
  70. }
  71. extension NCSelectableNavigationView where Self: UIViewController {
  72. func tapSelectMenu() {
  73. presentMenu(with: selectActions)
  74. }
  75. var selectActions: [NCMenuAction] {
  76. var actions = [NCMenuAction]()
  77. if selectOcId.count != selectableDataSource.count {
  78. actions.append(.selectAllAction(action: collectionViewSelectAll))
  79. }
  80. guard !selectOcId.isEmpty else { return actions }
  81. var selectedMetadatas: [tableMetadata] = []
  82. var selectedMediaMetadatas: [tableMetadata] = []
  83. for ocId in selectOcId {
  84. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { continue }
  85. selectedMetadatas.append(metadata)
  86. if [NCCommunicationCommon.typeClassFile.image.rawValue, NCCommunicationCommon.typeClassFile.video.rawValue].contains(metadata.classFile) {
  87. selectedMediaMetadatas.append(metadata)
  88. }
  89. }
  90. actions.append(.openInAction(selectedMetadatas: selectedMetadatas, viewController: self, completion: tapSelect))
  91. if !selectedMediaMetadatas.isEmpty {
  92. actions.append(.saveMediaAction(selectedMediaMetadatas: selectedMediaMetadatas, completion: tapSelect))
  93. }
  94. actions.append(.moveOrCopyAction(selectedMetadatas: selectedMetadatas, completion: tapSelect))
  95. actions.append(.copyAction(selectOcId: selectOcId, hudView: self.view, completion: tapSelect))
  96. actions.append(.deleteAction(selectedMetadatas: selectedMetadatas, viewController: self, completion: tapSelect))
  97. return actions
  98. }
  99. }