| 1 |
//
|
|
| 2 |
// RouteComposer
|
|
| 3 |
// PostponedIntegrationFactory.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 |
struct PostponedIntegrationFactory: CustomStringConvertible {
|
|
| 17 |
|
|
| 18 |
var factory: AnyFactory
|
|
| 19 |
|
|
| 20 |
var contextTasks: [AnyContextTask]
|
|
| 21 |
|
|
| 22 |
var transformer: AnyContextTransformer?
|
|
| 23 |
|
|
| 24 |
init(for factory: AnyFactory, with contextTasks: [AnyContextTask] = [], transformer: AnyContextTransformer? = nil) {
|
71x |
| 25 |
self.factory = factory
|
71x |
| 26 |
self.contextTasks = contextTasks
|
71x |
| 27 |
self.transformer = transformer
|
71x |
| 28 |
}
|
71x |
| 29 |
|
|
| 30 |
mutating func add(_ contextTask: AnyContextTask) {
|
6x |
| 31 |
contextTasks.append(contextTask)
|
6x |
| 32 |
}
|
6x |
| 33 |
|
|
| 34 |
mutating func prepare(with context: AnyContext) throws {
|
34x |
| 35 |
let context = transformer.flatMap { InPlaceTransformingAnyContext(context: context, transformer: $0) } ?? context
|
34x |
| 36 |
try factory.prepare(with: context)
|
34x |
| 37 |
contextTasks = try contextTasks.map {
|
34x |
| 38 |
var contextTask = $0
|
6x |
| 39 |
try contextTask.prepare(with: context)
|
6x |
| 40 |
return contextTask
|
6x |
| 41 |
}
|
6x |
| 42 |
}
|
34x |
| 43 |
|
|
| 44 |
func build(with context: AnyContext, in childViewControllers: inout [UIViewController]) throws {
|
63x |
| 45 |
let context = transformer.flatMap { InPlaceTransformingAnyContext(context: context, transformer: $0) } ?? context
|
63x |
| 46 |
let viewController = try factory.build(with: context)
|
63x |
| 47 |
try contextTasks.forEach { try $0.perform(on: viewController, with: context) }
|
63x |
| 48 |
try factory.action.perform(embedding: viewController, in: &childViewControllers)
|
63x |
| 49 |
}
|
63x |
| 50 |
|
|
| 51 |
public var description: String {
|
1x |
| 52 |
String(describing: factory)
|
1x |
| 53 |
}
|
1x |
| 54 |
|
|
| 55 |
}
|
|