NCMainTabBarController.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // NCMainTabBarController.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 02/04/24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. struct NavigationCollectionViewCommon {
  25. var serverUrl: String
  26. var navigationController: UINavigationController?
  27. var viewController: NCCollectionViewCommon
  28. }
  29. class NCMainTabBarController: UITabBarController {
  30. var sceneIdentifier: String = UUID().uuidString
  31. var account = ""
  32. var documentPickerViewController: NCDocumentPickerViewController?
  33. let navigationCollectionViewCommon = ThreadSafeArray<NavigationCollectionViewCommon>()
  34. let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  35. private var previousIndex: Int?
  36. required init?(coder: NSCoder) {
  37. super.init(coder: coder)
  38. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  39. }
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. delegate = self
  43. if #available(iOS 17.0, *) {
  44. traitOverrides.horizontalSizeClass = .compact
  45. }
  46. }
  47. override func viewDidAppear(_ animated: Bool) {
  48. super.viewDidAppear(animated)
  49. previousIndex = selectedIndex
  50. }
  51. @objc func changeTheming(_ notification: NSNotification) {
  52. guard let userInfo = notification.userInfo as? NSDictionary else { return }
  53. let account = userInfo["account"] as? String
  54. if let tabBar = self.tabBar as? NCMainTabBar,
  55. self.account == account {
  56. let color = NCBrandColor.shared.getElement(account: account)
  57. tabBar.color = color
  58. tabBar.tintColor = color
  59. tabBar.setNeedsDisplay()
  60. }
  61. }
  62. func currentViewController() -> UIViewController? {
  63. return (selectedViewController as? UINavigationController)?.topViewController
  64. }
  65. func currentServerUrl() -> String {
  66. let session = NCSession.shared.getSession(account: account)
  67. var serverUrl = NCUtilityFileSystem().getHomeServer(session: session)
  68. let viewController = currentViewController()
  69. if let collectionViewCommon = viewController as? NCCollectionViewCommon {
  70. if !collectionViewCommon.serverUrl.isEmpty {
  71. serverUrl = collectionViewCommon.serverUrl
  72. }
  73. }
  74. return serverUrl
  75. }
  76. }
  77. extension NCMainTabBarController: UITabBarControllerDelegate {
  78. func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
  79. if previousIndex == tabBarController.selectedIndex {
  80. scrollToTop(viewController: viewController)
  81. }
  82. previousIndex = tabBarController.selectedIndex
  83. }
  84. private func scrollToTop(viewController: UIViewController) {
  85. guard let navigationController = viewController as? UINavigationController,
  86. let topViewController = navigationController.topViewController else { return }
  87. if let scrollView = topViewController.view.subviews.compactMap({ $0 as? UIScrollView }).first {
  88. scrollView.setContentOffset(CGPoint(x: 0, y: -scrollView.adjustedContentInset.top), animated: true)
  89. }
  90. }
  91. }