Skip to content

Commit 82f8c95

Browse files
committed
Support current-value-label
1 parent 163811f commit 82f8c95

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

Sources/LiveViewNative/LiveContext.swift

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,11 @@ public struct LiveContext<R: CustomRegistry> {
4747
return coordinator.builder.fromNodes(element.children(), context: self)
4848
}
4949

50-
public func buildChildren(
51-
of element: ElementNode,
52-
withTagName tagName: String,
53-
namespace: String? = nil,
54-
includeDefaultSlot: Bool = false
55-
) -> some View {
56-
let children = element.children()
57-
let namedSlotChildren = children.filter({ child in
50+
private static func elementWithName(
51+
_ tagName: String,
52+
namespace: String?
53+
) -> (NodeChildrenSequence.Element) -> Bool {
54+
{ child in
5855
if case let .element(element) = child.data,
5956
element.namespace == namespace,
6057
element.tag == tagName
@@ -63,7 +60,25 @@ public struct LiveContext<R: CustomRegistry> {
6360
} else {
6461
return false
6562
}
66-
})
63+
}
64+
}
65+
66+
public func hasChild(
67+
of element: ElementNode,
68+
withTagName tagName: String,
69+
namespace: String? = nil
70+
) -> Bool {
71+
element.children().contains(where: Self.elementWithName(tagName, namespace: namespace))
72+
}
73+
74+
public func buildChildren(
75+
of element: ElementNode,
76+
withTagName tagName: String,
77+
namespace: String? = nil,
78+
includeDefaultSlot: Bool = false
79+
) -> some View {
80+
let children = element.children()
81+
let namedSlotChildren = children.filter(Self.elementWithName(tagName, namespace: namespace))
6782
if namedSlotChildren.isEmpty && includeDefaultSlot {
6883
let defaultSlotChildren = children.filter({
6984
if case let .element(element) = $0.data {

Sources/LiveViewNative/Views/Controls and Indicators/Indicators/ProgressView.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,37 @@ struct ProgressView<R: CustomRegistry>: View {
2020
if let timerIntervalStart = element.attributeValue(for: "timer-interval-start").flatMap({ try? ElixirDateParseStrategy().parse($0) }),
2121
let timerIntervalEnd = element.attributeValue(for: "timer-interval-end").flatMap({ try? ElixirDateParseStrategy().parse($0) })
2222
{
23-
// TODO: Note that this variant has a default `currentValueLabel`, which should be used if no current value label slot is used. It seems to only be active when initializing without the `currentValueLabel` argument.
24-
SwiftUI.ProgressView(
25-
timerInterval: timerIntervalStart...timerIntervalEnd,
26-
countsDown: element.attributeValue(for: "counts-down") != "false"
27-
) {
28-
context.buildChildren(of: element)
23+
// SwiftUI's default `currentValueLabel` is not present unless the argument is not included in the initializer.
24+
// Check if we have it first otherwise use the default.
25+
if context.hasChild(of: element, withTagName: "current-value-label", namespace: "progress-view") {
26+
SwiftUI.ProgressView(
27+
timerInterval: timerIntervalStart...timerIntervalEnd,
28+
countsDown: element.attributeValue(for: "counts-down") != "false"
29+
) {
30+
context.buildChildren(of: element, withTagName: "label", namespace: "progress-view", includeDefaultSlot: true)
31+
} currentValueLabel: {
32+
context.buildChildren(of: element, withTagName: "current-value-label", namespace: "progress-view")
33+
}
34+
} else {
35+
SwiftUI.ProgressView(
36+
timerInterval: timerIntervalStart...timerIntervalEnd,
37+
countsDown: element.attributeValue(for: "counts-down") != "false"
38+
) {
39+
context.buildChildren(of: element, withTagName: "label", namespace: "progress-view", includeDefaultSlot: true)
40+
}
2941
}
3042
} else if let value = element.attributeValue(for: "value").flatMap(Double.init) {
3143
SwiftUI.ProgressView(
3244
value: value,
3345
total: element.attributeValue(for: "total").flatMap(Double.init) ?? 1
3446
) {
35-
context.buildChildren(of: element)
47+
context.buildChildren(of: element, withTagName: "label", namespace: "progress-view", includeDefaultSlot: true)
3648
} currentValueLabel: {
37-
EmptyView() // TODO: Implement currentValueLabel once we have a design for multi-body content.
49+
context.buildChildren(of: element, withTagName: "current-value-label", namespace: "progress-view")
3850
}
3951
} else {
4052
SwiftUI.ProgressView {
41-
context.buildChildren(of: element)
53+
context.buildChildren(of: element, withTagName: "label", namespace: "progress-view", includeDefaultSlot: true)
4254
}
4355
}
4456
}

Sources/LiveViewNative/Views/Controls and Indicators/Value Inputs/Slider.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ struct Slider<R: CustomRegistry>: View {
2626
in: lowerBound...upperBound,
2727
step: step
2828
) {
29-
context.buildChildren(of: element, defaultSlotFor: "slider")
30-
context.buildChildren(of: element, withTagName: "label", namespace: "slider")
29+
context.buildChildren(of: element, withTagName: "label", namespace: "slider", includeDefaultSlot: true)
3130
} minimumValueLabel: {
3231
context.buildChildren(of: element, withTagName: "minimum-value-label", namespace: "slider")
3332
} maximumValueLabel: {
@@ -38,8 +37,7 @@ struct Slider<R: CustomRegistry>: View {
3837
value: $value,
3938
in: lowerBound...upperBound
4039
) {
41-
context.buildChildren(of: element, defaultSlotFor: "slider")
42-
context.buildChildren(of: element, withTagName: "label", namespace: "slider")
40+
context.buildChildren(of: element, withTagName: "label", namespace: "slider", includeDefaultSlot: true)
4341
} minimumValueLabel: {
4442
context.buildChildren(of: element, withTagName: "minimum-value-label", namespace: "slider")
4543
} maximumValueLabel: {

0 commit comments

Comments
 (0)