Skip to content

Commit 7301c9e

Browse files
committed
Initial changes for using uniffi bindings
1 parent ad2cf40 commit 7301c9e

File tree

10 files changed

+68
-65
lines changed

10 files changed

+68
-65
lines changed

Sources/LiveViewNative/ContentBuilder.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -483,14 +483,14 @@ public extension ContentBuilder {
483483
return try build(
484484
element
485485
.children()
486-
.filter({ $0.attributes.contains(where: { $0.name == "template" && $0.value == template }) }),
486+
.filter({ $0.attributes().contains(where: { $0.name == "template" && $0.value == template }) }),
487487
in: context
488488
)
489489
} else {
490490
return try build(
491491
element
492492
.children()
493-
.filter({ !$0.attributes.contains(where: { $0.name == "template" }) }),
493+
.filter({ !$0.attributes().contains(where: { $0.name == "template" }) }),
494494
in: context
495495
)
496496
}
@@ -504,7 +504,7 @@ public extension ContentBuilder {
504504
return try build(
505505
element
506506
.children()
507-
.filter({ $0.attributes.contains(where: { $0.name == "template" && template.value.contains($0.value ?? "") }) }),
507+
.filter({ $0.attributes().contains(where: { $0.name == "template" && template.value.contains($0.value ?? "") }) }),
508508
in: context
509509
)
510510
}
@@ -551,15 +551,15 @@ public extension ContentBuilder {
551551
if let template {
552552
ViewTreeBuilder().fromNodes(
553553
element.children()
554-
.filter({ $0.attributes.contains(where: { $0.name == "template" && $0.value == template }) }),
554+
.filter({ $0.attributes().contains(where: { $0.name == "template" && $0.value == template }) }),
555555
context: context.context
556556
)
557557
.environment(\.coordinatorEnvironment, context.coordinatorEnvironment)
558558
.environment(\.anyLiveContextStorage, context.context)
559559
} else {
560560
ViewTreeBuilder().fromNodes(
561561
element.children()
562-
.filter({ !$0.attributes.contains(where: { $0.name == "template" }) }),
562+
.filter({ !$0.attributes().contains(where: { $0.name == "template" }) }),
563563
context: context.context
564564
)
565565
.environment(\.coordinatorEnvironment, context.coordinatorEnvironment)
@@ -575,7 +575,7 @@ public extension ContentBuilder {
575575
) -> some View {
576576
ViewTreeBuilder().fromNodes(
577577
element.children()
578-
.filter({ $0.attributes.contains(where: { $0.name == "template" && template.value.contains($0.value ?? "") }) }),
578+
.filter({ $0.attributes().contains(where: { $0.name == "template" && template.value.contains($0.value ?? "") }) }),
579579
context: context.context
580580
)
581581
.environment(\.coordinatorEnvironment, context.coordinatorEnvironment)
@@ -590,7 +590,7 @@ public extension ContentBuilder {
590590
) -> SwiftUI.Text {
591591
element.children()
592592
.lazy
593-
.filter({ $0.attributes.contains(where: { $0.name == "template" && template.value.contains($0.value ?? "") }) })
593+
.filter({ $0.attributes().contains(where: { $0.name == "template" && template.value.contains($0.value ?? "") }) })
594594
.first?.asElement().flatMap({ Text<R>(element: $0).body })
595595
?? SwiftUI.Text("")
596596
}

Sources/LiveViewNative/Coordinators/LiveSessionCoordinator.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,35 @@ private let logger = Logger(subsystem: "LiveViewNative", category: "LiveSessionC
3434
public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
3535
/// The current state of the live view connection.
3636
@Published public private(set) var state = LiveSessionState.notConnected
37-
37+
3838
/// The current URL this live view is connected to.
3939
public private(set) var url: URL
40-
40+
4141
@Published var navigationPath = [LiveNavigationEntry<R>]()
42-
42+
4343
internal let configuration: LiveSessionConfiguration
44-
44+
4545
@Published private(set) var rootLayout: LiveViewNativeCore.Document?
4646
@Published private(set) var stylesheet: Stylesheet<R>?
47-
47+
4848
// Socket connection
49-
var socket: Socket?
50-
49+
var socket: SwiftPhoenixClient.Socket?
50+
5151
private var domValues: DOMValues!
52-
53-
private var liveReloadSocket: Socket?
54-
private var liveReloadChannel: Channel?
55-
52+
53+
private var liveReloadSocket: SwiftPhoenixClient.Socket?
54+
private var liveReloadChannel: SwiftPhoenixClient.Channel?
55+
5656
private var cancellables = Set<AnyCancellable>()
57-
57+
5858
private var mergedEventSubjects: AnyCancellable?
5959
private var eventSubject = PassthroughSubject<(LiveViewCoordinator<R>, (String, Payload)), Never>()
6060
private var eventHandlers = Set<AnyCancellable>()
61-
61+
6262
public convenience init(_ host: some LiveViewHost, config: LiveSessionConfiguration = .init(), customRegistryType: R.Type = R.self) {
6363
self.init(host.url, config: config, customRegistryType: customRegistryType)
6464
}
65-
65+
6666
/// Creates a new coordinator with a custom registry.
6767
/// - Parameter url: The URL of the page to establish the connection to.
6868
/// - Parameter config: The configuration for this coordinator.

Sources/LiveViewNative/Coordinators/LiveViewCoordinator.swift

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ private let logger = Logger(subsystem: "LiveViewNative", category: "LiveViewCoor
2727
@MainActor
2828
public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
2929
@Published internal private(set) var internalState: LiveSessionState = .notConnected
30-
30+
3131
var state: LiveSessionState {
3232
internalState
3333
}
34-
34+
3535
let session: LiveSessionCoordinator<R>
3636
var url: URL
37-
38-
private var channel: Channel?
39-
37+
38+
private var channel: SwiftPhoenixClient.Channel?
39+
4040
@Published var document: LiveViewNativeCore.Document?
4141
private var elementChangedSubjects = [NodeRef:ObjectWillChangePublisher]()
4242
func elementChanged(_ ref: NodeRef) -> ObjectWillChangePublisher {
@@ -194,9 +194,11 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
194194

195195
private func handleDiff(payload: Payload, baseURL: URL) throws {
196196
handleEvents(payload: payload)
197-
let diff = try RootDiff(from: FragmentDecoder(data: payload))
198-
self.rendered = try self.rendered.merge(with: diff)
199-
self.document?.merge(with: try Document.parse(self.rendered.buildString()))
197+
try self.document?.mergeFragmentJson(payload)
198+
199+
//let diff = try RootDiff(from: FragmentDecoder(data: payload))
200+
//self.rendered = try self.rendered.merge(with: diff)
201+
//self.document?.merge(with: try Document.parse(self.rendered.buildString()))
200202
}
201203

202204
private func handleEvents(payload: Payload) {
@@ -320,13 +322,13 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
320322
self?.internalState = .notConnected
321323
}
322324
}
323-
325+
324326
enum JoinResult {
325327
case rendered(Payload)
326328
case redirect(LiveRedirect)
327329
}
328330

329-
private func join(channel: Channel) -> AsyncThrowingStream<JoinResult, Error> {
331+
private func join(channel: SwiftPhoenixClient.Channel) -> AsyncThrowingStream<JoinResult, Error> {
330332
return AsyncThrowingStream<JoinResult, Error> { [weak channel] (continuation: AsyncThrowingStream<JoinResult, Error>.Continuation) -> Void in
331333
channel?.join()
332334
.receive("ok") { [weak self, weak channel] message in
@@ -382,10 +384,11 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
382384

383385
private func handleJoinPayload(renderedPayload: Payload) {
384386
// todo: what should happen if decoding or parsing fails?
385-
self.rendered = try! Root(from: FragmentDecoder(data: renderedPayload))
386-
self.document = try! LiveViewNativeCore.Document.parse(rendered.buildString())
387+
//self.rendered = try! Root(from: FragmentDecoder(data: renderedPayload))
388+
//self.document = try! LiveViewNativeCore.Document.parse(rendered.buildString())
389+
self.document = try! LiveViewNativeCore.Document.parseFragmentJson(payload: renderedPayload)
387390
self.document?.on(.changed) { [unowned self] doc, nodeRef in
388-
switch doc[nodeRef].data {
391+
switch doc[nodeRef].data() {
389392
case .root:
390393
// when the root changes, update the `NavStackEntry` itself.
391394
self.objectWillChange.send()
@@ -396,7 +399,7 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
396399
} else {
397400
self.elementChanged(nodeRef).send()
398401
}
399-
case .element:
402+
case .nodeElement:
400403
// when a single element changes, send an update only to that element.
401404
self.elementChanged(nodeRef).send()
402405
}

Sources/LiveViewNative/Property Wrappers/LiveContext.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public struct LiveContext<R: RootRegistry>: DynamicProperty {
5858
/// Ignores any children with a `template` attribute.
5959
public func buildChildren(of element: ElementNode) -> some View {
6060
return coordinator.builder.fromNodes(element.children().filter({
61-
if case let .element(element) = $0.data {
61+
if case let .nodeElement(element) = $0.data() {
6262
return !element.attributes.contains(where: { $0.name == "template" })
6363
} else {
6464
return true
@@ -70,7 +70,7 @@ public struct LiveContext<R: RootRegistry>: DynamicProperty {
7070
_ template: String
7171
) -> (NodeChildrenSequence.Element) -> Bool {
7272
{ child in
73-
if case let .element(element) = child.data,
73+
if case let .nodeElement(element) = child.data(),
7474
element.attributes.first(where: { $0.name == "template" })?.value == template
7575
{
7676
return true
@@ -81,7 +81,7 @@ public struct LiveContext<R: RootRegistry>: DynamicProperty {
8181
}
8282

8383
private static func hasTemplateAttribute(_ child: NodeChildrenSequence.Element) -> Bool {
84-
if case let .element(element) = child.data,
84+
if case let .nodeElement(element) = child.data(),
8585
element.attributes.contains(where: { $0.name == "template" })
8686
{
8787
return true
@@ -127,7 +127,7 @@ public struct LiveContext<R: RootRegistry>: DynamicProperty {
127127
let namedSlotChildren = children.filter(Self.isTemplateElement(template))
128128
if namedSlotChildren.isEmpty && includeDefaultSlot {
129129
let defaultSlotChildren = children.filter({
130-
if case let .element(element) = $0.data {
130+
if case let .nodeElement(element) = $0.data() {
131131
return !element.attributes.contains(where: {
132132
$0.name.rawValue == "template"
133133
})
@@ -153,7 +153,7 @@ public struct LiveContext<R: RootRegistry>: DynamicProperty {
153153
of element: ElementNode
154154
) -> [NodeChildrenSequence.Element] {
155155
element.children().filter {
156-
!$0.attributes.contains(where: { $0.name.rawValue == "template" })
156+
!$0.attributes().contains(where: { $0.name.rawValue == "template" })
157157
}
158158
}
159159
}

Sources/LiveViewNative/Property Wrappers/ObservedElement.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,6 @@ extension ObservedElement {
132132

133133
private extension Optional where Wrapped == ElementNode {
134134
var nodeRef: NodeRef? {
135-
self?.node.id
135+
self?.node.id()
136136
}
137137
}

Sources/LiveViewNative/Stylesheets/ViewReference.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,19 @@ struct ToolbarTreeBuilder<R: RootRegistry> {
130130
return ToolbarContentBuilder.buildBlock(f(.e(ToolbarError.badChildCount(e.count)), c), f(nil, c), f(nil, c), f(nil, c), f(nil, c), f(nil, c), f(nil, c), f(nil, c), f(nil, c), f(nil, c))
131131
}
132132
}
133-
133+
134134
// alias for typing
135135
@inline(__always)
136136
fileprivate func f(_ n: FromNodeValue?, _ c: LiveContextStorage<R>) -> some ToolbarContent {
137137
return n.flatMap({ fromNode($0, context: c) })
138138
}
139-
139+
140140
@ToolbarContentBuilder
141141
fileprivate func fromNode(_ node: FromNodeValue, context: LiveContextStorage<R>) -> some ToolbarContent {
142142
// ToolbarTreeBuilder.fromNode may not be called with a root or leaf node
143143
switch node {
144144
case let .n(node):
145-
if case .element(let element) = node.data {
145+
if case .nodeElement(let element) = node.data() {
146146
Self.lookup(ElementNode(node: node, data: element))
147147
}
148148
case let .e(error):
@@ -200,19 +200,19 @@ struct CustomizableToolbarTreeBuilder<R: RootRegistry> {
200200
return ToolbarContentBuilder.buildBlock(f(.e(ToolbarError.badChildCount(e.count)), c), f(nil, c), f(nil, c), f(nil, c), f(nil, c), f(nil, c), f(nil, c), f(nil, c), f(nil, c), f(nil, c))
201201
}
202202
}
203-
203+
204204
// alias for typing
205205
@inline(__always)
206206
fileprivate func f(_ n: FromNodeValue?, _ c: LiveContextStorage<R>) -> some CustomizableToolbarContent {
207207
return n.flatMap({ fromNode($0, context: c) })
208208
}
209-
209+
210210
@ToolbarContentBuilder
211211
fileprivate func fromNode(_ node: FromNodeValue, context: LiveContextStorage<R>) -> some CustomizableToolbarContent {
212212
// CustomizableToolbarTreeBuilder.fromNode may not be called with a root or leaf node
213213
switch node {
214214
case let .n(node):
215-
if case .element(let element) = node.data {
215+
if case .nodeElement(let element) = node.data() {
216216
Self.lookup(ElementNode(node: node, data: element))
217217
}
218218
case let .e(error):

Sources/LiveViewNative/Utils/DOM.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,24 @@ import LiveViewNativeCore
2525
/// - ``innerText()``
2626
public struct ElementNode {
2727
let node: Node
28-
let data: ElementData
29-
30-
init(node: Node, data: ElementData) {
28+
let data: Element
29+
30+
init(node: Node, data: Element) {
3131
self.node = node
3232
self.data = data
3333
}
34-
34+
3535
/// A sequence representing this element's direct children.
3636
public func children() -> NodeChildrenSequence { node.children() }
3737
/// A sequence that traverses the nested child nodes of this element in depth-first order.
3838
public func depthFirstChildren() -> NodeDepthFirstChildrenSequence { node.depthFirstChildren() }
3939
/// A sequence representing this element's direct children that are elements.
4040
public func elementChildren() -> [ElementNode] { node.children().compactMap({ $0.asElement() }) }
41-
41+
4242
/// The namespace of the element.
43-
public var namespace: String? { data.namespace }
43+
public var namespace: String? { data.name.namespace }
4444
/// The tag name of the element.
45-
public var tag: String { data.tag }
45+
public var tag: String { data.name.name }
4646
/// The list of attributes present on this element.
4747
public var attributes: [LiveViewNativeCore.Attribute] { data.attributes }
4848
/// The attribute with the given name, or `nil` if there is no such attribute.
@@ -89,7 +89,7 @@ public struct ElementNode {
8989
public func innerText() -> String {
9090
// TODO: should follow the spec and insert/collapse whitespace around elements
9191
self.children().compactMap { node in
92-
if case .leaf(let content) = node.data {
92+
if case .leaf(let content) = node.data() {
9393
return content
9494
} else {
9595
return nil
@@ -111,7 +111,7 @@ public struct ElementNode {
111111

112112
extension Node {
113113
func asElement() -> ElementNode? {
114-
if case .element(let data) = self.data {
114+
if case .nodeElement(let data) = self.data() {
115115
return ElementNode(node: self, data: data)
116116
} else {
117117
return nil

Sources/LiveViewNative/ViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public class LiveViewModel: ObservableObject {
3535
func updateForms(nodes: NodeDepthFirstChildrenSequence) {
3636
var formIDs = Set<String>()
3737
for node in nodes {
38-
guard case .element(let elementData) = node.data,
39-
elementData.namespace == nil && elementData.tag == "live-form" else {
38+
guard case .nodeElement(let elementData) = node.data(),
39+
elementData.name.namespace == nil && elementData.name.name == "live-form" else {
4040
continue
4141
}
4242
let id = node["id"]!.value!

Sources/LiveViewNative/ViewTree.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ struct ViewTreeBuilder<R: RootRegistry> {
2929
let context: LiveContextStorage<R>
3030

3131
var body: some View {
32-
switch node.data {
32+
switch node.data() {
3333
case .root:
3434
fatalError("ViewTreeBuilder.fromNode may not be called with the root node")
3535
case .leaf(let content):
3636
SwiftUI.Text(content)
37-
case .element(let element):
37+
case .nodeElement(let element):
3838
context.coordinator.builder.fromElement(ElementNode(node: node, data: element), context: context)
3939
}
4040
}
@@ -50,7 +50,7 @@ struct ViewTreeBuilder<R: RootRegistry> {
5050

5151
return withIDAndTag
5252
.environment(\.element, element)
53-
.environmentObject(ObservedElement.Observer(element.node.id))
53+
.environmentObject(ObservedElement.Observer(element.node.id()))
5454
.preference(key: _ProvidedBindingsKey.self, value: []) // reset for the next View.
5555
}
5656

@@ -214,7 +214,7 @@ private enum ForEachElement: Identifiable {
214214
case let .keyed(_, id):
215215
return id
216216
case let .unkeyed(node):
217-
return "\(node.id)"
217+
return "\(node.id())"
218218
}
219219
}
220220

Sources/LiveViewNative/Views/Images/ImageView.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ struct ImageView<R: RootRegistry>: View {
117117
return nil
118118
}
119119
}
120-
120+
121121
var label: SwiftUI.Text? {
122122
if let labelNode = element.children().first {
123-
switch labelNode.data {
124-
case let .element(element):
123+
switch labelNode.data() {
124+
case let .nodeElement(element):
125125
return Text<R>(element: ElementNode(node: labelNode, data: element)).body
126126
case let .leaf(label):
127127
return .init(label)

0 commit comments

Comments
 (0)