2021-10-28 13:17:41 +02:00
|
|
|
//
|
|
|
|
// ContentSplitViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by Cirno MainasuK on 2021-10-28.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreDataStack
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2021-10-28 13:17:41 +02:00
|
|
|
|
2021-11-01 13:09:30 +01:00
|
|
|
protocol ContentSplitViewControllerDelegate: AnyObject {
|
|
|
|
func contentSplitViewController(_ contentSplitViewController: ContentSplitViewController, sidebarViewController: SidebarViewController, didSelectTab tab: MainTabBarController.Tab)
|
|
|
|
}
|
|
|
|
|
2021-10-28 13:17:41 +02:00
|
|
|
final class ContentSplitViewController: UIViewController, NeedsDependency {
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
static let sidebarWidth: CGFloat = 89
|
|
|
|
|
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
|
2022-10-08 09:16:10 +02:00
|
|
|
var authContext: AuthContext?
|
|
|
|
|
2021-11-01 13:09:30 +01:00
|
|
|
weak var delegate: ContentSplitViewControllerDelegate?
|
|
|
|
|
2021-10-28 13:17:41 +02:00
|
|
|
private(set) lazy var sidebarViewController: SidebarViewController = {
|
|
|
|
let sidebarViewController = SidebarViewController()
|
|
|
|
sidebarViewController.context = context
|
|
|
|
sidebarViewController.coordinator = coordinator
|
|
|
|
sidebarViewController.viewModel = SidebarViewModel(context: context)
|
|
|
|
sidebarViewController.delegate = self
|
|
|
|
return sidebarViewController
|
|
|
|
}()
|
|
|
|
|
|
|
|
@Published var currentSupplementaryTab: MainTabBarController.Tab = .home
|
|
|
|
private(set) lazy var mainTabBarController: MainTabBarController = {
|
2022-10-08 09:16:10 +02:00
|
|
|
let mainTabBarController = MainTabBarController(context: context, coordinator: coordinator, authContext: authContext)
|
2021-10-29 12:14:51 +02:00
|
|
|
if let homeTimelineViewController = mainTabBarController.viewController(of: HomeTimelineViewController.self) {
|
2022-02-10 09:43:26 +01:00
|
|
|
homeTimelineViewController.viewModel.displaySettingBarButtonItem = false
|
2021-10-29 12:14:51 +02:00
|
|
|
}
|
2021-10-28 13:17:41 +02:00
|
|
|
return mainTabBarController
|
|
|
|
}()
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ContentSplitViewController {
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
navigationController?.setNavigationBarHidden(true, animated: false)
|
|
|
|
|
|
|
|
addChild(sidebarViewController)
|
2021-10-29 08:58:59 +02:00
|
|
|
sidebarViewController.view.translatesAutoresizingMaskIntoConstraints = false
|
2021-10-28 13:17:41 +02:00
|
|
|
view.addSubview(sidebarViewController.view)
|
|
|
|
sidebarViewController.didMove(toParent: self)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
sidebarViewController.view.topAnchor.constraint(equalTo: view.topAnchor),
|
|
|
|
sidebarViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
sidebarViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
|
|
sidebarViewController.view.widthAnchor.constraint(equalToConstant: ContentSplitViewController.sidebarWidth),
|
|
|
|
])
|
|
|
|
|
|
|
|
addChild(mainTabBarController)
|
|
|
|
mainTabBarController.view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.addSubview(mainTabBarController.view)
|
|
|
|
sidebarViewController.didMove(toParent: self)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
mainTabBarController.view.topAnchor.constraint(equalTo: view.topAnchor),
|
2021-10-29 11:26:26 +02:00
|
|
|
mainTabBarController.view.leadingAnchor.constraint(equalTo: sidebarViewController.view.trailingAnchor, constant: UIView.separatorLineHeight(of: view)),
|
2021-10-28 13:17:41 +02:00
|
|
|
mainTabBarController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
|
|
mainTabBarController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
|
|
])
|
|
|
|
|
2022-05-11 12:40:21 +02:00
|
|
|
// response keyboard command tab switch
|
|
|
|
mainTabBarController.$currentTab
|
|
|
|
.sink { [weak self] tab in
|
|
|
|
guard let self = self else { return }
|
|
|
|
if tab != self.currentSupplementaryTab {
|
|
|
|
self.currentSupplementaryTab = tab
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2021-10-28 13:17:41 +02:00
|
|
|
$currentSupplementaryTab
|
|
|
|
.removeDuplicates()
|
|
|
|
.sink(receiveValue: { [weak self] tab in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.mainTabBarController.selectedIndex = tab.rawValue
|
2022-05-06 08:29:34 +02:00
|
|
|
self.mainTabBarController.currentTab = tab
|
|
|
|
self.sidebarViewController.viewModel.currentTab = tab
|
2021-10-28 13:17:41 +02:00
|
|
|
})
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - SidebarViewControllerDelegate
|
|
|
|
extension ContentSplitViewController: SidebarViewControllerDelegate {
|
|
|
|
|
|
|
|
func sidebarViewController(_ sidebarViewController: SidebarViewController, didSelectTab tab: MainTabBarController.Tab) {
|
2021-11-01 13:09:30 +01:00
|
|
|
delegate?.contentSplitViewController(self, sidebarViewController: sidebarViewController, didSelectTab: tab)
|
2021-10-29 11:26:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func sidebarViewController(_ sidebarViewController: SidebarViewController, didLongPressItem item: SidebarViewModel.Item, sourceView: UIView) {
|
|
|
|
guard case let .tab(tab) = item, tab == .me else { return }
|
|
|
|
|
|
|
|
let accountListViewController = coordinator.present(scene: .accountList, from: nil, transition: .popover(sourceView: sourceView)) as! AccountListViewController
|
|
|
|
accountListViewController.dragIndicatorView.barView.isHidden = true
|
2021-11-02 09:02:46 +01:00
|
|
|
// content width needs > 300 to make checkmark display
|
|
|
|
accountListViewController.preferredContentSize = CGSize(width: 375, height: 400)
|
2021-10-28 13:17:41 +02:00
|
|
|
}
|
2021-10-29 11:26:26 +02:00
|
|
|
|
2021-10-28 13:17:41 +02:00
|
|
|
}
|