-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathSwiftUIPreview.swift
60 lines (40 loc) · 1.31 KB
/
SwiftUIPreview.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
import SwiftUI
import AsyncDisplayKit
import TextureBridging
enum Preview_Node: PreviewProvider {
private typealias TargetComponent = CustomNode
static var previews: some View {
Group {
ViewHost(instantiated: NodeView(node: TargetComponent()))
}
}
}
private final class CustomNode: ASDisplayNode {
private let textNode = ASTextNode()
override func didLoad() {
super.didLoad()
backgroundColor = .red
textNode.attributedText = "Hello".styled(.init())
automaticallyManagesSubnodes = true
}
override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
return ASWrapperLayoutSpec(layoutElements: [textNode])
}
}
public struct ViewHost<ContentView: UIView>: UIViewRepresentable {
private let contentView: ContentView
private let _update: @MainActor (_ uiView: ContentView, _ context: Context) -> Void
public init(
instantiated: ContentView,
update: @escaping @MainActor (_ uiView: ContentView, _ context: Context) -> Void = { _, _ in }
) {
self.contentView = instantiated
self._update = update
}
public func makeUIView(context: Context) -> ContentView {
return contentView
}
public func updateUIView(_ uiView: ContentView, context: Context) {
_update(uiView, context)
}
}