| 1 |
//
|
|
| 2 |
// RouteComposer
|
|
| 3 |
// PresentingFinder.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 |
/// `PresentingFinder` returns the presenting `UIViewController` of the topmost one in current stack.
|
|
| 17 |
public struct PresentingFinder<C>: Finder {
|
|
| 18 |
|
|
| 19 |
// MARK: Internal entities
|
|
| 20 |
|
|
| 21 |
/// A starting point in the `UIViewController`s stack
|
|
| 22 |
///
|
|
| 23 |
/// - topmost: Start from the topmost `UIViewController`
|
|
| 24 |
/// - custom: Start from the custom `UIViewController`
|
|
| 25 |
public enum StartingPoint {
|
|
| 26 |
|
|
| 27 |
/// Start from the topmost `UIViewController`
|
|
| 28 |
case topmost
|
|
| 29 |
|
|
| 30 |
/// Start from the custom `UIViewController`
|
|
| 31 |
case custom(@autoclosure () throws -> UIViewController?)
|
|
| 32 |
|
|
| 33 |
}
|
|
| 34 |
|
|
| 35 |
// MARK: Properties
|
|
| 36 |
|
|
| 37 |
/// `WindowProvider` instance.
|
|
| 38 |
public let windowProvider: WindowProvider
|
|
| 39 |
|
|
| 40 |
/// A starting point in the `UIViewController`s stack
|
|
| 41 |
public let startingPoint: StartingPoint
|
|
| 42 |
|
|
| 43 |
// MARK: Methods
|
|
| 44 |
|
|
| 45 |
/// Constructor
|
|
| 46 |
///
|
|
| 47 |
/// - Parameters:
|
|
| 48 |
/// - windowProvider: `WindowProvider` instance.
|
|
| 49 |
/// - startingPoint: `DefaultStackIterator.StartingPoint` value
|
|
| 50 |
public init(windowProvider: WindowProvider = RouteComposerDefaults.shared.windowProvider,
|
|
| 51 |
startingPoint: StartingPoint = .topmost) {
|
4x |
| 52 |
self.windowProvider = windowProvider
|
4x |
| 53 |
self.startingPoint = startingPoint
|
4x |
| 54 |
}
|
4x |
| 55 |
|
|
| 56 |
public func findViewController(with context: C) throws -> UIViewController? {
|
4x |
| 57 |
try getStartingViewController()?.presentingViewController
|
4x |
| 58 |
}
|
4x |
| 59 |
|
|
| 60 |
func getStartingViewController() throws -> UIViewController? {
|
4x |
| 61 |
switch startingPoint {
|
4x |
| 62 |
case .topmost:
|
4x |
| 63 |
return windowProvider.window?.topmostViewController
|
3x |
| 64 |
case let .custom(viewControllerClosure):
|
4x |
| 65 |
return try viewControllerClosure()
|
1x |
| 66 |
}
|
4x |
| 67 |
}
|
4x |
| 68 |
|
|
| 69 |
}
|
|