VLCTabBarCoordinator.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*****************************************************************************
  2. * VLCTabBarCoordinator.swift
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <nitz.carola # gmail.com>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. class VLCTabBarCoordinator: NSObject {
  13. private var tabBarController: UITabBarController
  14. private var services: Services
  15. init(tabBarController: UITabBarController, services: Services) {
  16. self.tabBarController = tabBarController
  17. self.services = services
  18. super.init()
  19. setup()
  20. NotificationCenter.default.addObserver(self, selector: #selector(updateTheme), name: .VLCThemeDidChangeNotification, object: nil)
  21. }
  22. private func setup() {
  23. setupViewControllers()
  24. updateTheme()
  25. }
  26. @objc func updateTheme() {
  27. //Setting this in appearanceManager doesn't update tabbar and UINavigationbar of the settingsViewController on change hence we do it here
  28. tabBarController.tabBar.isTranslucent = false
  29. tabBarController.tabBar.backgroundColor = PresentationTheme.current.colors.tabBarColor
  30. tabBarController.tabBar.barTintColor = PresentationTheme.current.colors.tabBarColor
  31. tabBarController.tabBar.itemPositioning = .fill
  32. tabBarController.viewControllers?.forEach {
  33. if let navController = $0 as? UINavigationController, navController.topViewController is VLCSettingsController {
  34. navController.navigationBar.isTranslucent = false
  35. navController.navigationBar.barTintColor = PresentationTheme.current.colors.navigationbarColor
  36. navController.navigationBar.tintColor = PresentationTheme.current.colors.orangeUI
  37. navController.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: PresentationTheme.current.colors.navigationbarTextColor]
  38. if #available(iOS 11.0, *) {
  39. navController.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: PresentationTheme.current.colors.navigationbarTextColor]
  40. }
  41. if #available(iOS 13.0, *) {
  42. navController.navigationBar.standardAppearance = AppearanceManager.navigationbarAppearance()
  43. navController.navigationBar.scrollEdgeAppearance = AppearanceManager.navigationbarAppearance()
  44. }
  45. }
  46. }
  47. }
  48. private func setupViewControllers() {
  49. let controllers: [UIViewController] = [
  50. VLCVideoViewController(services: services),
  51. VLCAudioViewController(services: services),
  52. VLCPlaylistViewController(services: services),
  53. VLCServerListViewController(nibName: nil, bundle: nil),
  54. VLCSettingsController(mediaLibraryService: services.medialibraryService)
  55. ]
  56. tabBarController.viewControllers = controllers.map { UINavigationController(rootViewController: $0) }
  57. }
  58. func handleShortcutItem(_ item: UIApplicationShortcutItem) {
  59. switch item.type {
  60. case kVLCApplicationShortcutLocalVideo:
  61. tabBarController.selectedIndex = tabBarController.viewControllers?.firstIndex(where: { vc -> Bool in
  62. vc is VLCVideoViewController
  63. }) ?? 0
  64. case kVLCApplicationShortcutLocalAudio:
  65. tabBarController.selectedIndex = tabBarController.viewControllers?.firstIndex(where: { vc -> Bool in
  66. vc is VLCAudioViewController
  67. }) ?? 1
  68. case kVLCApplicationShortcutPlaylist:
  69. tabBarController.selectedIndex = tabBarController.viewControllers?.firstIndex(where: { vc -> Bool in
  70. vc is VLCPlaylistViewController
  71. }) ?? 2
  72. case kVLCApplicationShortcutNetwork:
  73. tabBarController.selectedIndex = tabBarController.viewControllers?.firstIndex(where: { vc -> Bool in
  74. vc is VLCServerListViewController
  75. }) ?? 3
  76. default:
  77. assertionFailure("unhandled shortcut")
  78. }
  79. }
  80. }
  81. extension UITabBarController {
  82. open override var preferredStatusBarStyle: UIStatusBarStyle {
  83. return PresentationTheme.current.colors.statusBarStyle
  84. }
  85. override open func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  86. super.traitCollectionDidChange(previousTraitCollection)
  87. if #available(iOS 13.0, *) {
  88. guard previousTraitCollection?.userInterfaceStyle != traitCollection.userInterfaceStyle else {
  89. // Since traitCollectionDidChange is called in, for example rotations, we make sure that
  90. // there was a userInterfaceStyle change.
  91. return
  92. }
  93. guard UserDefaults.standard.integer(forKey: kVLCSettingAppTheme) == kVLCSettingAppThemeSystem else {
  94. // Theme is specificly set, do not follow systeme theme.
  95. return
  96. }
  97. let isSystemDarkTheme = traitCollection.userInterfaceStyle == .dark
  98. PresentationTheme.current = isSystemDarkTheme ? PresentationTheme.darkTheme : PresentationTheme.brightTheme
  99. }
  100. }
  101. }