1 |
//
|
|
2 |
// RouteComposer
|
|
3 |
// CompleteFactoryAssembly.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 |
/// Builds a `ContainerFactory` fulfilled with the children `UIViewController` factories.
|
|
17 |
///
|
|
18 |
/// ```swift
|
|
19 |
/// let rootFactory = CompleteFactoryAssembly(factory: TabBarFactory())
|
|
20 |
/// .with(XibFactory<HomeViewController, Any?>, using: UITabBarController.add())
|
|
21 |
/// .with(XibFactory<AccountViewController, Any?>, using: UITabBarController.add())
|
|
22 |
/// .assemble()
|
|
23 |
/// ```
|
|
24 |
/// *NB: Order matters here*
|
|
25 |
public final class CompleteFactoryAssembly<FC: ContainerFactory> {
|
|
26 |
|
|
27 |
// MARK: Internal entities
|
|
28 |
|
|
29 |
struct SimpleAddAction<FC: ContainerFactory>: ContainerAction {
|
|
30 |
|
|
31 |
func perform(with viewController: UIViewController, on existingController: FC.ViewController, animated: Bool, completion: @escaping (RoutingResult) -> Void) {
|
! |
32 |
assertionFailure("Should never be called")
|
! |
33 |
completion(.success)
|
! |
34 |
}
|
! |
35 |
|
|
36 |
func perform(embedding viewController: UIViewController, in childViewControllers: inout [UIViewController]) {
|
13x |
37 |
childViewControllers.append(viewController)
|
13x |
38 |
}
|
13x |
39 |
|
|
40 |
}
|
|
41 |
|
|
42 |
// MARK: Properties
|
|
43 |
|
|
44 |
private final var factory: FC
|
|
45 |
|
|
46 |
// MARK: Methods
|
|
47 |
|
|
48 |
/// Constructor
|
|
49 |
///
|
|
50 |
/// - Parameters:
|
|
51 |
/// - factory: The `ContainerFactory` instance.
|
|
52 |
public init(factory: FC) {
|
13x |
53 |
self.factory = factory
|
13x |
54 |
}
|
13x |
55 |
|
|
56 |
/// Adds a `Factory` that is going to be used as a child
|
|
57 |
///
|
|
58 |
/// - Parameters:
|
|
59 |
/// - childFactory: The instance of `Factory`.
|
|
60 |
/// - action: The instance of `Factory` to be used to integrate the view controller produced by the factory.
|
|
61 |
public final func with<ChildFC: Factory, A: ContainerAction, T: ContextTransformer>(_ childFactory: ChildFC, using action: A, adapting transformer: T) -> CompleteFactoryChainAssembly<FC, ChildFC.ViewController, ChildFC.Context>
|
|
62 |
where
|
|
63 |
T.TargetContext == ChildFC.Context, T.SourceContext == FC.Context, A.ViewController == FC.ViewController {
|
8x |
64 |
guard let factoryBox = FactoryBox(childFactory, action: ContainerActionBox(action)) else {
|
8x |
65 |
return CompleteFactoryChainAssembly<FC, ChildFC.ViewController, ChildFC.Context>(factory: factory, childFactories: [], previousChildFactory: nil)
|
1x |
66 |
}
|
7x |
67 |
return CompleteFactoryChainAssembly<FC, ChildFC.ViewController, ChildFC.Context>(factory: factory,
|
7x |
68 |
childFactories: [],
|
7x |
69 |
previousChildFactory: PostponedIntegrationFactory(for: factoryBox, transformer: ContextTransformerBox(transformer)))
|
7x |
70 |
}
|
8x |
71 |
|
|
72 |
/// Adds a `ContainerFactory` that is going to be used as a child
|
|
73 |
///
|
|
74 |
/// - Parameters:
|
|
75 |
/// - childContainer: The instance of `ContainerFactory`.
|
|
76 |
/// - action: The instance of `ContainerFactory` to be used to integrate the view controller produced by the factory.
|
|
77 |
public final func with<ChildFC: ContainerFactory, A: ContainerAction, T: ContextTransformer>(_ childContainer: ChildFC, using action: A, adapting transformer: T) -> CompleteFactoryChainAssembly<FC, ChildFC.ViewController, ChildFC.Context>
|
|
78 |
where
|
|
79 |
T.TargetContext == ChildFC.Context, T.SourceContext == FC.Context, A.ViewController == FC.ViewController {
|
4x |
80 |
guard let factoryBox = ContainerFactoryBox(childContainer, action: ContainerActionBox(action)) else {
|
4x |
81 |
return CompleteFactoryChainAssembly<FC, ChildFC.ViewController, ChildFC.Context>(factory: factory, childFactories: [], previousChildFactory: nil)
|
1x |
82 |
}
|
3x |
83 |
|
3x |
84 |
return CompleteFactoryChainAssembly<FC, ChildFC.ViewController, ChildFC.Context>(factory: factory,
|
3x |
85 |
childFactories: [],
|
3x |
86 |
previousChildFactory: PostponedIntegrationFactory(for: factoryBox, transformer: ContextTransformerBox(transformer)))
|
3x |
87 |
}
|
4x |
88 |
|
|
89 |
/// Adds a `Factory` as the last view controller in the stack.
|
|
90 |
///
|
|
91 |
/// - Parameters:
|
|
92 |
/// - childFactory: The instance of `Factory`.
|
|
93 |
/// - transformer: The instance of `ContextTransformer` to use to adapt parent `ContainerFactory` `Context`.
|
|
94 |
public final func with<ChildFC: Factory, T: ContextTransformer>(_ childFactory: ChildFC, adapting transformer: T) -> CompleteFactoryChainAssembly<FC, ChildFC.ViewController, ChildFC.Context> where T.TargetContext == ChildFC.Context, T.SourceContext == FC.Context {
|
2x |
95 |
return with(childFactory, using: SimpleAddAction<FC>(), adapting: transformer)
|
2x |
96 |
}
|
2x |
97 |
|
|
98 |
/// Adds a `ContainerFactory` as the last view controller in the stack.
|
|
99 |
///
|
|
100 |
/// - Parameters:
|
|
101 |
/// - childContainer: The instance of `ContainerFactory`.
|
|
102 |
/// - transformer: The instance of `ContextTransformer` to use to adapt parent `ContainerFactory` `Context`.
|
|
103 |
public final func with<ChildFC: ContainerFactory, T: ContextTransformer>(_ childContainer: ChildFC, adapting transformer: T) -> CompleteFactoryChainAssembly<FC, ChildFC.ViewController, ChildFC.Context> where T.TargetContext == ChildFC.Context, T.SourceContext == FC.Context {
|
2x |
104 |
return with(childContainer, using: SimpleAddAction<FC>(), adapting: transformer)
|
2x |
105 |
}
|
2x |
106 |
|
|
107 |
/// Adds a `Factory` that is going to be used as a child
|
|
108 |
///
|
|
109 |
/// - Parameters:
|
|
110 |
/// - childFactory: The instance of `Factory`.
|
|
111 |
/// - action: The instance of `Factory` to be used to integrate the view controller produced by the factory.
|
|
112 |
public final func with<ChildFC: Factory, A: ContainerAction>(_ childFactory: ChildFC, using action: A) -> CompleteFactoryChainAssembly<FC, ChildFC.ViewController, ChildFC.Context>
|
|
113 |
where
|
|
114 |
ChildFC.Context == FC.Context, A.ViewController == FC.ViewController {
|
6x |
115 |
return with(childFactory, using: SimpleAddAction<FC>(), adapting: NilContextTransformer())
|
6x |
116 |
}
|
6x |
117 |
|
|
118 |
/// Adds a `ContainerFactory` that is going to be used as a child
|
|
119 |
///
|
|
120 |
/// - Parameters:
|
|
121 |
/// - childContainer: The instance of `ContainerFactory`.
|
|
122 |
/// - action: The instance of `ContainerFactory` to be used to integrate the view controller produced by the factory.
|
|
123 |
public final func with<ChildFC: ContainerFactory, A: ContainerAction>(_ childContainer: ChildFC, using action: A) -> CompleteFactoryChainAssembly<FC, ChildFC.ViewController, ChildFC.Context>
|
|
124 |
where
|
|
125 |
ChildFC.Context == FC.Context, A.ViewController == FC.ViewController {
|
2x |
126 |
return with(childContainer, using: SimpleAddAction<FC>(), adapting: NilContextTransformer())
|
2x |
127 |
}
|
2x |
128 |
|
|
129 |
/// Adds a `Factory` as the last view controller in the stack.
|
|
130 |
///
|
|
131 |
/// - Parameters:
|
|
132 |
/// - childFactory: The instance of `Factory`.
|
|
133 |
public final func with<ChildFC: Factory>(_ childFactory: ChildFC) -> CompleteFactoryChainAssembly<FC, ChildFC.ViewController, ChildFC.Context> where ChildFC.Context == FC.Context {
|
5x |
134 |
return with(childFactory, using: CompleteFactoryAssembly<FC>.SimpleAddAction<FC>())
|
5x |
135 |
}
|
5x |
136 |
|
|
137 |
/// Adds a `ContainerFactory` as the last view controller in the stack.
|
|
138 |
///
|
|
139 |
/// - Parameters:
|
|
140 |
/// - childContainer: The instance of `ContainerFactory`.
|
|
141 |
public final func with<ChildFC: ContainerFactory>(_ childContainer: ChildFC) -> CompleteFactoryChainAssembly<FC, ChildFC.ViewController, ChildFC.Context> where ChildFC.Context == FC.Context {
|
2x |
142 |
return with(childContainer, using: CompleteFactoryAssembly<FC>.SimpleAddAction<FC>())
|
2x |
143 |
}
|
2x |
144 |
|
|
145 |
/// Assembles all the children factories provided and returns a `ContainerFactory` instance.
|
|
146 |
///
|
|
147 |
/// - Returns: The `CompleteFactory` with child factories provided.
|
|
148 |
public final func assemble() -> CompleteFactory<FC> {
|
1x |
149 |
CompleteFactory<FC>(factory: factory, childFactories: [])
|
1x |
150 |
}
|
1x |
151 |
|
|
152 |
}
|
|