Slather logo

Coverage for "DestinationStep.swift" : 100.00%

(21 of 21 relevant lines covered)

RouteComposer/Classes/DestinationStep.swift

1
//
2
// RouteComposer
3
// DestinationStep.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
/// Represents a single step for the `Router` to make.
17
public struct DestinationStep<VC: UIViewController, C>: RoutingStep, ChainableStep {
18
19
    // MARK: Associated types
20
21
    /// Type of the `ViewController` associated with the step
22
    public typealias ViewController = VC
23
24
    /// Type of the `Context` associated with the step
25
    public typealias Context = C
26
27
    // MARK: Properties
28
29
    let destinationStep: RoutingStep
30
31
    // MARK: Methods
32
33
    init(_ destinationStep: RoutingStep) {
392x
34
        self.destinationStep = destinationStep
392x
35
    }
392x
36
37
    func getPreviousStep(with context: AnyContext) -> RoutingStep? {
351x
38
        destinationStep
351x
39
    }
351x
40
41
    /// Adapts context and view controller type dependencies.
42
    ///
43
    /// *NB:* Developer guaranties that this types will compliment in runtime.
44
    public func unsafelyRewrapped<VC: UIViewController, C>() -> DestinationStep<VC, C> {
2x
45
        DestinationStep<VC, C>(destinationStep)
2x
46
    }
2x
47
48
    /// Transforms context using `ContextTransformer` provided.
49
    public func adaptingContext<T: ContextTransformer>(using contextTransformer: T) -> DestinationStep<VC, T.SourceContext> where T.TargetContext == C {
7x
50
        DestinationStep<VC, T.SourceContext>(ConvertingStep(contextTransformer: contextTransformer, previousStep: destinationStep))
7x
51
    }
7x
52
53
    /// Allows to avoid container view controller check.
54
    ///
55
    /// *NB:* Developer guaranties that it will be there in the runtime.
56
    public func expectingContainer<VC: ContainerViewController>() -> DestinationStep<VC, Context> {
9x
57
        DestinationStep<VC, Context>(destinationStep)
9x
58
    }
9x
59
60
}
61
62
// MARK: Helper methods where the Context is Any?
63
64
/// A step that has a context type Optional(Any) can be build with any type of context passed to the router.
65
public extension DestinationStep where DestinationStep.Context == Any? {
66
67
    /// Allows to avoid container view controller check. This method is available only for the steps that are
68
    /// able to accept any type of context.
69
    ///
70
    /// *NB:* Developer guaranties that it will be there in the runtime.
71
    func expectingContainer<VC: ContainerViewController, C>() -> DestinationStep<VC, C> {
24x
72
        DestinationStep<VC, C>(destinationStep)
24x
73
    }
24x
74
75
    /// Allows to compliment to the type check. A step that has context equal to Optional(Any) can be build
76
    /// with any type of context passed to the router.
77
    func adaptingContext<C>() -> DestinationStep<ViewController, C> {
3x
78
        DestinationStep<ViewController, C>(destinationStep)
3x
79
    }
3x
80
81
}