Slather logo

Coverage for "UIHostingControllerFactory.swift" : 100.00%

(7 of 7 relevant lines covered)

RouteComposer/Classes/Factories/SwiftUI/UIHostingControllerFactory.swift

1
//
2
// RouteComposer
3
// UIHostingControllerFactory.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
/// Builds `UIHostingController` with `ContentView` as a `UIHostingController.rootView` using the provided block.
20
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
21
public struct UIHostingControllerFactory<ContentView: View, Context>: Factory {
22
23
    // MARK: Associated types
24
25
    public typealias ViewController = UIHostingController<ContentView>
26
27
    public typealias Context = Context
28
29
    // MARK: Properties
30
31
    private let buildBlock: (Context) -> ContentView
32
33
    // MARK: Methods
34
35
    /// Constructor
36
    /// - Parameter buildBlock: Block that builds the `View` with the using the `Context` instance provided.
37
    public init(_ buildBlock: @escaping (Context) -> ContentView) {
1x
38
        self.buildBlock = buildBlock
1x
39
    }
1x
40
41
    public func build(with context: Context) throws -> UIHostingController<ContentView> {
1x
42
        let viewController = UIHostingController(rootView: buildBlock(context))
1x
43
        return viewController
1x
44
    }
1x
45
46
}
47
48
#endif