mirror of
https://github.com/pointfreeco/swift-composable-architecture.git
synced 2025-12-20 09:11:33 +01:00
* wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * Silence test warnings * wip * wip * wip * update a bunch of docs * wip * wip * fix * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * Kill integration tests for now * wip * wip * wip * wip * updating docs for @Reducer macro * replaced more Reducer protocols with @Reducer * Fixed some broken docc references * wip * Some @Reducer docs * more docs * convert some old styles to new style * wip * wip * wip * wip * wip * wip * wip * bump * update tutorials to use body * update tutorials to use DML on destination state enum * Add diagnostic * wip * updated a few more tests * wip * wip * Add another gotcha * wip * wip * wip * fixes * wip * wip * wip * wip * wip * fix * wip * remove for now * wip * wip * updated some docs * migration guides * more migration guide * fix ci * fix * soft deprecate all apis using AnyCasePath * wip * Fix * fix tests * swift-format 509 compatibility * wip * wip * Update Sources/ComposableArchitecture/Macros.swift Co-authored-by: Mateusz Bąk <bakmatthew@icloud.com> * wip * wip * update optional state case study * remove initializer * Don't use @State for BasicsView integration demo * fix tests * remove reduce diagnostics for now * diagnose error not warning * Update Sources/ComposableArchitecture/Macros.swift Co-authored-by: Jesse Tipton <jesse@jessetipton.com> * wip * move integration tests to cron * Revert "move integration tests to cron" This reverts commitf9bdf2f04b. * disable flakey tests on CI * wip * wip * Revert "Revert "move integration tests to cron"" This reverts commit66aafa7327. * fix * wip * fix --------- Co-authored-by: Brandon Williams <mbrandonw@hey.com> Co-authored-by: Mateusz Bąk <bakmatthew@icloud.com> Co-authored-by: Brandon Williams <135203+mbrandonw@users.noreply.github.com> Co-authored-by: Jesse Tipton <jesse@jessetipton.com>
125 lines
3.1 KiB
Swift
125 lines
3.1 KiB
Swift
import ComposableArchitecture
|
|
import SwiftUI
|
|
|
|
private let readMe = """
|
|
This file demonstrates how to handle two-way bindings in the Composable Architecture using \
|
|
binding state and actions.
|
|
|
|
Binding state and actions allow you to safely eliminate the boilerplate caused by needing to \
|
|
have a unique action for every UI control. Instead, all UI bindings can be consolidated into a \
|
|
single `binding` action that holds onto a `BindingAction` value, and all binding state can be \
|
|
safeguarded with the `BindingState` property wrapper.
|
|
|
|
It is instructive to compare this case study to the "Binding Basics" case study.
|
|
"""
|
|
|
|
// MARK: - Feature domain
|
|
|
|
@Reducer
|
|
struct BindingForm {
|
|
struct State: Equatable {
|
|
@BindingState var sliderValue = 5.0
|
|
@BindingState var stepCount = 10
|
|
@BindingState var text = ""
|
|
@BindingState var toggleIsOn = false
|
|
}
|
|
|
|
enum Action: BindableAction {
|
|
case binding(BindingAction<State>)
|
|
case resetButtonTapped
|
|
}
|
|
|
|
var body: some Reducer<State, Action> {
|
|
BindingReducer()
|
|
Reduce { state, action in
|
|
switch action {
|
|
case .binding(\.$stepCount):
|
|
state.sliderValue = .minimum(state.sliderValue, Double(state.stepCount))
|
|
return .none
|
|
|
|
case .binding:
|
|
return .none
|
|
|
|
case .resetButtonTapped:
|
|
state = State()
|
|
return .none
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Feature view
|
|
|
|
struct BindingFormView: View {
|
|
@State var store = Store(initialState: BindingForm.State()) {
|
|
BindingForm()
|
|
}
|
|
|
|
var body: some View {
|
|
WithViewStore(self.store, observe: { $0 }) { viewStore in
|
|
Form {
|
|
Section {
|
|
AboutView(readMe: readMe)
|
|
}
|
|
|
|
HStack {
|
|
TextField("Type here", text: viewStore.$text)
|
|
.disableAutocorrection(true)
|
|
.foregroundStyle(viewStore.toggleIsOn ? Color.secondary : .primary)
|
|
Text(alternate(viewStore.text))
|
|
}
|
|
.disabled(viewStore.toggleIsOn)
|
|
|
|
Toggle("Disable other controls", isOn: viewStore.$toggleIsOn.resignFirstResponder())
|
|
|
|
Stepper(
|
|
"Max slider value: \(viewStore.stepCount)",
|
|
value: viewStore.$stepCount,
|
|
in: 0...100
|
|
)
|
|
.disabled(viewStore.toggleIsOn)
|
|
|
|
HStack {
|
|
Text("Slider value: \(Int(viewStore.sliderValue))")
|
|
|
|
Slider(value: viewStore.$sliderValue, in: 0...Double(viewStore.stepCount))
|
|
.tint(.accentColor)
|
|
}
|
|
.disabled(viewStore.toggleIsOn)
|
|
|
|
Button("Reset") {
|
|
viewStore.send(.resetButtonTapped)
|
|
}
|
|
.tint(.red)
|
|
}
|
|
}
|
|
.monospacedDigit()
|
|
.navigationTitle("Bindings form")
|
|
}
|
|
}
|
|
|
|
private func alternate(_ string: String) -> String {
|
|
string
|
|
.enumerated()
|
|
.map { idx, char in
|
|
idx.isMultiple(of: 2)
|
|
? char.uppercased()
|
|
: char.lowercased()
|
|
}
|
|
.joined()
|
|
}
|
|
|
|
// MARK: - SwiftUI previews
|
|
|
|
struct BindingFormView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
NavigationView {
|
|
BindingFormView(
|
|
store: Store(initialState: BindingForm.State()) {
|
|
BindingForm()
|
|
}
|
|
)
|
|
}
|
|
}
|
|
}
|