Slather logo

Coverage for "DetailsNavigationFinder.swift" : 100.00%

(23 of 23 relevant lines covered)

RouteComposer/Classes/Extra/DetailsNavigationFinder.swift

1
//
2
// RouteComposer
3
// DetailsNavigationFinder.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
/// `Finder` that helps to find the `UINavigationController` inside of the details of the `UISplitController`
17
public struct DetailsNavigationFinder<C>: Finder {
18
19
    // MARK: Associated types
20
21
    public typealias ViewController = UINavigationController
22
23
    public typealias Context = C
24
25
    // MARK: Properties
26
27
    /// A `StackIterator` is to be used by `ClassFinder`
28
    public let iterator: StackIterator
29
30
    // MARK: Methods
31
32
    /// Constructor
33
    ///
34
    /// - Parameter iterator: A `StackIterator` is to be used by `ClassFinder`
35
    public init(iterator: StackIterator = RouteComposerDefaults.shared.stackIterator) {
2x
36
        self.iterator = iterator
2x
37
    }
2x
38
39
    public func findViewController(with context: Context) throws -> ViewController? {
4x
40
        guard let splitViewController = try ClassFinder<UISplitViewController, Context>(iterator: iterator).findViewController(with: context) else {
4x
41
            return nil
1x
42
        }
3x
43
        guard splitViewController.viewControllers.count > 1 else {
3x
44
            guard let firstNavigationController = splitViewController.viewControllers.first as? UINavigationController,
2x
45
                  let secondNavigationController = firstNavigationController.viewControllers.last as? UINavigationController else {
2x
46
                return nil
1x
47
            }
1x
48
            return secondNavigationController
1x
49
        }
2x
50
        return splitViewController.viewControllers.last as? UINavigationController
1x
51
    }
3x
52
53
}
54
55
/// Extension to use `DefaultStackIterator` as default iterator.
56
public extension DetailsNavigationFinder {
57
58
    /// Constructor
59
    ///
60
    /// Parameters
61
    ///   - options: A combination of the `SearchOptions`
62
    ///   - startingPoint: `DefaultStackIterator.StartingPoint` value
63
    ///   - windowProvider: `WindowProvider` instance.
64
    ///   - containerAdapterLocator: A `ContainerAdapterLocator` instance.
65
    init(options: SearchOptions,
66
         startingPoint: DefaultStackIterator.StartingPoint = .topmost,
67
         windowProvider: WindowProvider = RouteComposerDefaults.shared.windowProvider,
68
         containerAdapterLocator: ContainerAdapterLocator = RouteComposerDefaults.shared.containerAdapterLocator) {
2x
69
        let iterator = DefaultStackIterator(options: options,
2x
70
                                            startingPoint: startingPoint,
2x
71
                                            windowProvider: windowProvider,
2x
72
                                            containerAdapterLocator: containerAdapterLocator)
2x
73
        self.init(iterator: iterator)
2x
74
    }
2x
75
76
}