feat: coordinator sidebar search tab to trigger searching

This commit is contained in:
CMK 2021-11-01 20:09:30 +08:00
parent f0a570ea0c
commit 14236c27b8
2 changed files with 45 additions and 13 deletions

View File

@ -10,6 +10,10 @@ import UIKit
import Combine
import CoreDataStack
protocol ContentSplitViewControllerDelegate: AnyObject {
func contentSplitViewController(_ contentSplitViewController: ContentSplitViewController, sidebarViewController: SidebarViewController, didSelectTab tab: MainTabBarController.Tab)
}
final class ContentSplitViewController: UIViewController, NeedsDependency {
var disposeBag = Set<AnyCancellable>()
@ -19,6 +23,8 @@ final class ContentSplitViewController: UIViewController, NeedsDependency {
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
weak var delegate: ContentSplitViewControllerDelegate?
private(set) lazy var sidebarViewController: SidebarViewController = {
let sidebarViewController = SidebarViewController()
sidebarViewController.context = context
@ -87,18 +93,7 @@ extension ContentSplitViewController {
extension ContentSplitViewController: SidebarViewControllerDelegate {
func sidebarViewController(_ sidebarViewController: SidebarViewController, didSelectTab tab: MainTabBarController.Tab) {
guard let _ = MainTabBarController.Tab.allCases.firstIndex(of: tab) else {
assertionFailure()
return
}
let previousTab = currentSupplementaryTab
currentSupplementaryTab = tab
if previousTab == tab,
let navigationController = mainTabBarController.selectedViewController as? UINavigationController
{
navigationController.popToRootViewController(animated: true)
}
delegate?.contentSplitViewController(self, sidebarViewController: sidebarViewController, didSelectTab: tab)
}
func sidebarViewController(_ sidebarViewController: SidebarViewController, didLongPressItem item: SidebarViewModel.Item, sourceView: UIView) {

View File

@ -23,6 +23,7 @@ final class RootSplitViewController: UISplitViewController, NeedsDependency {
let contentSplitViewController = ContentSplitViewController()
contentSplitViewController.context = context
contentSplitViewController.coordinator = coordinator
contentSplitViewController.delegate = self
return contentSplitViewController
}()
@ -131,6 +132,36 @@ extension RootSplitViewController {
}
// 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 {
case .search:
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)
}
}
}
}
// MARK: - UISplitViewControllerDelegate
extension RootSplitViewController: UISplitViewControllerDelegate {
@ -180,7 +211,13 @@ extension RootSplitViewController: UISplitViewControllerDelegate {
}
RootSplitViewController.transform(from: compactMainTabBarViewController, to: contentSplitViewController.mainTabBarController)
contentSplitViewController.currentSupplementaryTab = compactMainTabBarViewController.currentTab.value
let tab = compactMainTabBarViewController.currentTab.value
if tab == .search {
contentSplitViewController.currentSupplementaryTab = .home
} else {
contentSplitViewController.currentSupplementaryTab = compactMainTabBarViewController.currentTab.value
}
return proposedDisplayMode
}