Skip to content

Commit a06929e

Browse files
committed
Changes for using uniffi bindings
1 parent 9fe36d4 commit a06929e

File tree

15 files changed

+66
-63
lines changed

15 files changed

+66
-63
lines changed

Package.resolved

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"pins" : [
33
{
4-
"identity" : "liveview-native-core-swift",
4+
"identity" : "liveview-native-core",
55
"kind" : "remoteSourceControl",
6-
"location" : "https://github.com/liveview-native/liveview-native-core-swift.git",
6+
"location" : "https://github.com/liveview-native/liveview-native-core.git",
77
"state" : {
8-
"revision" : "339594b395938e38f1fb2216a31c8791030ec9ba",
9-
"version" : "0.2.1"
8+
"revision" : "ced90995fda19a1b4eba0ea379112822246ee7ce",
9+
"version" : "0.4.0-alpha-1"
1010
}
1111
},
1212
{
@@ -86,8 +86,8 @@
8686
"kind" : "remoteSourceControl",
8787
"location" : "https://github.com/davidstump/SwiftPhoenixClient.git",
8888
"state" : {
89-
"revision" : "613989bf2e562d8a851ea83741681c3439353b45",
90-
"version" : "5.0.0"
89+
"revision" : "588bf6baab5d049752748e19a4bff32421ea40ec",
90+
"version" : "5.3.2"
9191
}
9292
},
9393
{

Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ let package = Package(
2727
.package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.3.2"),
2828
.package(url: "https://github.com/davidstump/SwiftPhoenixClient.git", .upToNextMinor(from: "5.3.2")),
2929
.package(url: "https://github.com/apple/swift-async-algorithms", from: "0.1.0"),
30-
.package(url: "https://github.com/liveview-native/liveview-native-core-swift.git", exact: "0.2.1"),
30+
.package(url: "https://github.com/liveview-native/liveview-native-core.git", exact: "0.4.0-alpha-1"),
3131

3232
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.2"),
3333
.package(url: "https://github.com/apple/swift-markdown.git", from: "0.2.0"),
@@ -45,7 +45,7 @@ let package = Package(
4545
"SwiftSoup",
4646
"SwiftPhoenixClient",
4747
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
48-
.product(name: "LiveViewNativeCore", package: "liveview-native-core-swift"),
48+
.product(name: "LiveViewNativeCore", package: "liveview-native-core"),
4949
"LiveViewNativeMacros",
5050
"LiveViewNativeStylesheet"
5151
],
@@ -146,7 +146,7 @@ let package = Package(
146146
name: "LiveViewNativeStylesheet",
147147
dependencies: [
148148
"LiveViewNativeStylesheetMacros",
149-
.product(name: "LiveViewNativeCore", package: "liveview-native-core-swift"),
149+
.product(name: "LiveViewNativeCore", package: "liveview-native-core"),
150150
.product(name: "Parsing", package: "swift-parsing"),
151151
]
152152
),

Sources/LiveViewNative/Coordinators/LiveSessionCoordinator.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
4646
@Published private(set) var stylesheet: Stylesheet<R>?
4747

4848
// Socket connection
49-
var socket: Socket?
49+
var socket: SwiftPhoenixClient.Socket?
5050

5151
private var domValues: DOMValues!
5252

53-
private var liveReloadSocket: Socket?
54-
private var liveReloadChannel: Channel?
53+
private var liveReloadSocket: SwiftPhoenixClient.Socket?
54+
private var liveReloadChannel: SwiftPhoenixClient.Channel?
5555

5656
private var cancellables = Set<AnyCancellable>()
5757

Sources/LiveViewNative/Coordinators/LiveViewCoordinator.swift

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
3535
@_spi(LiveForm) public let session: LiveSessionCoordinator<R>
3636
var url: URL
3737

38-
private var channel: Channel?
38+
private var channel: SwiftPhoenixClient.Channel?
3939

4040
@Published var document: LiveViewNativeCore.Document?
4141
private var elementChangedSubjects = [NodeRef:ObjectWillChangePublisher]()
@@ -197,9 +197,7 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
197197

198198
private func handleDiff(payload: Payload, baseURL: URL) throws {
199199
handleEvents(payload: payload)
200-
let diff = try RootDiff(from: FragmentDecoder(data: payload))
201-
self.rendered = try self.rendered.merge(with: diff)
202-
self.document?.merge(with: try Document.parse(self.rendered.buildString()))
200+
try self.document?.mergeFragmentJson(payload)
203201
}
204202

205203
private func handleEvents(payload: Payload) {
@@ -329,7 +327,7 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
329327
case redirect(LiveRedirect)
330328
}
331329

332-
private func join(channel: Channel) -> AsyncThrowingStream<JoinResult, Error> {
330+
private func join(channel: SwiftPhoenixClient.Channel) -> AsyncThrowingStream<JoinResult, Error> {
333331
return AsyncThrowingStream<JoinResult, Error> { [weak channel] (continuation: AsyncThrowingStream<JoinResult, Error>.Continuation) -> Void in
334332
channel?.join()
335333
.receive("ok") { [weak self, weak channel] message in
@@ -385,21 +383,20 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
385383

386384
private func handleJoinPayload(renderedPayload: Payload) {
387385
// todo: what should happen if decoding or parsing fails?
388-
self.rendered = try! Root(from: FragmentDecoder(data: renderedPayload))
389-
self.document = try! LiveViewNativeCore.Document.parse(rendered.buildString())
390-
self.document?.on(.changed) { [unowned self] doc, nodeRef in
391-
switch doc[nodeRef].data {
386+
self.document = try! LiveViewNativeCore.Document.parseFragmentJson(payload: renderedPayload)
387+
self.document?.on(.changed) { nodeRef, nodeData, parent in
388+
switch nodeData {
392389
case .root:
393390
// when the root changes, update the `NavStackEntry` itself.
394391
self.objectWillChange.send()
395392
case .leaf:
396393
// text nodes don't have their own views, changes to them need to be handled by the parent Text view
397-
if let parent = doc.getParent(nodeRef) {
394+
if let parent = parent {
398395
self.elementChanged(nodeRef).send()
399396
} else {
400397
self.elementChanged(nodeRef).send()
401398
}
402-
case .element:
399+
case .nodeElement:
403400
// when a single element changes, send an update only to that element.
404401
self.elementChanged(nodeRef).send()
405402
}

Sources/LiveViewNative/Live/LiveElement.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public extension _LiveElementTracked {
115115
///
116116
/// - Parameter predicate: The filter used to select child nodes for render.
117117
func children(_ predicate: (Node) -> Bool = { node in
118-
!node.attributes.contains(where: { $0.name.namespace == nil && $0.name.name == "template" })
118+
!node.attributes().contains(where: { $0.name.namespace == nil && $0.name.name == "template" })
119119
}) -> some View {
120120
context.coordinator.builder.fromNodes(_element.children.filter(predicate), context: context.storage)
121121
}
@@ -126,9 +126,9 @@ public extension _LiveElementTracked {
126126
/// - Parameter includeDefault: Whether elements without a `template` attribute should be included in the filter. Defaults to `false`.
127127
func children(in template: Template, default includeDefault: Bool = false) -> some View {
128128
children {
129-
$0.attributes.contains(where: {
129+
$0.attributes().contains(where: {
130130
$0 == template
131-
}) || (includeDefault && !$0.attributes.contains(where: { $0.name.namespace == nil && $0.name.name == "template" }))
131+
}) || (includeDefault && !$0.attributes().contains(where: { $0.name.namespace == nil && $0.name.name == "template" }))
132132
}
133133
}
134134

@@ -138,7 +138,7 @@ public extension _LiveElementTracked {
138138
/// - Parameter includeDefault: Whether elements without a `template` attribute should be included in the filter. Defaults to `false`.
139139
func hasTemplate(_ template: Template, default includeDefault: Bool = false) -> Bool {
140140
_element.children.contains(where: {
141-
for attribute in $0.attributes {
141+
for attribute in $0.attributes() {
142142
if attribute == template {
143143
return true
144144
} else if includeDefault && attribute.name.namespace == nil && attribute.name.name == "template" {

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
})
@@ -169,7 +169,7 @@ public struct LiveContext<R: RootRegistry>: DynamicProperty {
169169
of element: ElementNode
170170
) -> [NodeChildrenSequence.Element] {
171171
element.children().filter {
172-
!$0.attributes.contains(where: { $0.name.rawValue == "template" })
172+
!$0.attributes().contains(where: { $0.name.rawValue == "template" })
173173
}
174174
}
175175
}

Sources/LiveViewNative/Property Wrappers/ObservedElement.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ extension ObservedElement {
119119
if let _resolvedChildIDs {
120120
return _resolvedChildIDs
121121
} else {
122-
let result = Set(self.resolvedChildren.map(\.id))
122+
let result = Set(self.resolvedChildren.map { $0.id()})
123123
_resolvedChildIDs = result
124124
return result
125125
}
@@ -183,3 +183,9 @@ extension ObservedElement {
183183
}
184184
}
185185
}
186+
187+
private extension Optional where Wrapped == ElementNode {
188+
var nodeRef: NodeRef? {
189+
self?.node.id()
190+
}
191+
}

Sources/LiveViewNative/Protocols/ContentBuilder.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -497,14 +497,14 @@ public extension ContentBuilder {
497497
return try build(
498498
element
499499
.children()
500-
.filter({ $0.attributes.contains(where: { $0.name == "template" && $0.value == template }) }),
500+
.filter({ $0.attributes().contains(where: { $0.name == "template" && $0.value == template }) }),
501501
in: context
502502
)
503503
} else {
504504
return try build(
505505
element
506506
.children()
507-
.filter({ !$0.attributes.contains(where: { $0.name == "template" }) }),
507+
.filter({ !$0.attributes().contains(where: { $0.name == "template" }) }),
508508
in: context
509509
)
510510
}
@@ -518,7 +518,7 @@ public extension ContentBuilder {
518518
return try build(
519519
element
520520
.children()
521-
.filter({ $0.attributes.contains(where: { $0.name == "template" && template.value.contains($0.value ?? "") }) }),
521+
.filter({ $0.attributes().contains(where: { $0.name == "template" && template.value.contains($0.value ?? "") }) }),
522522
in: context
523523
)
524524
}
@@ -565,15 +565,15 @@ public extension ContentBuilder {
565565
if let template {
566566
ViewTreeBuilder().fromNodes(
567567
element.children()
568-
.filter({ $0.attributes.contains(where: { $0.name == "template" && $0.value == template }) }),
568+
.filter({ $0.attributes().contains(where: { $0.name == "template" && $0.value == template }) }),
569569
context: context.context
570570
)
571571
.environment(\.coordinatorEnvironment, context.coordinatorEnvironment)
572572
.environment(\.anyLiveContextStorage, context.context)
573573
} else {
574574
ViewTreeBuilder().fromNodes(
575575
element.children()
576-
.filter({ !$0.attributes.contains(where: { $0.name == "template" }) }),
576+
.filter({ !$0.attributes().contains(where: { $0.name == "template" }) }),
577577
context: context.context
578578
)
579579
.environment(\.coordinatorEnvironment, context.coordinatorEnvironment)
@@ -589,7 +589,7 @@ public extension ContentBuilder {
589589
) -> some View {
590590
ViewTreeBuilder().fromNodes(
591591
element.children()
592-
.filter({ $0.attributes.contains(where: { $0.name == "template" && template.value.contains($0.value ?? "") }) }),
592+
.filter({ $0.attributes().contains(where: { $0.name == "template" && template.value.contains($0.value ?? "") }) }),
593593
context: context.context
594594
)
595595
.environment(\.coordinatorEnvironment, context.coordinatorEnvironment)
@@ -604,7 +604,7 @@ public extension ContentBuilder {
604604
) -> SwiftUI.Text {
605605
element.children()
606606
.lazy
607-
.filter({ $0.attributes.contains(where: { $0.name == "template" && template.value.contains($0.value ?? "") }) })
607+
.filter({ $0.attributes().contains(where: { $0.name == "template" && template.value.contains($0.value ?? "") }) })
608608
.first?.asElement().flatMap({ Text<R>(element: $0, overrideStylesheet: context.stylesheet).body })
609609
?? SwiftUI.Text("")
610610
}

Sources/LiveViewNative/Stylesheets/ViewReference.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ struct ToolbarTreeBuilder<R: RootRegistry> {
201201
// ToolbarTreeBuilder.fromNode may not be called with a root or leaf node
202202
switch node {
203203
case let .n(node):
204-
if case .element(let element) = node.data {
204+
if case .nodeElement(let element) = node.data() {
205205
Self.lookup(ElementNode(node: node, data: element))
206206
}
207207
case let .e(error):
@@ -271,7 +271,7 @@ struct CustomizableToolbarTreeBuilder<R: RootRegistry> {
271271
// CustomizableToolbarTreeBuilder.fromNode may not be called with a root or leaf node
272272
switch node {
273273
case let .n(node):
274-
if case .element(let element) = node.data {
274+
if case .nodeElement(let element) = node.data() {
275275
Self.lookup(ElementNode(node: node, data: element))
276276
}
277277
case let .e(error):

Sources/LiveViewNative/Utils/DOM.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import LiveViewNativeCore
2424
/// - ``elementChildren()``
2525
/// - ``innerText()``
2626
public struct ElementNode: Identifiable {
27-
public let node: Node
28-
public let data: ElementData
27+
let node: Node
28+
let data: Element
2929

30-
init(node: Node, data: ElementData) {
30+
init(node: Node, data: Element) {
3131
self.node = node
3232
self.data = data
3333
}
@@ -42,9 +42,9 @@ public struct ElementNode: Identifiable {
4242
public func elementChildren() -> [ElementNode] { node.children().compactMap({ $0.asElement() }) }
4343

4444
/// The namespace of the element.
45-
public var namespace: String? { data.namespace }
45+
public var namespace: String? { data.name.namespace }
4646
/// The tag name of the element.
47-
public var tag: String { data.tag }
47+
public var tag: String { data.name.name }
4848
/// The list of attributes present on this element.
4949
public var attributes: [LiveViewNativeCore.Attribute] { data.attributes }
5050
/// The attribute with the given name, or `nil` if there is no such attribute.
@@ -93,7 +93,7 @@ public struct ElementNode: Identifiable {
9393
public func innerText() -> String {
9494
// TODO: should follow the spec and insert/collapse whitespace around elements
9595
self.children().compactMap { node in
96-
if case .leaf(let content) = node.data {
96+
if case .leaf(let content) = node.data() {
9797
return content
9898
} else {
9999
return nil
@@ -115,7 +115,7 @@ public struct ElementNode: Identifiable {
115115

116116
extension Node {
117117
public func asElement() -> ElementNode? {
118-
if case .element(let data) = self.data {
118+
if case .nodeElement(let data) = self.data() {
119119
return ElementNode(node: self, data: data)
120120
} else {
121121
return nil

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
}
@@ -48,7 +48,7 @@ struct ViewTreeBuilder<R: RootRegistry> {
4848
let withID = applyID(element: element, to: bound)
4949
let withIDAndTag = applyTag(element: element, to: withID)
5050

51-
return ObservedElement.Observer.Applicator(element.node.id) {
51+
return ObservedElement.Observer.Applicator(element.node.id()) {
5252
withIDAndTag
5353
.environment(\.element, element)
5454
}
@@ -288,7 +288,7 @@ private enum ForEachElement: Identifiable {
288288
case let .keyed(_, id):
289289
return id
290290
case let .unkeyed(node):
291-
return "\(node.id)"
291+
return "\(node.id().ref())"
292292
}
293293
}
294294

Sources/LiveViewNative/Views/Controls and Indicators/Links/ShareLink.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ struct ShareLink<Root: RootRegistry>: View {
106106
public var body: some View {
107107
#if !os(tvOS)
108108
let useDefaultLabel = $liveElement.childNodes.filter({
109-
guard case let .element(data) = $0.data else { return true }
110-
return data.tag != "SharePreview"
109+
guard case let .nodeElement(data) = $0.data() else { return true }
110+
return data.name.name != "SharePreview"
111111
}).isEmpty
112112

113113
let subject = self.subject.flatMap(SwiftUI.Text.init)

Sources/LiveViewNative/Views/Images/ImageView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ struct ImageView<Root: RootRegistry>: View {
129129

130130
var label: SwiftUI.Text? {
131131
if let labelNode = $liveElement.childNodes.first {
132-
switch labelNode.data {
133-
case let .element(element):
132+
switch labelNode.data() {
133+
case let .nodeElement(element):
134134
return Text<Root>(element: ElementNode(node: labelNode, data: element), overrideStylesheet: nil).body
135135
case let .leaf(label):
136136
return .init(label)

0 commit comments

Comments
 (0)