Slather logo

Coverage for "ContextAcceptingView.swift" : 100.00%

(7 of 7 relevant lines covered)

RouteComposer/Classes/Extra/SwiftUI/ContextAcceptingView.swift

1
//
2
// RouteComposer
3
// ContextAcceptingView.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
#if canImport(SwiftUI)
14
15
import Foundation
16
import SwiftUI
17
import UIKit
18
19
/// The protocol for a `View` to make it compatible with `ContextSettingTask`.
20
///
21
/// *Due to some current `swift` limitations protocol `ContextAccepting` can not be used directly.*
22
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
23
public protocol ContextAcceptingView {
24
25
    // MARK: Associated types
26
27
    /// Type of `Context` object that `View` can be accept
28
    associatedtype Context
29
30
    // MARK: Methods to implement
31
32
    /// If `View` does not support all the permutations that context instance may have -
33
    /// setup the check here.
34
    ///
35
    /// - Parameter context: `Context` instance.
36
    /// - Throws: throws `Error` if `Context` instance is not supported.
37
    static func checkCompatibility(with context: Context) throws
38
39
    /// `ContextSettingTask` will call this method to provide the `Context` instance to the `View`
40
    /// that has just been build or found.
41
    ///
42
    /// - Parameter context: `Context` instance.
43
    /// - Throws: throws `Error` if `Context` instance is not supported. `Router` will stop building the rest of the stack in this case.
44
    mutating func setup(with context: Context) throws
45
46
}
47
48
// MARK: Default implementation
49
50
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
51
public extension ContextAcceptingView {
52
53
    /// Default implementation does nothing.
54
    static func checkCompatibility(with context: Context) throws {}
1x
55
56
}
57
58
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
59
extension UIHostingController: ContextAccepting where Content: ContextAcceptingView {
60
61
    public static func checkCompatibility(with context: Content.Context) throws {
1x
62
        try Content.checkCompatibility(with: context)
1x
63
    }
1x
64
65
    public func setup(with context: Content.Context) throws {
1x
66
        try rootView.setup(with: context)
1x
67
    }
1x
68
69
}
70
71
#endif