Slather logo

Coverage for "ClassFactory.swift" : 100.00%

(12 of 12 relevant lines covered)

RouteComposer/Classes/Factories/ClassFactory.swift

1
//
2
// RouteComposer
3
// ClassFactory.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 UIKit
14
15
/// The `Factory` that creates a `UIViewController` instance using its type.
16
public struct ClassFactory<VC: UIViewController, C>: Factory {
17
18
    // MARK: Associated types
19
20
    public typealias ViewController = VC
21
22
    public typealias Context = C
23
24
    // MARK: Properties
25
26
    /// A Xib file name
27
    public let nibName: String?
28
29
    /// A `Bundle` instance
30
    public let bundle: Bundle?
31
32
    /// The additional configuration block
33
    public let configuration: ((_: VC) -> Void)?
34
35
    // MARK: Methods
36
37
    /// Constructor
38
    ///
39
    /// - Parameters:
40
    ///   - nibNameOrNil: A Xib file name
41
    ///   - nibBundleOrNil: A `Bundle` instance if needed
42
    ///   - configuration: A block of code that will be used for the extended configuration of the created `UIViewController`. Can be used for
43
    ///                    a quick configuration instead of `ContextTask`.
44
    public init(nibName nibNameOrNil: String? = nil, bundle nibBundleOrNil: Bundle? = nil, configuration: ((_: VC) -> Void)? = nil) {
23x
45
        self.nibName = nibNameOrNil
23x
46
        self.bundle = nibBundleOrNil
23x
47
        self.configuration = configuration
23x
48
    }
23x
49
50
    public func build(with context: C) throws -> VC {
14x
51
        let viewController = VC(nibName: nibName, bundle: bundle)
14x
52
        if let configuration {
14x
53
            configuration(viewController)
1x
54
        }
14x
55
        return viewController
14x
56
    }
14x
57
58
}