Slather logo

Coverage for "Destination.swift" : 100.00%

(15 of 15 relevant lines covered)

RouteComposer/Classes/Extra/Destination.swift

1
//
2
// RouteComposer
3
// Destination.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
/// `AnyDestination` represents a generic `Destination` that contains the screen configuration  for any type
17
/// of `UIViewController` and the context of any type.
18
public typealias AnyDestination = Destination<UIViewController, Any?>
19
20
/// `Destination` instance represents both final screen configuration and the data to provide. It is useful when
21
/// there is a need to wrap both values into a single DTO value.
22
public struct Destination<VC: UIViewController, C> {
23
24
    // MARK: Properties
25
26
    /// Final configuration.
27
    public let step: DestinationStep<VC, C>
28
29
    /// Data to be provided to the configuration.
30
    public let context: C
31
32
    // MARK: Methods
33
34
    /// Constructor
35
    ///
36
    /// - Parameters:
37
    ///   - step: `DestinationStep` instance containing the navigation configuration.
38
    ///   - context: `Context` instance to be provided to the configuration.
39
    public init(to step: DestinationStep<VC, C>, with context: C) {
15x
40
        self.step = step
15x
41
        self.context = context
15x
42
    }
15x
43
44
    /// Transforms into generic representation without information about types.
45
    public func unwrapped() -> AnyDestination {
1x
46
        AnyDestination(to: step.unsafelyRewrapped(), with: context)
1x
47
    }
1x
48
49
}
50
51
// MARK: Helper methods where the Context is Any?
52
53
public extension Destination where C == Any? {
54
55
    /// Constructor
56
    ///
57
    /// - Parameters:
58
    ///   - step: `DestinationStep` instance containing the navigation configuration.
59
    init(to step: DestinationStep<VC, C>) {
3x
60
        self.step = step
3x
61
        self.context = nil
3x
62
    }
3x
63
64
}
65
66
// MARK: Helper methods where the Context is Void
67
68
public extension Destination where C == Void {
69
70
    /// Constructor
71
    ///
72
    /// - Parameters:
73
    ///   - step: `DestinationStep` instance containing the navigation configuration.
74
    init(to step: DestinationStep<VC, C>) {
7x
75
        self.step = step
7x
76
        self.context = ()
7x
77
    }
7x
78
79
}