Skip to content

Commit db1a50f

Browse files
authored
Merge pull request #9 from Lickability/feature/adjusting-make-binding
Adding ability to have a bool from a publisher with optional value
2 parents 06988a9 + 7ec5c06 commit db1a50f

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

Sources/ViewStore/ViewStore+BindingAdditions.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ public extension ViewStore {
4848
}
4949
}
5050

51+
/// Provides a mechanism for creating `Binding<Bool>`s based on the existence of a property on the `ViewState`, leveraging `KeyPath`s to reduce duplication and errors.
52+
/// - Parameters:
53+
/// - viewStateKeyPath: The `KeyPath` to the optional `ViewState` property whose existence determines the wrapped value.
54+
/// - actionCasePath: The `CasePath` to the `Action` case associated with the `ViewState` property.
55+
func makeBinding<Value>(viewStateKeyPath: KeyPath<ViewState, Value?>, actionCasePath: CasePath<Action, Value?>) -> Binding<Bool> {
56+
return .init {
57+
self.viewState[keyPath: viewStateKeyPath] != nil
58+
} set: { value in
59+
guard !value else {
60+
return assertionFailure("Unexpectedly received `true` from `makeBinding` Bool convenience setter.")
61+
}
62+
63+
self.send(actionCasePath.embed(nil))
64+
}
65+
}
66+
5167
/// Provides a mechanism for creating `Binding`s that send their value to a `PassthroughSubject`, leveraging `KeyPath`s to reduce duplication and errors.
5268
/// - Parameters:
5369
/// - viewStateKeyPath: The `KeyPath` to the `ViewState` property that this binding wraps.

0 commit comments

Comments
 (0)