| 1 |
//
|
|
| 2 |
// RouteComposer
|
|
| 3 |
// TabBarControllerFactory.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 |
/// The `ContainerFactory` that creates a `UITabBarController` instance.
|
|
| 17 |
public struct TabBarControllerFactory<VC: UITabBarController, C>: ContainerFactory {
|
|
| 18 |
|
|
| 19 |
// MARK: Associated types
|
|
| 20 |
|
|
| 21 |
public typealias ViewController = VC
|
|
| 22 |
|
|
| 23 |
public typealias Context = C
|
|
| 24 |
|
|
| 25 |
// MARK: Properties
|
|
| 26 |
|
|
| 27 |
/// A Xib file name
|
|
| 28 |
public let nibName: String?
|
|
| 29 |
|
|
| 30 |
/// A `Bundle` instance
|
|
| 31 |
public let bundle: Bundle?
|
|
| 32 |
|
|
| 33 |
/// `UITabBarControllerDelegate` reference
|
|
| 34 |
public private(set) weak var delegate: UITabBarControllerDelegate?
|
|
| 35 |
|
|
| 36 |
/// The additional configuration block
|
|
| 37 |
public let configuration: ((_: VC) -> Void)?
|
|
| 38 |
|
|
| 39 |
// MARK: Methods
|
|
| 40 |
|
|
| 41 |
/// Constructor
|
|
| 42 |
public init(nibName nibNameOrNil: String? = nil,
|
|
| 43 |
bundle nibBundleOrNil: Bundle? = nil,
|
|
| 44 |
delegate: UITabBarControllerDelegate? = nil,
|
|
| 45 |
configuration: ((_: VC) -> Void)? = nil) {
|
22x |
| 46 |
self.nibName = nibNameOrNil
|
22x |
| 47 |
self.bundle = nibBundleOrNil
|
22x |
| 48 |
self.delegate = delegate
|
22x |
| 49 |
self.configuration = configuration
|
22x |
| 50 |
}
|
22x |
| 51 |
|
|
| 52 |
public func build(with context: C, integrating coordinator: ChildCoordinator) throws -> VC {
|
15x |
| 53 |
let tabBarController = VC(nibName: nibName, bundle: bundle)
|
15x |
| 54 |
if let delegate {
|
15x |
| 55 |
tabBarController.delegate = delegate
|
1x |
| 56 |
}
|
15x |
| 57 |
if !coordinator.isEmpty {
|
15x |
| 58 |
tabBarController.viewControllers = try coordinator.build(integrating: tabBarController.viewControllers ?? [])
|
15x |
| 59 |
}
|
15x |
| 60 |
if let configuration {
|
15x |
| 61 |
configuration(tabBarController)
|
1x |
| 62 |
}
|
15x |
| 63 |
return tabBarController
|
15x |
| 64 |
}
|
15x |
| 65 |
|
|
| 66 |
}
|
|