2021-01-21 00:33:53 +01:00
|
|
|
// Copyright © 2021 Metabolist. All rights reserved.
|
|
|
|
|
2021-01-21 09:45:09 +01:00
|
|
|
import Combine
|
|
|
|
import SwiftUI
|
2021-01-21 00:33:53 +01:00
|
|
|
import ViewModels
|
|
|
|
|
|
|
|
final class MainNavigationViewController: UITabBarController {
|
|
|
|
private let viewModel: NavigationViewModel
|
|
|
|
private let rootViewModel: RootViewModel
|
2021-01-21 09:45:09 +01:00
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
private weak var presentedSecondaryNavigation: UINavigationController?
|
2021-01-21 00:33:53 +01:00
|
|
|
|
|
|
|
init(viewModel: NavigationViewModel, rootViewModel: RootViewModel) {
|
|
|
|
self.viewModel = viewModel
|
|
|
|
self.rootViewModel = rootViewModel
|
|
|
|
|
|
|
|
super.init(nibName: nil, bundle: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(*, unavailable)
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2021-01-21 09:45:09 +01:00
|
|
|
viewModel.$presentingSecondaryNavigation.sink { [weak self] in
|
|
|
|
if $0 {
|
|
|
|
self?.presentSecondaryNavigation()
|
|
|
|
} else {
|
|
|
|
self?.dismissSecondaryNavigation()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
|
|
|
|
2021-01-27 23:23:52 +01:00
|
|
|
viewModel.identityContext.$identity.map(\.pending)
|
|
|
|
.removeDuplicates()
|
|
|
|
.print()
|
|
|
|
.sink { [weak self] in self?.setupViewControllers(pending: $0) }
|
|
|
|
.store(in: &cancellables)
|
|
|
|
|
2021-01-26 07:57:44 +01:00
|
|
|
viewModel.timelineNavigations.map { _ in }
|
|
|
|
.merge(with: viewModel.followRequestNavigations.map { _ in })
|
|
|
|
.sink { [weak self] in self?.selectedIndex = 0 }
|
2021-01-21 09:45:09 +01:00
|
|
|
.store(in: &cancellables)
|
2021-01-27 23:23:52 +01:00
|
|
|
|
|
|
|
NotificationCenter.default.publisher(for: UIScene.willEnterForegroundNotification)
|
|
|
|
.sink { [weak self] _ in self?.viewModel.refreshIdentity() }
|
|
|
|
.store(in: &cancellables)
|
2021-01-21 09:45:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
|
|
|
viewModel.refreshIdentity()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension MainNavigationViewController {
|
2021-01-27 23:23:52 +01:00
|
|
|
func setupViewControllers(pending: Bool) {
|
2021-01-23 04:48:33 +01:00
|
|
|
var controllers: [UIViewController] = [
|
|
|
|
TimelinesViewController(
|
|
|
|
viewModel: viewModel,
|
|
|
|
rootViewModel: rootViewModel),
|
|
|
|
ExploreViewController(
|
2021-01-27 23:23:52 +01:00
|
|
|
viewModel: viewModel.exploreViewModel(),
|
2021-01-26 01:45:18 +01:00
|
|
|
rootViewModel: rootViewModel)
|
2021-01-23 04:48:33 +01:00
|
|
|
]
|
2021-01-21 09:45:09 +01:00
|
|
|
|
2021-01-27 23:23:52 +01:00
|
|
|
if viewModel.identityContext.identity.authenticated && !pending {
|
2021-01-26 03:10:24 +01:00
|
|
|
controllers.append(NotificationsViewController(viewModel: viewModel, rootViewModel: rootViewModel))
|
2021-01-21 00:33:53 +01:00
|
|
|
|
|
|
|
let conversationsViewController = TableViewController(
|
2021-01-27 23:23:52 +01:00
|
|
|
viewModel: viewModel.conversationsViewModel(),
|
2021-01-26 01:45:18 +01:00
|
|
|
rootViewModel: rootViewModel)
|
2021-01-21 00:33:53 +01:00
|
|
|
|
|
|
|
conversationsViewController.tabBarItem = NavigationViewModel.Tab.messages.tabBarItem
|
|
|
|
conversationsViewController.navigationItem.title = NavigationViewModel.Tab.messages.title
|
|
|
|
|
2021-01-21 09:45:09 +01:00
|
|
|
controllers.append(conversationsViewController)
|
2021-01-27 23:23:52 +01:00
|
|
|
|
|
|
|
setupNewStatusButton()
|
2021-01-21 09:45:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let secondaryNavigationButton = SecondaryNavigationButton(viewModel: viewModel, rootViewModel: rootViewModel)
|
|
|
|
|
|
|
|
for controller in controllers {
|
|
|
|
controller.navigationItem.leftBarButtonItem = secondaryNavigationButton
|
|
|
|
}
|
|
|
|
|
|
|
|
viewControllers = controllers.map(UINavigationController.init(rootViewController:))
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupNewStatusButton() {
|
|
|
|
let newStatusButtonView = NewStatusButtonView(primaryAction: UIAction { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
|
|
|
let newStatusViewModel = self.rootViewModel.newStatusViewModel(
|
2021-01-26 01:06:35 +01:00
|
|
|
identityContext: self.viewModel.identityContext)
|
2021-01-21 09:45:09 +01:00
|
|
|
let newStatusViewController = NewStatusViewController(viewModel: newStatusViewModel)
|
|
|
|
let newStatusNavigationController = UINavigationController(rootViewController: newStatusViewController)
|
|
|
|
|
|
|
|
if UIDevice.current.userInterfaceIdiom == .phone {
|
|
|
|
newStatusNavigationController.modalPresentationStyle = .overFullScreen
|
2021-01-22 07:12:29 +01:00
|
|
|
} else {
|
|
|
|
newStatusNavigationController.isModalInPresentation = true
|
2021-01-21 09:45:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
self.present(newStatusNavigationController, animated: true)
|
|
|
|
})
|
|
|
|
|
|
|
|
view.addSubview(newStatusButtonView)
|
|
|
|
newStatusButtonView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
newStatusButtonView.widthAnchor.constraint(equalToConstant: .newStatusButtonDimension),
|
|
|
|
newStatusButtonView.heightAnchor.constraint(equalToConstant: .newStatusButtonDimension),
|
|
|
|
newStatusButtonView.trailingAnchor.constraint(
|
|
|
|
equalTo: view.safeAreaLayoutGuide.trailingAnchor,
|
|
|
|
constant: -.defaultSpacing * 2),
|
|
|
|
newStatusButtonView.bottomAnchor.constraint(equalTo: tabBar.topAnchor, constant: -.defaultSpacing * 2)
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
func presentSecondaryNavigation() {
|
|
|
|
let secondaryNavigationView = SecondaryNavigationView(viewModel: viewModel)
|
|
|
|
.environmentObject(rootViewModel)
|
|
|
|
let hostingController = UIHostingController(rootView: secondaryNavigationView)
|
|
|
|
|
|
|
|
hostingController.navigationItem.leftBarButtonItem = UIBarButtonItem(
|
|
|
|
systemItem: .close,
|
|
|
|
primaryAction: UIAction { [weak self] _ in self?.viewModel.presentingSecondaryNavigation = false })
|
|
|
|
|
|
|
|
let navigationController = UINavigationController(rootViewController: hostingController)
|
|
|
|
|
|
|
|
presentedSecondaryNavigation = navigationController
|
|
|
|
present(navigationController, animated: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func dismissSecondaryNavigation() {
|
|
|
|
if presentedViewController == presentedSecondaryNavigation {
|
|
|
|
dismiss(animated: true)
|
2021-01-21 00:33:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|