NCSelectableNavigationView.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 NextcloudKit
  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 selectIndexPath: [IndexPath] { get set }
  40. var titleCurrentFolder: String { get }
  41. var navigationItem: UINavigationItem { get }
  42. var navigationController: UINavigationController? { get }
  43. var layoutKey: String { get }
  44. var selectActions: [NCMenuAction] { get }
  45. func reloadDataSource(isForced: Bool)
  46. func setNavigationItem()
  47. func tapSelectMenu()
  48. func tapSelect()
  49. }
  50. extension NCSelectableNavigationView {
  51. func setNavigationItem() {
  52. setNavigationRightItems()
  53. }
  54. func setNavigationRightItems() {
  55. if isEditMode {
  56. let more = UIBarButtonItem(image: UIImage(systemName: "ellipsis"), style: .plain, action: tapSelectMenu)
  57. navigationItem.rightBarButtonItems = [more]
  58. } else {
  59. let select = UIBarButtonItem(title: NSLocalizedString("_select_", comment: ""), style: UIBarButtonItem.Style.plain, action: tapSelect)
  60. let notification = UIBarButtonItem(image: UIImage(systemName: "bell"), style: .plain, action: tapNotification)
  61. if layoutKey == NCGlobal.shared.layoutViewFiles {
  62. navigationItem.rightBarButtonItems = [select, notification]
  63. } else {
  64. navigationItem.rightBarButtonItems = [select]
  65. }
  66. }
  67. }
  68. func tapSelect() {
  69. isEditMode = !isEditMode
  70. selectOcId.removeAll()
  71. selectIndexPath.removeAll()
  72. self.setNavigationItem()
  73. self.collectionView.reloadData()
  74. }
  75. func collectionViewSelectAll() {
  76. selectOcId = selectableDataSource.compactMap({ $0.primaryKeyValue })
  77. collectionView.reloadData()
  78. }
  79. func tapNotification() {
  80. if let viewController = UIStoryboard(name: "NCNotification", bundle: nil).instantiateInitialViewController() as? NCNotification {
  81. navigationController?.pushViewController(viewController, animated: true)
  82. }
  83. }
  84. }
  85. extension NCSelectableNavigationView where Self: UIViewController {
  86. func tapSelectMenu() {
  87. presentMenu(with: selectActions)
  88. }
  89. var selectActions: [NCMenuAction] {
  90. var actions = [NCMenuAction]()
  91. actions.append(.cancelAction {
  92. self.tapSelect()
  93. })
  94. if selectOcId.count != selectableDataSource.count {
  95. actions.append(.selectAllAction(action: collectionViewSelectAll))
  96. }
  97. guard !selectOcId.isEmpty else { return actions }
  98. actions.append(.seperator(order: 0))
  99. var selectedMetadatas: [tableMetadata] = []
  100. var selectedMediaMetadatas: [tableMetadata] = []
  101. var isAnyOffline = false
  102. var isAnyFolder = false
  103. var isAnyLocked = false
  104. var canUnlock = true
  105. for ocId in selectOcId {
  106. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { continue }
  107. selectedMetadatas.append(metadata)
  108. if [NKCommon.TypeClassFile.image.rawValue, NKCommon.TypeClassFile.video.rawValue].contains(metadata.classFile) {
  109. selectedMediaMetadatas.append(metadata)
  110. }
  111. if metadata.directory { isAnyFolder = true }
  112. if metadata.lock {
  113. isAnyLocked = true
  114. if metadata.lockOwner != appDelegate.userId {
  115. canUnlock = false
  116. }
  117. }
  118. guard !isAnyOffline else { continue }
  119. if metadata.directory,
  120. let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, metadata.serverUrl + "/" + metadata.fileName)) {
  121. isAnyOffline = directory.offline
  122. } else if let localFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  123. isAnyOffline = localFile.offline
  124. } // else: file is not offline, continue
  125. }
  126. actions.append(.openInAction(selectedMetadatas: selectedMetadatas, viewController: self, completion: tapSelect))
  127. if !isAnyFolder, canUnlock, !NCGlobal.shared.capabilityFilesLockVersion.isEmpty {
  128. actions.append(.lockUnlockFiles(shouldLock: !isAnyLocked, metadatas: selectedMetadatas, completion: tapSelect))
  129. }
  130. if !selectedMediaMetadatas.isEmpty {
  131. actions.append(.saveMediaAction(selectedMediaMetadatas: selectedMediaMetadatas, completion: tapSelect))
  132. }
  133. actions.append(.setAvailableOfflineAction(selectedMetadatas: selectedMetadatas, isAnyOffline: isAnyOffline, viewController: self, completion: {
  134. self.reloadDataSource(isForced: true)
  135. self.tapSelect()
  136. }))
  137. actions.append(.moveOrCopyAction(selectedMetadatas: selectedMetadatas, indexPath: selectIndexPath, completion: tapSelect))
  138. actions.append(.copyAction(selectOcId: selectOcId, hudView: self.view, completion: tapSelect))
  139. actions.append(.deleteAction(selectedMetadatas: selectedMetadatas, indexPath: selectIndexPath, viewController: self, completion: tapSelect))
  140. return actions
  141. }
  142. }