2021-09-22 13:08:09 +02:00
|
|
|
//
|
|
|
|
// RootSplitViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by Cirno MainasuK on 2021-9-22.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
2021-09-23 13:32:44 +02:00
|
|
|
import Combine
|
2021-09-26 12:29:08 +02:00
|
|
|
import CoreDataStack
|
2021-09-22 13:08:09 +02:00
|
|
|
|
|
|
|
final class RootSplitViewController: UISplitViewController, NeedsDependency {
|
|
|
|
|
2021-09-23 13:32:44 +02:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
2021-10-28 13:17:41 +02:00
|
|
|
static let sidebarWidth: CGFloat = 89
|
|
|
|
|
2021-09-22 13:08:09 +02:00
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
|
2021-11-02 09:03:34 +01:00
|
|
|
private var isPrimaryDisplay = false
|
|
|
|
|
2021-10-28 13:17:41 +02:00
|
|
|
private(set) lazy var contentSplitViewController: ContentSplitViewController = {
|
|
|
|
let contentSplitViewController = ContentSplitViewController()
|
|
|
|
contentSplitViewController.context = context
|
|
|
|
contentSplitViewController.coordinator = coordinator
|
2021-11-01 13:09:30 +01:00
|
|
|
contentSplitViewController.delegate = self
|
2021-10-28 13:17:41 +02:00
|
|
|
return contentSplitViewController
|
2021-09-23 13:32:44 +02:00
|
|
|
}()
|
2021-09-22 13:08:09 +02:00
|
|
|
|
2021-10-29 11:26:26 +02:00
|
|
|
private(set) lazy var searchViewController: SearchViewController = {
|
|
|
|
let searchViewController = SearchViewController()
|
|
|
|
searchViewController.context = context
|
|
|
|
searchViewController.coordinator = coordinator
|
|
|
|
return searchViewController
|
|
|
|
}()
|
|
|
|
|
2021-10-29 08:58:59 +02:00
|
|
|
lazy var compactMainTabBarViewController = MainTabBarController(context: context, coordinator: coordinator)
|
|
|
|
|
2021-10-29 11:26:26 +02:00
|
|
|
let separatorLine = UIView.separatorLine
|
|
|
|
|
2021-09-22 13:08:09 +02:00
|
|
|
init(context: AppContext, coordinator: SceneCoordinator) {
|
|
|
|
self.context = context
|
|
|
|
self.coordinator = coordinator
|
2021-10-28 13:17:41 +02:00
|
|
|
super.init(style: .doubleColumn)
|
2021-09-22 13:08:09 +02:00
|
|
|
|
2021-10-28 13:17:41 +02:00
|
|
|
primaryEdge = .trailing
|
2021-09-22 13:08:09 +02:00
|
|
|
primaryBackgroundStyle = .sidebar
|
2021-10-28 13:17:41 +02:00
|
|
|
preferredDisplayMode = .twoBesideSecondary
|
2021-09-22 13:08:09 +02:00
|
|
|
preferredSplitBehavior = .tile
|
2021-09-23 13:32:44 +02:00
|
|
|
delegate = self
|
2021-09-22 13:08:09 +02:00
|
|
|
|
2021-10-28 13:17:41 +02:00
|
|
|
// disable edge swipe gesture
|
|
|
|
presentsWithGesture = false
|
|
|
|
|
2021-09-22 13:08:09 +02:00
|
|
|
if #available(iOS 14.5, *) {
|
2021-10-28 13:17:41 +02:00
|
|
|
displayModeButtonVisibility = .never
|
2021-09-22 13:08:09 +02:00
|
|
|
} else {
|
|
|
|
// Fallback on earlier versions
|
|
|
|
}
|
|
|
|
|
2021-10-29 11:26:26 +02:00
|
|
|
setViewController(searchViewController, for: .primary)
|
2021-10-28 13:17:41 +02:00
|
|
|
setViewController(contentSplitViewController, for: .secondary)
|
2021-10-29 08:58:59 +02:00
|
|
|
setViewController(compactMainTabBarViewController, for: .compact)
|
2021-09-22 13:08:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension RootSplitViewController {
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
updateBehavior(size: view.frame.size)
|
2021-10-29 11:26:26 +02:00
|
|
|
|
|
|
|
setupBackground(theme: ThemeService.shared.currentTheme.value)
|
|
|
|
ThemeService.shared.currentTheme
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] theme in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.setupBackground(theme: theme)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-09-22 13:08:09 +02:00
|
|
|
}
|
|
|
|
|
2021-11-02 09:03:34 +01:00
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
|
|
|
updateBehavior(size: view.frame.size)
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:08:09 +02:00
|
|
|
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
|
|
|
|
super.viewWillTransition(to: size, with: coordinator)
|
|
|
|
|
2022-02-18 11:00:34 +01:00
|
|
|
self.updateBehavior(size: size)
|
2021-09-22 13:08:09 +02:00
|
|
|
}
|
|
|
|
|
2021-11-04 08:20:37 +01:00
|
|
|
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
|
|
|
|
super.traitCollectionDidChange(previousTraitCollection)
|
|
|
|
|
|
|
|
setupBackground(theme: ThemeService.shared.currentTheme.value)
|
|
|
|
}
|
|
|
|
|
2021-10-28 13:17:41 +02:00
|
|
|
private func updateBehavior(size: CGSize) {
|
2021-11-02 09:03:34 +01:00
|
|
|
if size.width > 960 {
|
|
|
|
show(.primary)
|
|
|
|
isPrimaryDisplay = true
|
|
|
|
|
|
|
|
} else {
|
2021-10-28 13:17:41 +02:00
|
|
|
hide(.primary)
|
2021-11-02 09:03:34 +01:00
|
|
|
isPrimaryDisplay = false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (contentSplitViewController.currentSupplementaryTab, isPrimaryDisplay) {
|
|
|
|
case (.search, true):
|
|
|
|
// needs switch to other tab when primary display
|
|
|
|
// use FIFO queue save tab history
|
|
|
|
contentSplitViewController.currentSupplementaryTab = .home
|
2021-10-28 13:17:41 +02:00
|
|
|
default:
|
2021-11-02 09:03:34 +01:00
|
|
|
// do nothing
|
|
|
|
break
|
2021-09-26 12:29:08 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-28 13:17:41 +02:00
|
|
|
|
2021-09-23 13:32:44 +02:00
|
|
|
}
|
|
|
|
|
2021-10-29 11:26:26 +02:00
|
|
|
extension RootSplitViewController {
|
|
|
|
|
|
|
|
private func setupBackground(theme: Theme) {
|
|
|
|
// this set column separator line color
|
2021-11-10 10:55:37 +01:00
|
|
|
view.backgroundColor = theme.separator
|
2021-10-29 11:26:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-11-01 13:09:30 +01:00
|
|
|
// MARK: - ContentSplitViewControllerDelegate
|
|
|
|
extension RootSplitViewController: ContentSplitViewControllerDelegate {
|
|
|
|
func contentSplitViewController(_ contentSplitViewController: ContentSplitViewController, sidebarViewController: SidebarViewController, didSelectTab tab: MainTabBarController.Tab) {
|
|
|
|
guard let _ = MainTabBarController.Tab.allCases.firstIndex(of: tab) else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch tab {
|
2021-11-02 09:03:34 +01:00
|
|
|
case .search:
|
|
|
|
guard isPrimaryDisplay else {
|
|
|
|
// only control search tab behavior when primary display
|
|
|
|
fallthrough
|
|
|
|
}
|
2021-11-01 13:09:30 +01:00
|
|
|
guard let navigationController = searchViewController.navigationController else { return }
|
|
|
|
if navigationController.viewControllers.count == 1 {
|
|
|
|
searchViewController.searchBarTapPublisher.send()
|
|
|
|
} else {
|
|
|
|
navigationController.popToRootViewController(animated: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
let previousTab = contentSplitViewController.currentSupplementaryTab
|
|
|
|
contentSplitViewController.currentSupplementaryTab = tab
|
|
|
|
|
|
|
|
if previousTab == tab,
|
|
|
|
let navigationController = contentSplitViewController.mainTabBarController.selectedViewController as? UINavigationController
|
|
|
|
{
|
|
|
|
navigationController.popToRootViewController(animated: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 13:32:44 +02:00
|
|
|
// MARK: - UISplitViewControllerDelegate
|
|
|
|
extension RootSplitViewController: UISplitViewControllerDelegate {
|
|
|
|
|
2021-11-02 09:03:34 +01:00
|
|
|
private static func transform(from: UITabBarController, to: UITabBarController) {
|
2021-10-29 08:58:59 +02:00
|
|
|
let sourceNavigationControllers = from.viewControllers ?? []
|
|
|
|
let targetNavigationControllers = to.viewControllers ?? []
|
|
|
|
|
|
|
|
for (source, target) in zip(sourceNavigationControllers, targetNavigationControllers) {
|
|
|
|
guard let source = source as? UINavigationController,
|
|
|
|
let target = target as? UINavigationController
|
|
|
|
else { continue }
|
|
|
|
let viewControllers = source.popToRootViewController(animated: false) ?? []
|
|
|
|
_ = target.popToRootViewController(animated: false)
|
|
|
|
target.viewControllers.append(contentsOf: viewControllers)
|
|
|
|
}
|
|
|
|
|
|
|
|
to.selectedIndex = from.selectedIndex
|
|
|
|
}
|
|
|
|
|
2021-11-02 09:03:34 +01:00
|
|
|
private static func transform(from: UINavigationController, to: UINavigationController) {
|
|
|
|
let viewControllers = from.popToRootViewController(animated: false) ?? []
|
|
|
|
to.viewControllers.append(contentsOf: viewControllers)
|
|
|
|
}
|
|
|
|
|
2021-10-29 08:58:59 +02:00
|
|
|
// .regular to .compact
|
|
|
|
func splitViewController(
|
|
|
|
_ svc: UISplitViewController,
|
|
|
|
topColumnForCollapsingToProposedTopColumn proposedTopColumn: UISplitViewController.Column
|
|
|
|
) -> UISplitViewController.Column {
|
|
|
|
switch proposedTopColumn {
|
|
|
|
case .compact:
|
|
|
|
RootSplitViewController.transform(from: contentSplitViewController.mainTabBarController, to: compactMainTabBarViewController)
|
2022-05-06 08:29:34 +02:00
|
|
|
compactMainTabBarViewController.currentTab = contentSplitViewController.currentSupplementaryTab
|
2021-09-29 11:37:32 +02:00
|
|
|
|
2021-10-29 08:58:59 +02:00
|
|
|
default:
|
|
|
|
assertionFailure()
|
|
|
|
}
|
|
|
|
|
|
|
|
return proposedTopColumn
|
|
|
|
}
|
|
|
|
|
|
|
|
// .compact to .regular
|
|
|
|
func splitViewController(
|
|
|
|
_ svc: UISplitViewController,
|
|
|
|
displayModeForExpandingToProposedDisplayMode proposedDisplayMode: UISplitViewController.DisplayMode
|
|
|
|
) -> UISplitViewController.DisplayMode {
|
|
|
|
let compactNavigationController = compactMainTabBarViewController.selectedViewController as? UINavigationController
|
|
|
|
|
|
|
|
if let topMost = compactNavigationController?.topMost,
|
|
|
|
topMost is AccountListViewController {
|
|
|
|
topMost.dismiss(animated: false, completion: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
RootSplitViewController.transform(from: compactMainTabBarViewController, to: contentSplitViewController.mainTabBarController)
|
2021-11-01 13:09:30 +01:00
|
|
|
|
2022-05-06 08:29:34 +02:00
|
|
|
let tab = compactMainTabBarViewController.currentTab
|
2021-11-01 13:09:30 +01:00
|
|
|
if tab == .search {
|
|
|
|
contentSplitViewController.currentSupplementaryTab = .home
|
|
|
|
} else {
|
2022-05-06 08:29:34 +02:00
|
|
|
contentSplitViewController.currentSupplementaryTab = compactMainTabBarViewController.currentTab
|
2021-11-01 13:09:30 +01:00
|
|
|
}
|
2021-10-28 13:17:41 +02:00
|
|
|
|
2021-10-29 08:58:59 +02:00
|
|
|
return proposedDisplayMode
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-11-02 12:15:46 +01:00
|
|
|
|
|
|
|
// MARK: - WizardViewControllerDelegate
|
|
|
|
extension RootSplitViewController: WizardViewControllerDelegate {
|
|
|
|
|
|
|
|
func readyToLayoutItem(_ wizardViewController: WizardViewController, item: WizardViewController.Item) -> Bool {
|
|
|
|
guard traitCollection.horizontalSizeClass != .compact else {
|
|
|
|
return compactMainTabBarViewController.readyToLayoutItem(wizardViewController, item: item)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch item {
|
|
|
|
case .multipleAccountSwitch:
|
|
|
|
return contentSplitViewController.sidebarViewController.viewModel.isReadyForWizardAvatarButton
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func layoutSpotlight(_ wizardViewController: WizardViewController, item: WizardViewController.Item) -> UIBezierPath {
|
|
|
|
guard traitCollection.horizontalSizeClass != .compact else {
|
|
|
|
return compactMainTabBarViewController.layoutSpotlight(wizardViewController, item: item)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch item {
|
|
|
|
case .multipleAccountSwitch:
|
|
|
|
guard let frame = avatarButtonFrameInWizardView(wizardView: wizardViewController.view)
|
|
|
|
else {
|
|
|
|
assertionFailure()
|
|
|
|
return UIBezierPath()
|
|
|
|
}
|
|
|
|
return UIBezierPath(ovalIn: frame)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func layoutWizardCard(_ wizardViewController: WizardViewController, item: WizardViewController.Item) {
|
|
|
|
guard traitCollection.horizontalSizeClass != .compact else {
|
|
|
|
return compactMainTabBarViewController.layoutWizardCard(wizardViewController, item: item)
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let frame = avatarButtonFrameInWizardView(wizardView: wizardViewController.view) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let anchorView = UIView()
|
|
|
|
anchorView.frame = frame
|
|
|
|
wizardViewController.backgroundView.addSubview(anchorView)
|
|
|
|
|
|
|
|
let wizardCardView = WizardCardView()
|
|
|
|
wizardCardView.arrowRectCorner = .allCorners // no arrow
|
|
|
|
wizardCardView.titleLabel.text = item.title
|
|
|
|
wizardCardView.descriptionLabel.text = item.description
|
|
|
|
|
|
|
|
wizardCardView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
wizardViewController.backgroundView.addSubview(wizardCardView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
wizardCardView.centerYAnchor.constraint(equalTo: anchorView.centerYAnchor),
|
|
|
|
wizardCardView.leadingAnchor.constraint(equalTo: anchorView.trailingAnchor, constant: 20), // 20pt spacing
|
|
|
|
wizardCardView.widthAnchor.constraint(equalToConstant: 320),
|
|
|
|
])
|
|
|
|
wizardCardView.setContentHuggingPriority(.defaultLow, for: .vertical)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func avatarButtonFrameInWizardView(wizardView: UIView) -> CGRect? {
|
|
|
|
guard let diffableDataSource = contentSplitViewController.sidebarViewController.viewModel.diffableDataSource,
|
|
|
|
let indexPath = diffableDataSource.indexPath(for: .tab(.me)),
|
|
|
|
let cell = contentSplitViewController.sidebarViewController.collectionView.cellForItem(at: indexPath) as? SidebarListCollectionViewCell,
|
|
|
|
let contentView = cell._contentView,
|
|
|
|
let frame = sourceViewFrameInTargetView(
|
2021-11-03 08:29:25 +01:00
|
|
|
sourceView: contentView.avatarButton,
|
|
|
|
targetView: wizardView
|
2021-11-02 12:15:46 +01:00
|
|
|
)
|
|
|
|
else {
|
|
|
|
assertionFailure()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return frame
|
|
|
|
}
|
|
|
|
|
|
|
|
private func sourceViewFrameInTargetView(
|
|
|
|
sourceView: UIView,
|
|
|
|
targetView: UIView
|
|
|
|
) -> CGRect? {
|
|
|
|
guard let superview = sourceView.superview else {
|
|
|
|
assertionFailure()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return superview.convert(sourceView.frame, to: targetView)
|
|
|
|
}
|
|
|
|
}
|