Slather logo

Coverage for "UIHostingControllerWithContextFinder.swift" : 100.00%

(9 of 9 relevant lines covered)

RouteComposer/Classes/Finders/SwiftUI/UIHostingControllerWithContextFinder.swift

1
//
2
// RouteComposer
3
// UIHostingControllerWithContextFinder.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
#if canImport(SwiftUI)
14
15
import Foundation
16
import SwiftUI
17
import UIKit
18
19
/// A default implementation of the finder, that searches for a `UIHostingController` with a specific `View`
20
/// and its `Context` instance.
21
///
22
/// The `View` should conform to the `ContextChecking` to be used with this finder.
23
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
24
public struct UIHostingControllerWithContextFinder<ContentView: View & ContextChecking>: StackIteratingFinder {
25
26
    // MARK: Associated types
27
28
    public typealias ViewController = UIHostingController<ContentView>
29
30
    public typealias Context = ContentView.Context
31
32
    // MARK: Properties
33
34
    /// A `StackIterator` is to be used by `ClassWithContextFinder`
35
    public let iterator: StackIterator
36
37
    // MARK: Methods
38
39
    /// Constructor
40
    ///
41
    /// - Parameter iterator: A `StackIterator` is to be used by `ClassWithContextFinder`
42
    public init(iterator: StackIterator = RouteComposerDefaults.shared.stackIterator) {
2x
43
        self.iterator = iterator
2x
44
    }
2x
45
46
    public func isTarget(_ viewController: ViewController, with context: Context) -> Bool {
2x
47
        viewController.rootView.isTarget(for: context)
2x
48
    }
2x
49
50
}
51
52
/// Extension to use `DefaultStackIterator` as default iterator.
53
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
54
public extension UIHostingControllerWithContextFinder {
55
56
    /// Constructor
57
    ///
58
    /// Parameters
59
    ///   - options: A combination of the `SearchOptions`
60
    ///   - startingPoint: `DefaultStackIterator.StartingPoint` value
61
    ///   - windowProvider: `WindowProvider` instance.
62
    ///   - containerAdapterLocator: A `ContainerAdapterLocator` instance.
63
    init(options: SearchOptions,
64
         startingPoint: DefaultStackIterator.StartingPoint = .topmost,
65
         windowProvider: WindowProvider = RouteComposerDefaults.shared.windowProvider,
66
         containerAdapterLocator: ContainerAdapterLocator = RouteComposerDefaults.shared.containerAdapterLocator) {
1x
67
        self.init(iterator: DefaultStackIterator(options: options, startingPoint: startingPoint, windowProvider: windowProvider, containerAdapterLocator: containerAdapterLocator))
1x
68
    }
1x
69
70
}
71
72
#endif