Slather logo

Coverage for "CompleteFactory.swift" : 100.00%

(20 of 20 relevant lines covered)

RouteComposer/Classes/Factories/CompleteFactory.swift

1
//
2
// RouteComposer
3
// CompleteFactory.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
/// The `CompleteFactory` instance is used by the `CompleteFactoryAssembly` as a `ContainerFactory` to
17
/// pre-populate the children view controllers instead of the `Router`.
18
public struct CompleteFactory<FC: ContainerFactory>: ContainerFactory, CustomStringConvertible {
19
20
    // MARK: Associated types
21
22
    public typealias ViewController = FC.ViewController
23
24
    public typealias Context = FC.Context
25
26
    // MARK: Properties
27
28
    private var factory: FC
29
30
    var childFactories: [PostponedIntegrationFactory]
31
32
    // MARK: Methods
33
34
    init(factory: FC, childFactories: [PostponedIntegrationFactory]) {
17x
35
        self.factory = factory
17x
36
        self.childFactories = childFactories
17x
37
    }
17x
38
39
    public mutating func prepare(with context: FC.Context) throws {
11x
40
        try factory.prepare(with: context)
11x
41
        childFactories = try childFactories.map {
17x
42
            var factory = $0
17x
43
            try factory.prepare(with: AnyContextBox(context))
17x
44
            return factory
17x
45
        }
17x
46
    }
11x
47
48
    public func build(with context: FC.Context, integrating coordinator: ChildCoordinator) throws -> FC.ViewController {
13x
49
        var finalChildFactories: [(factory: PostponedIntegrationFactory, context: AnyContext)] = childFactories.map { (factory: $0, context: AnyContextBox(context)) }
21x
50
        finalChildFactories.append(contentsOf: coordinator.childFactories)
13x
51
        return try factory.build(with: context, integrating: ChildCoordinator(childFactories: finalChildFactories))
13x
52
    }
13x
53
54
    public var description: String {
7x
55
        String(describing: factory)
7x
56
    }
7x
57
58
}