-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathSwiftUIScreen.swift
99 lines (84 loc) · 3.52 KB
/
SwiftUIScreen.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright 2022 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if canImport(SwiftUI) && canImport(UIKit) && canImport(Combine) && swift(>=5.1)
import Combine
import SwiftUI
import Workflow
import WorkflowUI
@available(iOS 13.0, macOS 10.15, *)
public protocol SwiftUIScreen: Screen {
associatedtype Content: View
@ViewBuilder
static func makeView(model: ObservableValue<Self>) -> Content
static var isDuplicate: ((Self, Self) -> Bool)? { get }
}
@available(iOS 13.0, macOS 10.15, *)
public extension SwiftUIScreen {
static var isDuplicate: ((Self, Self) -> Bool)? { return nil }
}
@available(iOS 13.0, macOS 10.15, *)
public extension SwiftUIScreen where Self: Equatable {
static var isDuplicate: ((Self, Self) -> Bool)? { { $0 == $1 } }
}
@available(iOS 13.0, macOS 10.15, *)
public extension SwiftUIScreen {
func viewControllerDescription(environment: ViewEnvironment) -> ViewControllerDescription {
ViewControllerDescription(
type: ModeledHostingController<Self, WithModel<Self, EnvironmentInjectingView<Content>>>.self,
build: {
let (model, modelSink) = ObservableValue.makeObservableValue(self, isDuplicate: Self.isDuplicate)
let (viewEnvironment, envSink) = ObservableValue.makeObservableValue(environment)
return ModeledHostingController(
modelSink: modelSink,
viewEnvironmentSink: envSink,
rootView: WithModel(model, content: { model in
EnvironmentInjectingView(
viewEnvironment: viewEnvironment,
content: Self.makeView(model: model)
)
})
)
},
update: {
$0.modelSink.send(self)
$0.viewEnvironmentSink.send(environment)
}
)
}
}
@available(iOS 13.0, macOS 10.15, *)
private struct EnvironmentInjectingView<Content: View>: View {
@ObservedObject var viewEnvironment: ObservableValue<ViewEnvironment>
let content: Content
var body: some View {
content
.environment(\.viewEnvironment, viewEnvironment.value)
}
}
@available(iOS 13.0, macOS 10.15, *)
private final class ModeledHostingController<Model, Content: View>: UIHostingController<Content> {
let modelSink: Sink<Model>
let viewEnvironmentSink: Sink<ViewEnvironment>
init(modelSink: Sink<Model>, viewEnvironmentSink: Sink<ViewEnvironment>, rootView: Content) {
self.modelSink = modelSink
self.viewEnvironmentSink = viewEnvironmentSink
super.init(rootView: rootView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("not implemented")
}
}
#endif