NCMainTabBarController.swift 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 documentPickerViewController: NCDocumentPickerViewController?
  32. let navigationCollectionViewCommon = ThreadSafeArray<NavigationCollectionViewCommon>()
  33. let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  34. private var previousIndex: Int?
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37. delegate = self
  38. }
  39. override func viewDidAppear(_ animated: Bool) {
  40. super.viewDidAppear(animated)
  41. previousIndex = selectedIndex
  42. }
  43. func currentViewController() -> UIViewController? {
  44. return (selectedViewController as? UINavigationController)?.topViewController
  45. }
  46. func currentServerUrl() -> String {
  47. var serverUrl = NCUtilityFileSystem().getHomeServer(urlBase: self.appDelegate.urlBase, userId: self.appDelegate.userId)
  48. let viewController = currentViewController()
  49. if let collectionViewCommon = viewController as? NCCollectionViewCommon {
  50. if !collectionViewCommon.serverUrl.isEmpty {
  51. serverUrl = collectionViewCommon.serverUrl
  52. }
  53. } else if let media = viewController as? NCMedia {
  54. serverUrl = media.serverUrl
  55. } else if let viewerMediaPage = viewController as? NCViewerMediaPage {
  56. serverUrl = viewerMediaPage.metadatas[viewerMediaPage.currentIndex].serverUrl
  57. }
  58. return serverUrl
  59. }
  60. }
  61. extension NCMainTabBarController: UITabBarControllerDelegate {
  62. func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
  63. if previousIndex == tabBarController.selectedIndex {
  64. scrollToTop(viewController: viewController)
  65. }
  66. previousIndex = tabBarController.selectedIndex
  67. }
  68. private func scrollToTop(viewController: UIViewController) {
  69. guard let navigationController = viewController as? UINavigationController,
  70. let topViewController = navigationController.topViewController else { return }
  71. if let scrollView = topViewController.view.subviews.compactMap({ $0 as? UIScrollView }).first {
  72. scrollView.setContentOffset(CGPoint(x: 0, y: -scrollView.adjustedContentInset.top), animated: true)
  73. }
  74. }
  75. }