NCSelectableNavigationView.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. var selectActions: [NCMenuAction] { get }
  42. func reloadDataSource()
  43. func setNavigationItem()
  44. func tapSelectMenu()
  45. func tapSelect()
  46. }
  47. extension NCSelectableNavigationView {
  48. func setNavigationItem() { setNavigationHeader() }
  49. func setNavigationHeader() {
  50. if isEditMode {
  51. navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "navigationMore"), style: .plain, action: tapSelectMenu)
  52. navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: .plain, action: tapSelect)
  53. navigationItem.title = NSLocalizedString("_selected_", comment: "") + " : \(selectOcId.count)" + " / \(selectableDataSource.count)"
  54. } else {
  55. navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_select_", comment: ""), style: UIBarButtonItem.Style.plain, action: tapSelect)
  56. navigationItem.leftBarButtonItem = nil
  57. navigationItem.title = titleCurrentFolder
  58. }
  59. }
  60. func tapSelect() {
  61. isEditMode = !isEditMode
  62. selectOcId.removeAll()
  63. self.setNavigationItem()
  64. self.collectionView.reloadData()
  65. }
  66. func collectionViewSelectAll() {
  67. selectOcId = selectableDataSource.compactMap({ $0.primaryKeyValue })
  68. navigationItem.title = NSLocalizedString("_selected_", comment: "") + " : \(selectOcId.count)" + " / \(selectableDataSource.count)"
  69. collectionView.reloadData()
  70. }
  71. }
  72. extension NCSelectableNavigationView where Self: UIViewController {
  73. func tapSelectMenu() {
  74. presentMenu(with: selectActions)
  75. }
  76. var selectActions: [NCMenuAction] {
  77. var actions = [NCMenuAction]()
  78. if selectOcId.count != selectableDataSource.count {
  79. actions.append(.selectAllAction(action: collectionViewSelectAll))
  80. }
  81. guard !selectOcId.isEmpty else { return actions }
  82. var selectedMetadatas: [tableMetadata] = []
  83. var selectedMediaMetadatas: [tableMetadata] = []
  84. var isAnyOffline = false
  85. for ocId in selectOcId {
  86. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { continue }
  87. selectedMetadatas.append(metadata)
  88. if [NCCommunicationCommon.typeClassFile.image.rawValue, NCCommunicationCommon.typeClassFile.video.rawValue].contains(metadata.classFile) {
  89. selectedMediaMetadatas.append(metadata)
  90. }
  91. guard !isAnyOffline else { continue }
  92. if metadata.directory,
  93. let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, metadata.serverUrl + "/" + metadata.fileName)) {
  94. isAnyOffline = directory.offline
  95. } else if let localFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  96. isAnyOffline = localFile.offline
  97. } // else: file is not offline, continue
  98. }
  99. actions.append(.openInAction(selectedMetadatas: selectedMetadatas, viewController: self, completion: tapSelect))
  100. if !selectedMediaMetadatas.isEmpty {
  101. actions.append(.saveMediaAction(selectedMediaMetadatas: selectedMediaMetadatas, completion: tapSelect))
  102. }
  103. actions.append(.setAvailableOfflineAction(selectedMetadatas: selectedMetadatas, isAnyOffline: isAnyOffline, viewController: self, completion: {
  104. self.reloadDataSource()
  105. self.tapSelect()
  106. }))
  107. actions.append(.moveOrCopyAction(selectedMetadatas: selectedMetadatas, completion: tapSelect))
  108. actions.append(.copyAction(selectOcId: selectOcId, hudView: self.view, completion: tapSelect))
  109. actions.append(.deleteAction(selectedMetadatas: selectedMetadatas, viewController: self, completion: tapSelect))
  110. return actions
  111. }
  112. }