| 1 |
//
|
|
| 2 |
// RouteComposer
|
|
| 3 |
// InlineInterceptor.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 |
/// `InlineInterceptor`
|
|
| 17 |
///
|
|
| 18 |
/// **NB:** It may be used for the purpose of configuration testing, but then replaced with a strongly typed
|
|
| 19 |
/// `RoutingInterceptor` instance.
|
|
| 20 |
public struct InlineInterceptor<C>: RoutingInterceptor {
|
|
| 21 |
|
|
| 22 |
// MARK: Properties
|
|
| 23 |
|
|
| 24 |
private let prepareBlock: ((_: C) throws -> Void)?
|
|
| 25 |
|
|
| 26 |
private let performBlock: (_: C, _: @escaping (RoutingResult) -> Void) -> Void
|
|
| 27 |
|
|
| 28 |
// MARK: Methods
|
|
| 29 |
|
|
| 30 |
/// Constructor
|
|
| 31 |
///
|
|
| 32 |
/// - Parameter completion: the block to be called when `InlineInterceptor` will take a control over the navigation process.
|
|
| 33 |
///
|
|
| 34 |
/// **NB** For `Router` to be able to continue navigation process, completion block method **MUST** be called.
|
|
| 35 |
public init(prepare prepareBlock: ((_: C) throws -> Void)? = nil, _ performBlock: @escaping (_: C, _: @escaping (RoutingResult) -> Void) -> Void) {
|
3x |
| 36 |
self.prepareBlock = prepareBlock
|
3x |
| 37 |
self.performBlock = performBlock
|
3x |
| 38 |
}
|
3x |
| 39 |
|
|
| 40 |
/// Constructor
|
|
| 41 |
///
|
|
| 42 |
/// - Parameter completion: the block to be called when `InlineInterceptor` will take a control over the navigation process.
|
|
| 43 |
///
|
|
| 44 |
/// **NB** completion method will be called automatically, do not use this constructor if your interceptor
|
|
| 45 |
/// task is asynchronous.
|
|
| 46 |
public init(prepare prepareBlock: ((_: C) throws -> Void)? = nil, _ inlineBlock: @escaping (_: C) throws -> Void) {
|
14x |
| 47 |
self.prepareBlock = prepareBlock
|
14x |
| 48 |
self.performBlock = { context, completion in
|
14x |
| 49 |
do {
|
12x |
| 50 |
try inlineBlock(context)
|
12x |
| 51 |
completion(.success)
|
12x |
| 52 |
} catch {
|
12x |
| 53 |
completion(.failure(error))
|
1x |
| 54 |
}
|
12x |
| 55 |
}
|
12x |
| 56 |
}
|
14x |
| 57 |
|
|
| 58 |
public func prepare(with context: C) throws {
|
18x |
| 59 |
try prepareBlock?(context)
|
18x |
| 60 |
}
|
18x |
| 61 |
|
|
| 62 |
public func perform(with context: C, completion: @escaping (RoutingResult) -> Void) {
|
15x |
| 63 |
performBlock(context, completion)
|
15x |
| 64 |
}
|
15x |
| 65 |
|
|
| 66 |
}
|
|