Skip to content

Commit c37dd87

Browse files
committed
Refactor event binding preference key
1 parent 75280f6 commit c37dd87

File tree

7 files changed

+40
-25
lines changed

7 files changed

+40
-25
lines changed

Sources/LiveViewNative/BuiltinRegistry+applyBinding.swift

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,56 @@
88
import SwiftUI
99
import LiveViewNativeCore
1010

11+
public enum _EventBinding: String {
12+
case windowFocus = "phx-window-focus"
13+
case windowBlur = "phx-window-blur"
14+
case focus = "phx-focus"
15+
case blur = "phx-blur"
16+
case click = "phx-click"
17+
}
18+
1119
extension BuiltinRegistry {
1220
@ViewBuilder
1321
static func applyBinding(
14-
_ binding: AttributeName,
22+
_ binding: _EventBinding,
1523
event: String,
1624
value: Payload,
1725
to view: some View,
1826
element: ElementNode
1927
) -> some View {
2028
ProvidedBindingsReader(
21-
binding: binding.rawValue,
29+
binding: binding,
2230
content: view
2331
) {
2432
switch binding {
25-
case "phx-window-focus":
33+
case .windowFocus:
2634
ScenePhaseObserver(content: view, target: .active, type: "focus", event: event, value: value)
27-
case "phx-window-blur":
35+
case .windowBlur:
2836
ScenePhaseObserver(content: view, target: .background, type: "blur", event: event, value: value)
29-
case "phx-focus":
37+
case .focus:
3038
FocusObserver(content: view, target: true, type: "focus", event: event, value: value)
31-
case "phx-blur":
39+
case .blur:
3240
FocusObserver(content: view, target: false, type: "blur", event: event, value: value)
33-
case "phx-click":
41+
case .click:
3442
TapGestureView(content: view, type: "click", event: event, value: value)
35-
default:
36-
view
3743
}
3844
}
3945
}
4046
}
4147

4248
/// A preference key that specifies what bindings the View handles internally.
4349
public enum _ProvidedBindingsKey: PreferenceKey {
44-
public static var defaultValue: Set<String> { [] }
45-
public static func reduce(value: inout Set<String>, nextValue: () -> Set<String>) {
50+
public static var defaultValue: Set<_EventBinding> { [] }
51+
public static func reduce(
52+
value: inout Set<_EventBinding>,
53+
nextValue: () -> Set<_EventBinding>
54+
) {
4655
value = nextValue()
4756
}
4857
}
4958

5059
fileprivate struct ProvidedBindingsReader<Content: View, ApplyBinding: View>: View {
51-
let binding: String
60+
let binding: _EventBinding
5261
let content: Content
5362
@ViewBuilder let applyBinding: () -> ApplyBinding
5463
@State private var providesBinding: Bool = false

Sources/LiveViewNative/ViewTree.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ struct ViewTreeBuilder<R: RootRegistry> {
8989
context: LiveContextStorage<R>
9090
) -> some View {
9191
view.applyBindings(
92-
element.attributes.filter({
93-
$0.name.namespace == nil
94-
&& $0.name.name.hasPrefix("phx-")
95-
&& $0.value != nil
92+
element.attributes.compactMap({
93+
guard $0.name.namespace == nil,
94+
$0.value != nil,
95+
let name = _EventBinding(rawValue: $0.name.name)
96+
else { return nil }
97+
return (name, $0)
9698
})[...],
9799
element: element,
98100
context: context
@@ -200,7 +202,7 @@ extension EnvironmentValues {
200202

201203
private struct BindingApplicator<Parent: View, R: RootRegistry>: View {
202204
let parent: Parent
203-
let bindings: ArraySlice<LiveViewNativeCore.Attribute>
205+
let bindings: ArraySlice<(_EventBinding, LiveViewNativeCore.Attribute)>
204206
let element: ElementNode
205207
let context: LiveContextStorage<R>
206208

@@ -209,8 +211,8 @@ private struct BindingApplicator<Parent: View, R: RootRegistry>: View {
209211
// force-unwrap is okay, this view is never constructed with an empty slice
210212
let binding = bindings.first!
211213
BuiltinRegistry<R>.applyBinding(
212-
binding.name,
213-
event: binding.value!,
214+
binding.0,
215+
event: binding.1.value!,
214216
value: element.buildPhxValuePayload(),
215217
to: parent,
216218
element: element
@@ -238,7 +240,11 @@ private struct ModifierApplicator<Parent: View, R: RootRegistry>: View {
238240

239241
extension View {
240242
@ViewBuilder
241-
func applyBindings<R: RootRegistry>(_ bindings: ArraySlice<LiveViewNativeCore.Attribute>, element: ElementNode, context: LiveContextStorage<R>) -> some View {
243+
func applyBindings<R: RootRegistry>(
244+
_ bindings: ArraySlice<(_EventBinding, LiveViewNativeCore.Attribute)>,
245+
element: ElementNode,
246+
context: LiveContextStorage<R>
247+
) -> some View {
242248
if bindings.isEmpty {
243249
self
244250
} else {

Sources/LiveViewNative/Views/Controls and Indicators/Buttons/Button.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public struct Button<Root: RootRegistry>: View {
5353
SwiftUI.Button(role: role, action: self.handleClick) {
5454
$liveElement.children()
5555
}
56-
.preference(key: _ProvidedBindingsKey.self, value: ["phx-click"])
56+
.preference(key: _ProvidedBindingsKey.self, value: [.click])
5757
}
5858

5959
private func handleClick() {

Sources/LiveViewNative/Views/Controls and Indicators/Buttons/PasteButton.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct PasteButton<Root: RootRegistry>: View {
4949
SwiftUI.PasteButton(payloadType: String.self) { strings in
5050
click(value: ["value": strings]) {}
5151
}
52-
.preference(key: _ProvidedBindingsKey.self, value: ["phx-click"])
52+
.preference(key: _ProvidedBindingsKey.self, value: [.click])
5353
#endif
5454
}
5555
}

Sources/LiveViewNative/Views/Text Input and Output/SecureField.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct SecureField<Root: RootRegistry>: TextFieldProtocol {
5353
}
5454
.focused($isFocused)
5555
.onChange(of: isFocused, perform: handleFocus)
56-
.preference(key: _ProvidedBindingsKey.self, value: ["phx-focus", "phx-blur"])
56+
.preference(key: _ProvidedBindingsKey.self, value: [.focus, .blur])
5757
}
5858

5959
@MainActor

Sources/LiveViewNative/Views/Text Input and Output/TextEditor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct TextEditor<Root: RootRegistry>: TextFieldProtocol {
3939
SwiftUI.TextEditor(text: textBinding)
4040
.focused($isFocused)
4141
.onChange(of: isFocused, perform: handleFocus)
42-
.preference(key: _ProvidedBindingsKey.self, value: ["phx-focus", "phx-blur"])
42+
.preference(key: _ProvidedBindingsKey.self, value: [.focus, .blur])
4343
#endif
4444
}
4545

Sources/LiveViewNative/Views/Text Input and Output/TextField.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ struct TextField<Root: RootRegistry>: TextFieldProtocol {
141141
field
142142
.focused($isFocused)
143143
.onChange(of: isFocused, perform: handleFocus)
144-
.preference(key: _ProvidedBindingsKey.self, value: ["phx-focus", "phx-blur"])
144+
.preference(key: _ProvidedBindingsKey.self, value: [.focus, .blur])
145145
}
146146

147147
@MainActor

0 commit comments

Comments
 (0)