mastodon-app-ufficiale-ipho.../Mastodon/Scene/Root/RootSplitViewController.swift

193 lines
7.1 KiB
Swift
Raw Normal View History

2021-09-22 13:08:09 +02:00
//
// RootSplitViewController.swift
// Mastodon
//
// Created by Cirno MainasuK on 2021-9-22.
//
import os.log
import UIKit
import Combine
import CoreDataStack
2021-09-22 13:08:09 +02:00
final class RootSplitViewController: UISplitViewController, NeedsDependency {
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-10-28 13:17:41 +02:00
private(set) lazy var contentSplitViewController: ContentSplitViewController = {
let contentSplitViewController = ContentSplitViewController()
contentSplitViewController.context = context
contentSplitViewController.coordinator = coordinator
return contentSplitViewController
}()
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
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-28 13:17:41 +02:00
setViewController(UIViewController(), for: .primary)
setViewController(contentSplitViewController, for: .secondary)
contentSplitViewController.sidebarViewController.view.layer.zPosition = 100
contentSplitViewController.mainTabBarController.view.layer.zPosition = 90
view.layer.zPosition = 80
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-28 13:17:41 +02:00
contentSplitViewController.$currentSupplementaryTab
.receive(on: DispatchQueue.main)
2021-10-28 13:17:41 +02:00
.sink { [weak self] _ in
guard let self = self else { return }
2021-10-28 13:17:41 +02:00
self.updateBehavior(size: self.view.frame.size)
}
.store(in: &disposeBag)
2021-09-22 13:08:09 +02:00
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
2021-10-28 13:17:41 +02:00
coordinator.animate { [weak self] context in
guard let self = self else { return }
self.updateBehavior(size: size)
} completion: { context in
// do nothing
2021-09-22 13:08:09 +02:00
}
}
2021-10-28 13:17:41 +02:00
private func updateBehavior(size: CGSize) {
switch contentSplitViewController.currentSupplementaryTab {
case .search:
hide(.primary)
default:
if size.width > 960 {
show(.primary)
} else {
2021-10-28 13:17:41 +02:00
hide(.primary)
}
}
}
2021-10-28 13:17:41 +02:00
}
// MARK: - UISplitViewControllerDelegate
extension RootSplitViewController: UISplitViewControllerDelegate {
2021-10-28 13:17:41 +02:00
// // .regular to .compact
// // move navigation stack from .supplementary & .secondary to .compact
// func splitViewController(
// _ svc: UISplitViewController,
// topColumnForCollapsingToProposedTopColumn proposedTopColumn: UISplitViewController.Column
// ) -> UISplitViewController.Column {
// switch proposedTopColumn {
// case .compact:
// guard let index = MainTabBarController.Tab.allCases.firstIndex(of: currentSupplementaryTab) else {
// assertionFailure()
// break
// }
// mainTabBarController.selectedIndex = index
// mainTabBarController.currentTab.value = currentSupplementaryTab
//
// guard let navigationController = mainTabBarController.selectedViewController as? UINavigationController else { break }
// navigationController.popToRootViewController(animated: false)
// var viewControllers = navigationController.viewControllers // init navigation stack with topMost
//
// if let supplementaryNavigationController = viewController(for: .supplementary) as? UINavigationController {
// // append supplementary
// viewControllers.append(contentsOf: supplementaryNavigationController.popToRootViewController(animated: true) ?? [])
// }
// if let secondaryNavigationController = viewController(for: .secondary) as? UINavigationController {
// // append secondary
// viewControllers.append(contentsOf: secondaryNavigationController.popToRootViewController(animated: true) ?? [])
// }
// // set navigation stack
// navigationController.setViewControllers(viewControllers, animated: false)
//
// default:
// assertionFailure()
// }
//
// return proposedTopColumn
// }
//
// // .compact to .regular
// // restore navigation stack to .supplementary & .secondary
// func splitViewController(
// _ svc: UISplitViewController,
// displayModeForExpandingToProposedDisplayMode proposedDisplayMode: UISplitViewController.DisplayMode
// ) -> UISplitViewController.DisplayMode {
// let compactNavigationController = mainTabBarController.selectedViewController as? UINavigationController
//
// if let topMost = compactNavigationController?.topMost,
// topMost is AccountListViewController {
// topMost.dismiss(animated: false, completion: nil)
// }
//
// let viewControllers = compactNavigationController?.popToRootViewController(animated: true) ?? []
//
// var supplementaryViewControllers: [UIViewController] = []
// var secondaryViewControllers: [UIViewController] = []
// for viewController in viewControllers {
// if coordinator.secondaryStackHashValues.contains(viewController.hashValue) {
// secondaryViewControllers.append(viewController)
// } else {
// supplementaryViewControllers.append(viewController)
// }
//
// }
// if let supplementary = viewController(for: .supplementary) as? UINavigationController {
// supplementary.setViewControllers(supplementary.viewControllers + supplementaryViewControllers, animated: false)
// }
// if let secondaryNavigationController = viewController(for: .secondary) as? UINavigationController {
// secondaryNavigationController.setViewControllers(secondaryNavigationController.viewControllers + secondaryViewControllers, animated: false)
// }
//
// return proposedDisplayMode
// }
2021-09-22 13:08:09 +02:00
}
2021-10-28 13:17:41 +02:00
//extension UIView {
// func setNeedsLayoutForSubviews() {
// self.subviews.forEach({
// $0.setNeedsLayout()
// $0.setNeedsLayoutForSubviews()
// })
// }
//}