Skip to content

Commit

Permalink
add sugar for passing Action as Binding setter
Browse files Browse the repository at this point in the history
  • Loading branch information
square-tomb committed Jun 22, 2023
1 parent d3fb358 commit c78d2ea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Samples/TicTacToe/Sources/Authentication/LoginScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct LoginScreen: SwiftUIScreen, Equatable {
"[email protected]",
text: model.binding(
get: \.email,
set: { screen in { screen.actionSink.send(.emailUpdated($0)) } }
set: Action.emailUpdated
)
)
.autocapitalization(.none)
Expand All @@ -42,7 +42,7 @@ struct LoginScreen: SwiftUIScreen, Equatable {
"password",
text: model.binding(
get: \.password,
set: { screen in { screen.actionSink.send(.passwordUpdated($0)) } }
set: Action.passwordUpdated
),
onCommit: { model.value.actionSink.send(.login) }
)
Expand Down
12 changes: 12 additions & 0 deletions WorkflowSwiftUI/Sources/ObservableValue+Binding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,16 @@ public extension ObservableValue {
}
}

public extension ObservableValue where Value: SwiftUIScreen {
func binding<T>(
get: @escaping (Value) -> T,
set: @escaping (T) -> (Value.Action)
) -> Binding<T> {
binding(
get: get,
set: { screen in { screen.actionSink.send(set($0)) } }
)
}
}

#endif

0 comments on commit c78d2ea

Please sign in to comment.