forked from shadowfacts/Tusker
227 lines
8.5 KiB
Swift
227 lines
8.5 KiB
Swift
//
|
|
// MainTabBarViewController.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 8/21/18.
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import ComposeUI
|
|
|
|
@available(iOS, obsoleted: 18.0)
|
|
class MainTabBarViewController: BaseMainTabBarViewController {
|
|
|
|
private var composePlaceholder: UIViewController!
|
|
|
|
var currentTab: Tab {
|
|
return Tab(rawValue: selectedIndex)!
|
|
}
|
|
|
|
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
|
|
if UIDevice.current.userInterfaceIdiom == .phone {
|
|
return .portrait
|
|
} else {
|
|
return .all
|
|
}
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
stateRestorationLogger.info("MainTabBarViewController: viewDidLoad, selectedIndex=\(self.selectedIndex, privacy: .public)")
|
|
|
|
self.delegate = self
|
|
|
|
composePlaceholder = UIViewController()
|
|
composePlaceholder.title = "Compose"
|
|
composePlaceholder.tabBarItem.image = UIImage(systemName: "pencil")
|
|
|
|
viewControllers = [
|
|
embedInNavigationController(Tab.timelines.createViewController(mastodonController)),
|
|
embedInNavigationController(Tab.notifications.createViewController(mastodonController)),
|
|
composePlaceholder,
|
|
embedInNavigationController(Tab.explore.createViewController(mastodonController)),
|
|
embedInNavigationController(Tab.myProfile.createViewController(mastodonController)),
|
|
]
|
|
|
|
#if !os(visionOS)
|
|
setupFastAccountSwitcher()
|
|
#endif
|
|
|
|
tabBar.isSpringLoaded = true
|
|
|
|
view.backgroundColor = .appBackground
|
|
}
|
|
|
|
func select(tab: Tab, dismissPresented: Bool, animated: Bool, completion: (() -> Void)? = nil) {
|
|
if tab == .compose {
|
|
compose(editing: nil, completion: completion)
|
|
} else {
|
|
// when switching tabs, dismiss the currently presented VC
|
|
// otherwise the selected tab changes behind the presented VC
|
|
if presentedViewController != nil && dismissPresented {
|
|
dismiss(animated: animated) {
|
|
stateRestorationLogger.info("MainTabBarViewController: selecting \(String(describing: tab), privacy: .public)")
|
|
self.selectedIndex = tab.rawValue
|
|
completion?()
|
|
}
|
|
} else {
|
|
stateRestorationLogger.info("MainTabBarViewController: selecting \(String(describing: tab), privacy: .public)")
|
|
selectedIndex = tab.rawValue
|
|
completion?()
|
|
}
|
|
}
|
|
}
|
|
|
|
@objc func handleComposeKeyCommand() {
|
|
compose(editing: nil)
|
|
}
|
|
|
|
func embedInNavigationController(_ vc: UIViewController) -> UINavigationController {
|
|
if let vc = vc as? UINavigationController {
|
|
return vc
|
|
} else {
|
|
let nav = EnhancedNavigationViewController(rootViewController: vc)
|
|
return nav
|
|
}
|
|
}
|
|
|
|
func setViewController(_ viewController: UIViewController, forTab tab: Tab) {
|
|
viewControllers![tab.rawValue] = viewController
|
|
}
|
|
|
|
func viewController(for tab: Tab) -> UIViewController {
|
|
return viewControllers![tab.rawValue]
|
|
}
|
|
}
|
|
|
|
extension MainTabBarViewController {
|
|
enum Tab: Int, Hashable, CaseIterable {
|
|
case timelines
|
|
case notifications
|
|
case compose
|
|
case explore
|
|
case myProfile
|
|
|
|
@MainActor
|
|
func createViewController(_ mastodonController: MastodonController) -> UIViewController {
|
|
switch self {
|
|
case .timelines:
|
|
return TimelinesPageViewController(mastodonController: mastodonController)
|
|
case .notifications:
|
|
return NotificationsPageViewController(mastodonController: mastodonController)
|
|
case .compose:
|
|
return ComposeHostingController(draft: nil, mastodonController: mastodonController)
|
|
case .explore:
|
|
return ExploreViewController(mastodonController: mastodonController)
|
|
case .myProfile:
|
|
return MyProfileViewController(mastodonController: mastodonController)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func getTabController(tab: Tab) -> UIViewController? {
|
|
if tab == .compose {
|
|
return nil
|
|
} else {
|
|
// viewWControllers array is setup in viewDidLoad
|
|
loadViewIfNeeded()
|
|
return viewControllers![tab.rawValue]
|
|
}
|
|
}
|
|
}
|
|
|
|
extension MainTabBarViewController: UITabBarControllerDelegate {
|
|
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
|
|
if viewController == composePlaceholder {
|
|
compose(editing: nil)
|
|
return false
|
|
}
|
|
if selectedIndex != NSNotFound,
|
|
viewController == viewControllers![selectedIndex],
|
|
let nav = viewController as? UINavigationController,
|
|
nav.viewControllers.count == 1,
|
|
let scrollableVC = nav.viewControllers.first as? TabBarScrollableViewController {
|
|
scrollableVC.tabBarScrollToTop()
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
|
|
extension MainTabBarViewController: TuskerRootViewController {
|
|
func select(route: TuskerRoute, animated: Bool, completion: (() -> Void)?) {
|
|
switch route {
|
|
case .timelines:
|
|
select(tab: .timelines, dismissPresented: true, animated: animated, completion: completion)
|
|
case .notifications:
|
|
select(tab: .notifications, dismissPresented: true, animated: animated, completion: completion)
|
|
case .myProfile:
|
|
select(tab: .myProfile, dismissPresented: true, animated: animated, completion: completion)
|
|
case .explore:
|
|
select(tab: .explore, dismissPresented: true, animated: animated, completion: completion)
|
|
case .bookmarks:
|
|
select(tab: .explore, dismissPresented: true, animated: animated, completion: completion)
|
|
getNavigationController().pushViewController(BookmarksViewController(mastodonController: mastodonController), animated: animated)
|
|
case .list(id: let id):
|
|
select(tab: .explore, dismissPresented: true, animated: animated, completion: completion)
|
|
if let list = mastodonController.getCachedList(id: id) {
|
|
let nav = getNavigationController()
|
|
_ = nav.popToRootViewController(animated: animated)
|
|
nav.pushViewController(ListTimelineViewController(for: list, mastodonController: mastodonController), animated: animated)
|
|
}
|
|
}
|
|
}
|
|
|
|
func getNavigationDelegate() -> TuskerNavigationDelegate? {
|
|
return self
|
|
}
|
|
|
|
func getNavigationController() -> NavigationControllerProtocol {
|
|
return (selectedViewController as! UINavigationController)
|
|
}
|
|
|
|
func performSearch(query: String) {
|
|
guard let exploreNavController = getTabController(tab: .explore) as? UINavigationController,
|
|
let exploreController = exploreNavController.viewControllers.first as? ExploreViewController else {
|
|
return
|
|
}
|
|
|
|
select(tab: .explore, dismissPresented: true, animated: false)
|
|
exploreNavController.popToRootViewController(animated: false)
|
|
|
|
// setting searchController.isActive directly doesn't work until the view has loaded/appeared for the first time
|
|
if exploreController.isViewLoaded {
|
|
exploreController.searchController.isActive = true
|
|
} else {
|
|
exploreController.searchControllerStatusOnAppearance = true
|
|
// we still need to load the view so that we can setup the search query
|
|
exploreController.loadViewIfNeeded()
|
|
}
|
|
|
|
exploreController.searchController.searchBar.text = query
|
|
exploreController.resultsController.performSearch(query: query)
|
|
}
|
|
|
|
func presentPreferences(completion: (() -> Void)?) -> PreferencesNavigationController? {
|
|
let vc = PreferencesNavigationController(mastodonController: mastodonController)
|
|
present(vc, animated: true, completion: completion)
|
|
return vc
|
|
}
|
|
}
|
|
|
|
extension MainTabBarViewController: AccountSwitchableViewController {
|
|
var isFastAccountSwitcherActive: Bool {
|
|
#if os(visionOS)
|
|
return false
|
|
#else
|
|
if let fastAccountSwitcher {
|
|
return !fastAccountSwitcher.view.isHidden
|
|
} else {
|
|
return false
|
|
}
|
|
#endif
|
|
}
|
|
}
|