Slather logo

Coverage for "Finder.swift" : 100.00%

(19 of 19 relevant lines covered)

RouteComposer/Classes/Finder.swift

1
//
2
// RouteComposer
3
// Finder.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
/// An instance that conforms to the `Finder` protocol will be used by the `Router` to find out if some `UIViewController`
17
/// instance is integrated into the view controller stack
18
public protocol Finder {
19
20
    // MARK: Associated types
21
22
    /// Type of `UIViewController` that `Finder` can find
23
    associatedtype ViewController: UIViewController
24
25
    /// Type of `Context` object that `Finder` can deal with
26
    associatedtype Context
27
28
    // MARK: Methods to implement
29
30
    /// Returns the view controller instance if it is present in the stack.
31
    ///
32
    /// - Parameter context: The `Context` instance passed to the `Router`.
33
    /// - Returns: The `UIViewController` instance that the `Router` is looking for, nil otherwise.
34
    func findViewController(with context: Context) throws -> ViewController?
35
36
}
37
38
// MARK: Helper methods
39
40
public extension Finder {
41
42
    /// Returns the view controller instance if it is present in the stack. Doesn't throw any exceptions in case the search
43
    /// can not be performed.
44
    ///
45
    /// - Parameter context: The `Context` instance passed to the `Router`.
46
    /// - Returns: The `UIViewController` instance that the `Router` is looking for, nil otherwise.
47
    func getViewController(with context: Context) -> ViewController? {
21x
48
        guard let viewController = try? findViewController(with: context) else {
21x
49
            return nil
8x
50
        }
13x
51
13x
52
        return viewController
13x
53
    }
21x
54
55
}
56
57
// MARK: Helper methods where the Context is Any?
58
59
public extension Finder where Context == Any? {
60
61
    /// Returns the view controller instance if it is present in the stack.
62
    ///
63
    /// - Returns: The `UIViewController` instance that the `Router` is looking for, nil otherwise.
64
    func findViewController() throws -> ViewController? {
1x
65
        try findViewController(with: nil)
1x
66
    }
1x
67
68
    /// Returns the view controller instance if it is present in the stack. Doesn't throw any exceptions in case the search
69
    /// can not be performed.
70
    ///
71
    /// - Returns: The `UIViewController` instance that the `Router` is looking for, nil otherwise.
72
    func getViewController() -> ViewController? {
15x
73
        getViewController(with: nil)
15x
74
    }
15x
75
76
}
77
78
// MARK: Helper methods where the Context is Void
79
80
public extension Finder where Context == Void {
81
82
    /// Returns the view controller instance if it is present in the stack.
83
    ///
84
    /// - Returns: The `UIViewController` instance that the `Router` is looking for, nil otherwise.
85
    func findViewController() throws -> ViewController? {
2x
86
        try findViewController(with: ())
2x
87
    }
2x
88
89
    /// Returns the view controller instance if it is present in the stack. Doesn't throw any exceptions in case the search
90
    /// can not be performed.
91
    ///
92
    /// - Returns: The `UIViewController` instance that the `Router` is looking for, nil otherwise.
93
    func getViewController() -> ViewController? {
1x
94
        getViewController(with: ())
1x
95
    }
1x
96
97
}