Slather logo

Coverage for "Router.swift" : 100.00%

(19 of 19 relevant lines covered)

RouteComposer/Classes/Router.swift

1
//
2
// RouteComposer
3
// Router.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
/// Base router protocol.
17
public protocol Router {
18
19
    // MARK: Methods to implement
20
21
    /// Navigates the application to the view controller configured in `DestinationStep` with the `Context` provided.
22
    ///
23
    /// - Parameters:
24
    ///   - step: `DestinationStep` instance.
25
    ///   - context: `Context` instance.
26
    ///   - animated: if true - the navigation should be animated where it is possible.
27
    ///   - completion: completion block.
28
    func navigate<ViewController: UIViewController, Context>(to step: DestinationStep<ViewController, Context>,
29
                                                             with context: Context,
30
                                                             animated: Bool,
31
                                                             completion: ((_: RoutingResult) -> Void)?) throws
32
33
}
34
35
// MARK: Helper methods
36
37
public extension Router {
38
39
    /// Navigates the application to the view controller configured in `DestinationStep` with the `Context` set to `Any?`.
40
    ///
41
    /// - Parameters:
42
    ///   - step: `DestinationStep` instance.
43
    ///   - animated: if true - the navigation should be animated where it is possible.
44
    ///   - completion: completion block.
45
    func navigate(to step: DestinationStep<some UIViewController, Any?>,
46
                  animated: Bool,
47
                  completion: ((_: RoutingResult) -> Void)?) throws {
3x
48
        try navigate(to: step, with: nil, animated: animated, completion: completion)
3x
49
    }
3x
50
51
    /// Navigates the application to the view controller configured in `DestinationStep` with the `Context` set to `Void`.
52
    ///
53
    /// - Parameters:
54
    ///   - step: `DestinationStep` instance.
55
    ///   - animated: if true - the navigation should be animated where it is possible.
56
    ///   - completion: completion block.
57
    func navigate(to step: DestinationStep<some UIViewController, Void>,
58
                  animated: Bool,
59
                  completion: ((_: RoutingResult) -> Void)?) throws {
3x
60
        try navigate(to: step, with: (), animated: animated, completion: completion)
3x
61
    }
3x
62
63
}
64
65
// MARK: Navigation without the exception throwing
66
67
public extension Router {
68
69
    /// Navigates the application to the view controller configured in `DestinationStep` with the `Context` provided.
70
    /// Method does not throw errors, but propagates them to the completion block
71
    ///
72
    /// - Parameters:
73
    ///   - step: `DestinationStep` instance.
74
    ///   - context: `Context` instance.
75
    ///   - animated: if true - the navigation should be animated where it is possible.
76
    ///   - completion: completion block.
77
    func commitNavigation<Context>(to step: DestinationStep<some UIViewController, Context>,
78
                                   with context: Context,
79
                                   animated: Bool,
80
                                   completion: ((RoutingResult) -> Void)?) {
6x
81
        do {
6x
82
            try navigate(to: step, with: context, animated: animated, completion: completion)
6x
83
        } catch {
6x
84
            completion?(.failure(error))
3x
85
        }
6x
86
    }
6x
87
88
    /// Navigates the application to the view controller configured in `DestinationStep` with the `Context` set to `Any?`.
89
    /// Method does not throw errors, but propagates them to the completion block
90
    ///
91
    /// - Parameters:
92
    ///   - step: `DestinationStep` instance.
93
    ///   - animated: if true - the navigation should be animated where it is possible.
94
    ///   - completion: completion block.
95
    func commitNavigation(to step: DestinationStep<some UIViewController, Any?>,
96
                          animated: Bool,
97
                          completion: ((RoutingResult) -> Void)?) {
1x
98
        commitNavigation(to: step, with: nil, animated: animated, completion: completion)
1x
99
    }
1x
100
101
    /// Navigates the application to the view controller configured in `DestinationStep` with the `Context` set to `Void`.
102
    /// Method does not throw errors, but propagates them to the completion block
103
    ///
104
    /// - Parameters:
105
    ///   - step: `DestinationStep` instance.
106
    ///   - animated: if true - the navigation should be animated where it is possible.
107
    ///   - completion: completion block.
108
    func commitNavigation(to step: DestinationStep<some UIViewController, Void>,
109
                          animated: Bool,
110
                          completion: ((RoutingResult) -> Void)?) {
1x
111
        commitNavigation(to: step, with: (), animated: animated, completion: completion)
1x
112
    }
1x
113
114
}