| 1 |
//
|
|
| 2 |
// RouteComposer
|
|
| 3 |
// DefaultContainerAdapterLocator.swift
|
|
| 4 |
// https://github.com/ekazaev/route-composer
|
|
| 5 |
//
|
|
| 6 |
// Created by Eugene Kazaev in 2018-2022.
|
|
| 7 |
// Distributed under the MIT license.
|
|
| 8 |
//
|
|
| 9 |
// Become a sponsor:
|
|
| 10 |
// https://github.com/sponsors/ekazaev
|
|
| 11 |
//
|
|
| 12 |
|
|
| 13 |
import Foundation
|
|
| 14 |
import UIKit
|
|
| 15 |
|
|
| 16 |
/// Default implementation of `ContainerAdapterLocator`
|
|
| 17 |
public struct DefaultContainerAdapterLocator: ContainerAdapterLocator {
|
|
| 18 |
|
|
| 19 |
// MARK: Methods
|
|
| 20 |
|
|
| 21 |
/// Constructor
|
|
| 22 |
public init() {}
|
39x |
| 23 |
|
|
| 24 |
/// Returns the `ContainerAdapter` suitable for the `ContainerViewController`.
|
|
| 25 |
///
|
|
| 26 |
/// For the container view controllers that extend `CustomContainerViewController` it returns the an instance provided
|
|
| 27 |
/// in `CustomContainerViewController.adapter` property.
|
|
| 28 |
/// For the default `ContainerViewController`s like `UINavigationController`, `TabBarControllerAdapter`, `UISplitViewController`
|
|
| 29 |
/// and their subclasses it returns suitable default implementation of the `ContainerAdapter`.
|
|
| 30 |
///
|
|
| 31 |
/// - Parameter containerViewController: The `ContainerViewController` instance
|
|
| 32 |
/// - Returns: Suitable `ContainerAdapter` instance
|
|
| 33 |
/// - Throws: `RoutingError` if the suitable `ContainerAdapter` can not be provided
|
|
| 34 |
public func getAdapter(for containerViewController: ContainerViewController) throws -> ContainerAdapter {
|
844x |
| 35 |
guard let customContainerController = containerViewController as? CustomContainerViewController else {
|
844x |
| 36 |
return try getDefaultAdapter(for: containerViewController)
|
836x |
| 37 |
}
|
836x |
| 38 |
return customContainerController.adapter
|
8x |
| 39 |
}
|
844x |
| 40 |
|
|
| 41 |
func getDefaultAdapter(for containerViewController: ContainerViewController) throws -> ContainerAdapter {
|
836x |
| 42 |
switch containerViewController {
|
836x |
| 43 |
case let navigationController as UINavigationController:
|
836x |
| 44 |
return NavigationControllerAdapter(with: navigationController)
|
437x |
| 45 |
case let tabBarController as UITabBarController:
|
836x |
| 46 |
return TabBarControllerAdapter(with: tabBarController)
|
359x |
| 47 |
case let splitController as UISplitViewController:
|
836x |
| 48 |
return SplitControllerAdapter(with: splitController)
|
40x |
| 49 |
default:
|
836x |
| 50 |
let message = "Container adapter for \(String(describing: type(of: containerViewController))) not found"
|
! |
| 51 |
assertionFailure(message)
|
! |
| 52 |
throw RoutingError.compositionFailed(.init(message))
|
! |
| 53 |
}
|
836x |
| 54 |
}
|
836x |
| 55 |
|
|
| 56 |
}
|
|