-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NativeScriptSwiftUIProviders.swift
229 lines (193 loc) · 5.89 KB
/
NativeScriptSwiftUIProviders.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import SwiftUI
import UIKit
@objc
class TitleViewProvider: UIViewController, SwiftUIProvider {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
required public init() {
super.init(nibName: nil, bundle: nil)
}
public override func viewDidLoad() {
super.viewDidLoad()
setupSwiftUIView(content: IntroText(finished: {
self.onEvent?([:])
}))
}
/// Receive data from NativeScript
func updateData(data: NSDictionary) {
// data.forEach { (k,v) in swiftUIView.data.props[k] = v }
}
/// Allow sending of data to NativeScript
var onEvent: ((NSDictionary) -> ())?
}
private struct TitleText: View {
var title: String
var body: some View {
Text(title)
.monospaced()
.font(.system(size: 50, weight: .bold))
}
}
@Observable
class TitleViewModel {
var titleText: String = ""
var isTitleFinished: Bool = false
var finalTitle: String = "Hello World"
}
struct IntroText: View {
@State var model = TitleViewModel()
var finished: (() -> Void)
var body: some View {
@Bindable var model = model
// A hidden version of the final text keeps the layout fixed
// while the overlaid visible version types on.
VStack {
// A hidden version of the final text keeps the layout fixed
// while the overlaid visible version types on.
TitleText(title: model.finalTitle)
.padding(.horizontal, 70)
.hidden()
.overlay(alignment: .leading) {
TitleText(title: model.titleText)
.padding(.leading, 70)
}
Text("Discover a new way of looking at the world.")
.font(.title)
.opacity(model.isTitleFinished ? 1 : 0)
}
.typeText(
text: $model.titleText,
finalText: model.finalTitle,
isFinished: $model.isTitleFinished,
isAnimated: !model.isTitleFinished)
.animation(.default.speed(0.25), value: model.isTitleFinished)
.onChange(of: model.isTitleFinished) {
print("model.isTitleFinished \(model.isTitleFinished)")
if (model.isTitleFinished) {
finished()
}
}
}
}
class DetailToggleButtonProviderBase: UIViewController, SwiftUIProvider {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
required public init() {
super.init(nibName: nil, bundle: nil)
}
func updateData(data: NSDictionary) {}
var onEvent: ((NSDictionary) -> ())?
}
@objc
class DetailToggleGlobeProvider: DetailToggleButtonProviderBase {
public override func viewDidLoad() {
super.viewDidLoad()
setupSwiftUIView(content: DetailToggleButton(type: "globe"))
}
}
@objc
class DetailToggleOrbitProvider: DetailToggleButtonProviderBase {
public override func viewDidLoad() {
super.viewDidLoad()
setupSwiftUIView(content: DetailToggleButton(type: "orbit"))
}
}
@objc
class DetailToggleSolarProvider: DetailToggleButtonProviderBase {
public override func viewDidLoad() {
super.viewDidLoad()
setupSwiftUIView(content: DetailToggleButton(type: "solar"))
}
}
@Observable
class DetailToggleButtonModel {
var isShowingGlobe: Bool = false
var isShowingOrbit: Bool = false
var isShowingSolar: Bool = false
}
struct DetailToggleButton: View {
@State var model = DetailToggleButtonModel()
var type: String
var body: some View {
@Bindable var model = model
switch type {
case "orbit":
SpaceToggle(
title: "View Orbits",
id: "Orbit",
isShowing: $model.isShowingOrbit)
case "solar":
SpaceToggle(
title: "View Outer Space",
id: "Solar",
isShowing: $model.isShowingSolar)
default:
WindowToggle(
title: "View Globe",
id: "Globe",
isShowing: $model.isShowingGlobe)
}
}
}
struct WindowToggle: View {
var title: String
var id: String
@Binding var isShowing: Bool
@Environment(\.openWindow) private var openWindow
@Environment(\.dismissWindow) private var dismissWindow
var body: some View {
Toggle(title, isOn: $isShowing)
.onChange(of: isShowing) { wasShowing, isShowing in
if isShowing {
openWindow(id: id)
} else {
dismissWindow(id: id)
}
}
.toggleStyle(.button)
}
}
/// A toggle that activates or deactivates the immersive space with
/// the specified identifier.
struct SpaceToggle: View {
var title: String
var id: String
@Binding var isShowing: Bool
@Environment(\.openImmersiveSpace) private var openImmersiveSpace
@Environment(\.dismissImmersiveSpace) private var dismissImmersiveSpace
var body: some View {
Toggle(title, isOn: $isShowing)
.onChange(of: isShowing) { wasShowing, isShowing in
Task {
if isShowing {
await openImmersiveSpace(id: id)
} else {
await dismissImmersiveSpace()
}
}
}
.toggleStyle(.button)
}
}
@objc
class OrbitViewProvider: UIViewController, SwiftUIProvider {
private var swiftUI: OrbitModule?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
required public init() {
super.init(nibName: nil, bundle: nil)
}
public override func viewDidLoad() {
super.viewDidLoad()
}
func updateData(data: NSDictionary) {
if (self.swiftUI == nil) {
swiftUI = OrbitModule()
setupSwiftUIView(content: swiftUI.environment(EnvData.model))
}
}
var onEvent: ((NSDictionary) -> ())?
}