Skip to content

Commit

Permalink
add sugar for passing Action as View callback
Browse files Browse the repository at this point in the history
  • Loading branch information
square-tomb committed Jun 22, 2023
1 parent c78d2ea commit 3039076
Show file tree
Hide file tree
Showing 2 changed files with 13 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 @@ -44,10 +44,10 @@ struct LoginScreen: SwiftUIScreen, Equatable {
get: \.password,
set: Action.passwordUpdated
),
onCommit: { model.value.actionSink.send(.login) }
onCommit: model.action(.login)
)

Button("Login", action: { model.value.actionSink.send(.login) })
Button("Login", action: model.action(.login))
}
.frame(maxWidth: 400)
}
Expand Down
11 changes: 11 additions & 0 deletions WorkflowSwiftUI/Sources/ObservableValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,14 @@ public final class ObservableValue<Value>: ObservableObject {
return subject.removeDuplicates(by: isDuplicate).eraseToAnyPublisher()
}
}

#if canImport(UIKit)

public extension ObservableValue where Value: SwiftUIScreen {
func action(_ action: Value.Action) -> () -> Void {
// TODO: Should this closure capture the current `actionSink` instead?
{ [weak self] in self?.value.actionSink.send(action) }
}
}

#endif

0 comments on commit 3039076

Please sign in to comment.