Skip to content

Instantly share code, notes, and snippets.

@iampatbrown
Created July 7, 2022 02:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iampatbrown/d449e1d82bbb4f0dd8b8ca5e81425f89 to your computer and use it in GitHub Desktop.
Save iampatbrown/d449e1d82bbb4f0dd8b8ca5e81425f89 to your computer and use it in GitHub Desktop.
Helpers for creating reducers for BindableState
import ComposableArchitecture
import SwiftUI
extension Reducer {
/// Returns a reducer that applies ``BindingAction`` mutations to `LocalState` on `State`.
///
/// - Parameters:
/// - toLocalState: A key path that can get/set `LocalState` inside `State`.
/// - toLocalAction: A case path that can extract/embed `BindingAction` of `LocalState` from `Action`.
/// - Returns: A reducer that applies ``BindingAction`` mutations to `LocalState` on `State`.
public static func binding<LocalState>(
state toLocalState: WritableKeyPath<State, LocalState>,
action toLocalAction: CasePath<Action, BindingAction<LocalState>>
) -> Self {
let reducer = Reducer<LocalState, BindableActionOf<LocalState>, Void>.empty.binding()
return Reducer<LocalState, BindingAction<LocalState>, Void> { state, action, _ in
_ = reducer.run(&state, .binding(action), ())
return .none
}
.pullback(state: toLocalState, action: toLocalAction, environment: { _ in })
}
/// Returns a reducer that applies ``BindingAction`` mutations to `LocalState` on `State`before running this
/// reducer's logic.
///
/// - Parameters:
/// - toLocalState: A key path that can get/set `LocalState` inside `State`.
/// - toLocalAction: A case path that can extract/embed `BindingAction` of `LocalState` from `Action`.
/// - Returns: A reducer that applies ``BindingAction`` mutations to `LocalState` on `State`before running this
/// reducer's logic.
public func binding<LocalState>(
state toLocalState: WritableKeyPath<State, LocalState>,
action toLocalAction: CasePath<Action, BindingAction<LocalState>>
) -> Self {
.binding(state: toLocalState, action: toLocalAction).combined(with: self)
}
/// Returns a reducer that applies ``BindingAction`` mutations to `LocalState` on `State`.
///
/// - Parameters:
/// - toLocalState: A case path that can extract/embed `LocalState` from `State`.
/// - toLocalAction: A case path that can extract/embed `BindingAction` of `LocalState` from `Action`.
/// - Returns: A reducer that applies ``BindingAction`` mutations to `LocalState` on `State`.
public static func binding<LocalState>(
state toLocalState: CasePath<State, LocalState>,
action toLocalAction: CasePath<Action, BindingAction<LocalState>>
) -> Self {
let reducer = Reducer<LocalState, BindableActionOf<LocalState>, Void>.empty.binding()
return Reducer<LocalState, BindingAction<LocalState>, Void> { state, action, _ in
_ = reducer.run(&state, .binding(action), ())
return .none
}
.pullback(state: toLocalState, action: toLocalAction, environment: { _ in })
}
/// Returns a reducer that applies ``BindingAction`` mutations to `LocalState` on `State`before running this
/// reducer's logic.
///
/// - Parameters:
/// - toLocalState: A case path that can extract/embed `LocalState` from `State`.
/// - toLocalAction: A case path that can extract/embed `BindingAction` of `LocalState` from `Action`.
/// - Returns: A reducer that applies ``BindingAction`` mutations to `LocalState` on `State`before running this
/// reducer's logic.
public func binding<LocalState>(
state toLocalState: CasePath<State, LocalState>,
action toLocalAction: CasePath<Action, BindingAction<LocalState>>
) -> Self {
.binding(state: toLocalState, action: toLocalAction).combined(with: self)
}
}
extension ViewStore {
/// Returns a binding to the resulting bindable state of a given key path.
///
/// - Parameter keyPath: A key path to a specific bindable state.
/// - Returns: A new binding.
public func binding<Value: Equatable>(
_ keyPath: WritableKeyPath<State, BindableState<Value>>
) -> Binding<Value> where Action == BindingAction<State> {
self.binding(get: { $0[keyPath: keyPath].wrappedValue }, send: { .set(keyPath, $0) })
}
}
/// Helper for creating a bindable action directly to state
private enum BindableActionOf<State>: BindableAction {
case binding(BindingAction<State>)
}
MIT License
Copyright (c) 2022 Pat Brown
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment