Skip to content

Commit 3039076

Browse files
committed
add sugar for passing Action as View callback
1 parent c78d2ea commit 3039076

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

Samples/TicTacToe/Sources/Authentication/LoginScreen.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ struct LoginScreen: SwiftUIScreen, Equatable {
4444
get: \.password,
4545
set: Action.passwordUpdated
4646
),
47-
onCommit: { model.value.actionSink.send(.login) }
47+
onCommit: model.action(.login)
4848
)
4949

50-
Button("Login", action: { model.value.actionSink.send(.login) })
50+
Button("Login", action: model.action(.login))
5151
}
5252
.frame(maxWidth: 400)
5353
}

WorkflowSwiftUI/Sources/ObservableValue.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,14 @@ public final class ObservableValue<Value>: ObservableObject {
104104
return subject.removeDuplicates(by: isDuplicate).eraseToAnyPublisher()
105105
}
106106
}
107+
108+
#if canImport(UIKit)
109+
110+
public extension ObservableValue where Value: SwiftUIScreen {
111+
func action(_ action: Value.Action) -> () -> Void {
112+
// TODO: Should this closure capture the current `actionSink` instead?
113+
{ [weak self] in self?.value.actionSink.send(action) }
114+
}
115+
}
116+
117+
#endif

0 commit comments

Comments
 (0)