2018-08-21 21:17:25 +00:00
|
|
|
//
|
|
|
|
// MainTabBarViewController.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
2019-02-08 18:41:19 +00:00
|
|
|
// Created by Shadowfacts on 8/21/18.
|
2018-08-21 21:17:25 +00:00
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2020-08-31 23:28:50 +00:00
|
|
|
import SwiftUI
|
2018-08-21 21:17:25 +00:00
|
|
|
|
2019-01-05 17:59:55 +00:00
|
|
|
class MainTabBarViewController: UITabBarController, UITabBarControllerDelegate {
|
2020-01-05 20:25:07 +00:00
|
|
|
|
2020-01-20 04:02:07 +00:00
|
|
|
weak var mastodonController: MastodonController!
|
2020-06-24 20:40:45 +00:00
|
|
|
|
|
|
|
private var composePlaceholder: UIViewController!
|
2020-11-10 00:39:42 +00:00
|
|
|
private var fastAccountSwitcher: FastAccountSwitcherViewController!
|
2020-06-30 02:21:03 +00:00
|
|
|
|
|
|
|
var selectedTab: Tab {
|
|
|
|
return Tab(rawValue: selectedIndex)!
|
|
|
|
}
|
2018-08-21 21:17:25 +00:00
|
|
|
|
2020-01-08 03:19:38 +00:00
|
|
|
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
|
|
|
|
if UIDevice.current.userInterfaceIdiom == .phone {
|
|
|
|
return .portrait
|
|
|
|
} else {
|
|
|
|
return .all
|
|
|
|
}
|
|
|
|
}
|
2020-01-05 20:25:07 +00:00
|
|
|
|
|
|
|
init(mastodonController: MastodonController) {
|
|
|
|
self.mastodonController = mastodonController
|
|
|
|
|
|
|
|
super.init(nibName: nil, bundle: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
2020-01-18 23:56:36 +00:00
|
|
|
}
|
2020-01-05 20:25:07 +00:00
|
|
|
|
2018-08-21 21:17:25 +00:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2019-01-05 17:59:55 +00:00
|
|
|
self.delegate = self
|
|
|
|
|
2020-06-24 20:40:45 +00:00
|
|
|
composePlaceholder = UIViewController()
|
|
|
|
composePlaceholder.title = "Compose"
|
|
|
|
composePlaceholder.tabBarItem.image = UIImage(systemName: "pencil")
|
|
|
|
|
2019-01-05 17:59:55 +00:00
|
|
|
viewControllers = [
|
2020-06-24 20:40:45 +00:00
|
|
|
embedInNavigationController(Tab.timelines.createViewController(mastodonController)),
|
|
|
|
embedInNavigationController(Tab.notifications.createViewController(mastodonController)),
|
|
|
|
composePlaceholder,
|
|
|
|
embedInNavigationController(Tab.explore.createViewController(mastodonController)),
|
|
|
|
embedInNavigationController(Tab.myProfile.createViewController(mastodonController)),
|
2019-01-05 17:59:55 +00:00
|
|
|
]
|
2020-11-10 00:39:42 +00:00
|
|
|
|
|
|
|
fastAccountSwitcher = FastAccountSwitcherViewController()
|
|
|
|
fastAccountSwitcher.delegate = self
|
|
|
|
fastAccountSwitcher.view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(fastAccountSwitcher.view)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
fastAccountSwitcher.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
fastAccountSwitcher.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
|
|
fastAccountSwitcher.view.topAnchor.constraint(equalTo: view.topAnchor),
|
|
|
|
fastAccountSwitcher.view.bottomAnchor.constraint(equalTo: tabBar.topAnchor),
|
|
|
|
])
|
|
|
|
|
|
|
|
tabBar.addGestureRecognizer(fastAccountSwitcher.createSwitcherGesture())
|
|
|
|
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(tabBarTapped))
|
|
|
|
tapRecognizer.cancelsTouchesInView = false
|
|
|
|
tabBar.addGestureRecognizer(tapRecognizer)
|
2020-12-15 03:16:16 +00:00
|
|
|
|
|
|
|
tabBar.isSpringLoaded = true
|
2020-11-10 00:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func tabBarTapped(_ recognizer: UITapGestureRecognizer) {
|
|
|
|
fastAccountSwitcher.hide()
|
2018-08-21 21:17:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 14:38:36 +00:00
|
|
|
func embedInNavigationController(_ vc: UIViewController) -> UINavigationController {
|
|
|
|
if let vc = vc as? UINavigationController {
|
|
|
|
return vc
|
|
|
|
} else {
|
2020-02-20 03:07:12 +00:00
|
|
|
return EnhancedNavigationViewController(rootViewController: vc)
|
2018-10-06 14:38:36 +00:00
|
|
|
}
|
2018-10-01 01:28:22 +00:00
|
|
|
}
|
2018-08-31 02:30:19 +00:00
|
|
|
|
2019-01-05 17:59:55 +00:00
|
|
|
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
|
2020-06-24 20:40:45 +00:00
|
|
|
if viewController == composePlaceholder {
|
2019-09-16 01:00:57 +00:00
|
|
|
presentCompose()
|
2019-01-05 17:59:55 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-07-03 23:36:48 +00:00
|
|
|
if viewController == viewControllers![selectedIndex],
|
|
|
|
let nav = viewController as? UINavigationController,
|
|
|
|
nav.viewControllers.count == 1,
|
|
|
|
let scrollableVC = nav.viewControllers.first as? TabBarScrollableViewController {
|
|
|
|
scrollableVC.tabBarScrollToTop()
|
|
|
|
return false
|
|
|
|
}
|
2019-01-05 17:59:55 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-06-30 02:21:03 +00:00
|
|
|
|
|
|
|
func setViewController(_ viewController: UIViewController, forTab tab: Tab) {
|
|
|
|
viewControllers![tab.rawValue] = viewController
|
|
|
|
}
|
|
|
|
|
|
|
|
func viewController(for tab: Tab) -> UIViewController {
|
|
|
|
return viewControllers![tab.rawValue]
|
|
|
|
}
|
2019-09-16 01:15:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension MainTabBarViewController {
|
2020-06-24 20:40:45 +00:00
|
|
|
enum Tab: Int, Hashable, CaseIterable {
|
2019-09-16 01:15:32 +00:00
|
|
|
case timelines
|
|
|
|
case notifications
|
|
|
|
case compose
|
2019-12-17 05:22:25 +00:00
|
|
|
case explore
|
2019-09-16 01:15:32 +00:00
|
|
|
case myProfile
|
2020-06-24 20:40:45 +00:00
|
|
|
|
|
|
|
func createViewController(_ mastodonController: MastodonController) -> UIViewController {
|
|
|
|
switch self {
|
|
|
|
case .timelines:
|
|
|
|
return TimelinesPageViewController(mastodonController: mastodonController)
|
|
|
|
case .notifications:
|
|
|
|
return NotificationsPageViewController(mastodonController: mastodonController)
|
|
|
|
case .compose:
|
2020-08-31 23:28:50 +00:00
|
|
|
return ComposeHostingController(mastodonController: mastodonController)
|
2020-06-24 20:40:45 +00:00
|
|
|
case .explore:
|
|
|
|
return ExploreViewController(mastodonController: mastodonController)
|
|
|
|
case .myProfile:
|
2020-07-05 20:17:56 +00:00
|
|
|
return MyProfileViewController(mastodonController: mastodonController)
|
2020-06-24 20:40:45 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-16 01:15:32 +00:00
|
|
|
}
|
2019-09-16 01:00:57 +00:00
|
|
|
|
2020-06-24 20:40:45 +00:00
|
|
|
func getTabController(tab: Tab) -> UIViewController? {
|
2019-09-16 01:00:57 +00:00
|
|
|
if tab == .compose {
|
2020-06-24 20:40:45 +00:00
|
|
|
return nil
|
2019-09-16 01:00:57 +00:00
|
|
|
} else {
|
2020-06-24 20:40:45 +00:00
|
|
|
return viewControllers![tab.rawValue]
|
2019-09-16 01:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-24 20:40:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 00:39:42 +00:00
|
|
|
extension MainTabBarViewController: FastAccountSwitcherViewControllerDelegate {
|
|
|
|
func fastAccountSwitcher(_ fastAccountSwitcher: FastAccountSwitcherViewController, triggerZoneContains point: CGPoint) -> Bool {
|
|
|
|
let tabBarButtons = tabBar.subviews.filter { String(describing: type(of: $0)).lowercased().contains("button") }
|
|
|
|
// sanity check that there is 1 button per VC
|
|
|
|
guard tabBarButtons.count == viewControllers!.count,
|
|
|
|
let myProfileButton = tabBarButtons.last else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
let locationInButton = myProfileButton.convert(point, from: fastAccountSwitcher.view)
|
|
|
|
return myProfileButton.bounds.contains(locationInButton)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 20:40:45 +00:00
|
|
|
extension MainTabBarViewController: TuskerRootViewController {
|
2020-11-14 16:55:19 +00:00
|
|
|
@objc func presentCompose() {
|
2020-08-31 23:28:50 +00:00
|
|
|
let vc = ComposeHostingController(mastodonController: mastodonController)
|
|
|
|
let nav = EnhancedNavigationViewController(rootViewController: vc)
|
|
|
|
nav.presentationController?.delegate = vc
|
|
|
|
present(nav, animated: true)
|
2020-06-24 20:40:45 +00:00
|
|
|
}
|
2019-09-16 01:00:57 +00:00
|
|
|
|
2020-06-24 20:40:45 +00:00
|
|
|
func select(tab: Tab) {
|
2019-09-16 01:00:57 +00:00
|
|
|
if tab == .compose {
|
2020-06-24 20:40:45 +00:00
|
|
|
presentCompose()
|
2019-09-16 01:00:57 +00:00
|
|
|
} else {
|
2020-06-24 20:40:45 +00:00
|
|
|
selectedIndex = tab.rawValue
|
2019-09-16 01:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-21 21:17:25 +00:00
|
|
|
}
|
2020-10-27 02:55:58 +00:00
|
|
|
|
|
|
|
extension MainTabBarViewController: BackgroundableViewController {
|
|
|
|
func sceneDidEnterBackground() {
|
|
|
|
if let selectedVC = selectedViewController as? BackgroundableViewController {
|
|
|
|
selectedVC.sceneDidEnterBackground()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|