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