| 1 |
//
|
|
| 2 |
// RouteComposer
|
|
| 3 |
// ContextTaskBox.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 ContextTaskBox<CT: ContextTask>: AnyContextTask, PreparableEntity, MainThreadChecking, CustomStringConvertible {
|
|
| 17 |
|
|
| 18 |
var contextTask: CT
|
|
| 19 |
|
|
| 20 |
var isPrepared = false
|
|
| 21 |
|
|
| 22 |
init(_ contextTask: CT) {
|
128x |
| 23 |
self.contextTask = contextTask
|
128x |
| 24 |
}
|
128x |
| 25 |
|
|
| 26 |
mutating func prepare(with context: AnyContext) throws {
|
137x |
| 27 |
let typedContext: CT.Context = try context.value()
|
137x |
| 28 |
try contextTask.prepare(with: typedContext)
|
137x |
| 29 |
isPrepared = true
|
137x |
| 30 |
}
|
137x |
| 31 |
|
|
| 32 |
func perform(on viewController: UIViewController, with context: AnyContext) throws {
|
136x |
| 33 |
guard let typedViewController = viewController as? CT.ViewController else {
|
136x |
| 34 |
throw RoutingError.typeMismatch(type: type(of: context),
|
! |
| 35 |
expectedType: CT.Context.self,
|
! |
| 36 |
.init("\(String(describing: contextTask.self)) does not accept \(String(describing: context.self)) as a context."))
|
! |
| 37 |
}
|
136x |
| 38 |
let typedContext: CT.Context = try context.value()
|
136x |
| 39 |
|
136x |
| 40 |
assertIfNotMainThread()
|
136x |
| 41 |
assertIfNotPrepared()
|
136x |
| 42 |
try contextTask.perform(on: typedViewController, with: typedContext)
|
136x |
| 43 |
}
|
136x |
| 44 |
|
|
| 45 |
var description: String {
|
2x |
| 46 |
String(describing: contextTask)
|
2x |
| 47 |
}
|
2x |
| 48 |
|
|
| 49 |
}
|
|