NCSelectableNavigationView.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. public protocol NCSelectableViewTabBar {
  34. var tabBarController: UITabBarController? { get }
  35. var hostingController: UIViewController? { get }
  36. }
  37. protocol NCSelectableNavigationView: AnyObject {
  38. var viewController: UIViewController { get }
  39. var appDelegate: AppDelegate { get }
  40. var selectableDataSource: [RealmSwiftObject] { get }
  41. var collectionView: UICollectionView! { get set }
  42. var isEditMode: Bool { get set }
  43. var selectOcId: [String] { get set }
  44. var selectIndexPath: [IndexPath] { get set }
  45. var titleCurrentFolder: String { get }
  46. var navigationItem: UINavigationItem { get }
  47. var navigationController: UINavigationController? { get }
  48. var layoutKey: String { get }
  49. var serverUrl: String { get }
  50. var tabBarSelect: NCSelectableViewTabBar? { get set }
  51. func reloadDataSource(withQueryDB: Bool)
  52. func setNavigationLeftItems()
  53. func setNavigationRightItems(enableMoreMenu: Bool)
  54. func createMenuActions() -> [UIMenuElement]
  55. func toggleSelect(isOn: Bool?)
  56. func onListSelected()
  57. func onGridSelected()
  58. }
  59. extension NCSelectableNavigationView {
  60. func setNavigationLeftItems() {}
  61. func saveLayout(_ layoutForView: NCDBLayoutForView) {
  62. NCManageDatabase.shared.setLayoutForView(layoutForView: layoutForView)
  63. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource)
  64. setNavigationRightItems(enableMoreMenu: true)
  65. }
  66. /// If explicit `isOn` is not set, it will invert `isEditMode`
  67. func toggleSelect(isOn: Bool? = nil) {
  68. DispatchQueue.main.async {
  69. self.isEditMode = isOn ?? !self.isEditMode
  70. self.selectOcId.removeAll()
  71. self.selectIndexPath.removeAll()
  72. self.setNavigationLeftItems()
  73. self.setNavigationRightItems(enableMoreMenu: true)
  74. self.collectionView.reloadData()
  75. }
  76. }
  77. func collectionViewSelectAll() {
  78. selectOcId = selectableDataSource.compactMap({ $0.primaryKeyValue })
  79. collectionView.reloadData()
  80. setNavigationRightItems(enableMoreMenu: true)
  81. }
  82. func tapNotification() {
  83. if let viewController = UIStoryboard(name: "NCNotification", bundle: nil).instantiateInitialViewController() as? NCNotification {
  84. navigationController?.pushViewController(viewController, animated: true)
  85. }
  86. }
  87. }
  88. extension NCSelectableNavigationView where Self: UIViewController {
  89. var viewController: UIViewController {
  90. self
  91. }
  92. }