Slather logo

Coverage for "SingleContainerStep.swift" : 100.00%

(34 of 34 relevant lines covered)

RouteComposer/Classes/Steps/SingleContainerStep.swift

1
//
2
// RouteComposer
3
// SingleContainerStep.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 a container view controller.
17
public class SingleContainerStep<F: Finder, FC: ContainerFactory>: 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: ContainerFactory>: ActionToStepIntegrator<VC, C>
24
        where
25
        F.ViewController == FC.ViewController, F.Context == FC.Context {
26
27
        final let step: SingleContainerStep<F, FC>
28
29
        init(step: SingleContainerStep<F, FC>) {
4x
30
            self.step = step
4x
31
            super.init(taskCollector: step.taskCollector)
4x
32
        }
4x
33
34
        final override func routingStep(with action: some Action) -> RoutingStep? {
4x
35
            step.routingStep(with: action)
4x
36
        }
4x
37
38
        final override func embeddableRoutingStep(with action: some ContainerAction) -> RoutingStep? {
4x
39
            step.embeddableRoutingStep(with: action)
4x
40
        }
4x
41
42
    }
43
44
    // MARK: Properties
45
46
    final let finder: F
47
48
    final let factory: FC
49
50
    // MARK: Methods
51
52
    /// Creates an instance of the `ActionToStepIntegrator` describing a container view controller.
53
    ///
54
    /// - Parameters:
55
    ///   - finder: The `UIViewController` `Finder`.
56
    ///   - factory: The `UIViewController` `ContainerFactory`.
57
    public init(finder: F, factory: FC) {
49x
58
        self.finder = finder
49x
59
        self.factory = factory
49x
60
    }
49x
61
62
    final override func routingStep(with action: some Action) -> RoutingStep {
41x
63
        let entitiesCollector = BaseEntitiesCollector<ContainerFactoryBox<FC>, ActionBox>(finder: finder, factory: factory, action: action)
41x
64
        return BaseStep(entitiesProvider: entitiesCollector, taskProvider: taskCollector)
41x
65
    }
41x
66
67
    final override func embeddableRoutingStep(with action: some ContainerAction) -> RoutingStep {
11x
68
        let entitiesCollector = BaseEntitiesCollector<ContainerFactoryBox<FC>, ContainerActionBox>(finder: finder, factory: factory, action: action)
11x
69
        return BaseStep(entitiesProvider: entitiesCollector, taskProvider: taskCollector)
11x
70
    }
11x
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> {
1x
76
        UnsafeWrapper(step: self)
1x
77
    }
1x
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> {
1x
83
        UnsafeWrapper(step: self)
1x
84
    }
1x
85
86
}
87
88
// MARK: Helper methods where the Context is Any?
89
90
public extension SingleContainerStep 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> {
1x
97
        UnsafeWrapper(step: self)
1x
98
    }
1x
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> {
1x
103
        UnsafeWrapper(step: self)
1x
104
    }
1x
105
106
}