1 |
//
|
|
2 |
// RouteComposer
|
|
3 |
// SingleStep.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 |
/// A simple class that produces an intermediate `ActionToStepIntegrator` describing any view controller.
|
|
17 |
public final class SingleStep<F: Finder, FC: Factory>: ActionToStepIntegrator<F.ViewController, F.Context>
|
|
18 |
where
|
|
19 |
F.ViewController == FC.ViewController, F.Context == FC.Context {
|
|
20 |
|
|
21 |
// MARK: Internal entities
|
|
22 |
|
|
23 |
final class UnsafeWrapper<VC: UIViewController, C, F: Finder, FC: Factory>: ActionToStepIntegrator<VC, C>
|
|
24 |
where
|
|
25 |
F.ViewController == FC.ViewController, F.Context == FC.Context {
|
|
26 |
|
|
27 |
final let step: SingleStep<F, FC>
|
|
28 |
|
|
29 |
init(step: SingleStep<F, FC>) {
|
12x |
30 |
self.step = step
|
12x |
31 |
super.init(taskCollector: step.taskCollector)
|
12x |
32 |
}
|
12x |
33 |
|
|
34 |
final override func routingStep(with action: some Action) -> RoutingStep? {
|
15x |
35 |
step.routingStep(with: action)
|
15x |
36 |
}
|
15x |
37 |
|
|
38 |
final override func embeddableRoutingStep(with action: some ContainerAction) -> RoutingStep? {
|
2x |
39 |
step.embeddableRoutingStep(with: action)
|
2x |
40 |
}
|
2x |
41 |
|
|
42 |
}
|
|
43 |
|
|
44 |
// MARK: Properties
|
|
45 |
|
|
46 |
final let finder: F
|
|
47 |
|
|
48 |
final let factory: FC
|
|
49 |
|
|
50 |
// MARK: Methods
|
|
51 |
|
|
52 |
/// A simple class that produces an intermediate `ActionToStepIntegrator`.
|
|
53 |
///
|
|
54 |
/// - Parameters:
|
|
55 |
/// - finder: The `UIViewController` `Finder`.
|
|
56 |
/// - factory: The `UIViewController` `Factory`.
|
|
57 |
public init(finder: F, factory: FC) {
|
17x |
58 |
self.finder = finder
|
17x |
59 |
self.factory = factory
|
17x |
60 |
}
|
17x |
61 |
|
|
62 |
final override func routingStep(with action: some Action) -> RoutingStep {
|
23x |
63 |
let entitiesCollector = BaseEntitiesCollector<FactoryBox<FC>, ActionBox>(finder: finder, factory: factory, action: action)
|
23x |
64 |
return BaseStep(entitiesProvider: entitiesCollector, taskProvider: taskCollector)
|
23x |
65 |
}
|
23x |
66 |
|
|
67 |
final override func embeddableRoutingStep(with action: some ContainerAction) -> RoutingStep {
|
5x |
68 |
let entitiesCollector = BaseEntitiesCollector<FactoryBox<FC>, ContainerActionBox>(finder: finder, factory: factory, action: action)
|
5x |
69 |
return BaseStep(entitiesProvider: entitiesCollector, taskProvider: taskCollector)
|
5x |
70 |
}
|
5x |
71 |
|
|
72 |
/// Adapts context and view controller type dependencies.
|
|
73 |
///
|
|
74 |
/// *NB:* Developer guaranties that this types will compliment in runtime.
|
|
75 |
public final func unsafelyRewrapped<VC: UIViewController, C>() -> ActionToStepIntegrator<VC, C> {
|
2x |
76 |
UnsafeWrapper(step: self)
|
2x |
77 |
}
|
2x |
78 |
|
|
79 |
/// Allows to avoid container view controller check.
|
|
80 |
///
|
|
81 |
/// *NB:* Developer guaranties that it will be there in the runtime.
|
|
82 |
public final func expectingContainer<VC: ContainerViewController>() -> ActionToStepIntegrator<VC, F.Context> {
|
3x |
83 |
UnsafeWrapper(step: self)
|
3x |
84 |
}
|
3x |
85 |
|
|
86 |
}
|
|
87 |
|
|
88 |
// MARK: Helper methods where the Context is Any?
|
|
89 |
|
|
90 |
public extension SingleStep where FC.Context == Any? {
|
|
91 |
|
|
92 |
/// Allows to avoid container view controller check. This method is available only for the steps that are
|
|
93 |
/// able to accept any type of context.
|
|
94 |
///
|
|
95 |
/// *NB:* Developer guaranties that it will be there in the runtime.
|
|
96 |
final func expectingContainer<VC: ContainerViewController, C>() -> ActionToStepIntegrator<VC, C> {
|
4x |
97 |
UnsafeWrapper(step: self)
|
4x |
98 |
}
|
4x |
99 |
|
|
100 |
/// Allows to compliment to the type check. A step that has context equal to Optional(Any) can be build
|
|
101 |
/// with any type of context passed to the router.
|
|
102 |
final func adaptingContext<C>() -> ActionToStepIntegrator<F.ViewController, C> {
|
3x |
103 |
UnsafeWrapper(step: self)
|
3x |
104 |
}
|
3x |
105 |
|
|
106 |
}
|
|