|
1 |
| -#if compiler(>=5.4) |
2 |
| - import ComposableArchitecture |
3 |
| - import SwiftUI |
4 |
| - |
5 |
| - private let readMe = """ |
6 |
| - This file demonstrates how to handle two-way bindings in the Composable Architecture using \ |
7 |
| - bindable state and actions. |
8 |
| -
|
9 |
| - Bindable state and actions allow you to safely eliminate the boilerplate caused by needing to \ |
10 |
| - have a unique action for every UI control. Instead, all UI bindings can be consolidated into a \ |
11 |
| - single `binding` action that holds onto a `BindingAction` value, and all bindable state can be \ |
12 |
| - safeguarded with the `BindableState` property wrapper. |
13 |
| -
|
14 |
| - It is instructive to compare this case study to the "Binding Basics" case study. |
15 |
| - """ |
16 |
| - |
17 |
| - // The state for this screen holds a bunch of values that will drive |
18 |
| - struct BindingFormState: Equatable { |
19 |
| - @BindableState var sliderValue = 5.0 |
20 |
| - @BindableState var stepCount = 10 |
21 |
| - @BindableState var text = "" |
22 |
| - @BindableState var toggleIsOn = false |
| 1 | +import ComposableArchitecture |
| 2 | +import SwiftUI |
| 3 | + |
| 4 | +private let readMe = """ |
| 5 | + This file demonstrates how to handle two-way bindings in the Composable Architecture using \ |
| 6 | + bindable state and actions. |
| 7 | +
|
| 8 | + Bindable state and actions allow you to safely eliminate the boilerplate caused by needing to \ |
| 9 | + have a unique action for every UI control. Instead, all UI bindings can be consolidated into a \ |
| 10 | + single `binding` action that holds onto a `BindingAction` value, and all bindable state can be \ |
| 11 | + safeguarded with the `BindableState` property wrapper. |
| 12 | +
|
| 13 | + It is instructive to compare this case study to the "Binding Basics" case study. |
| 14 | + """ |
| 15 | + |
| 16 | +// The state for this screen holds a bunch of values that will drive |
| 17 | +struct BindingFormState: Equatable { |
| 18 | + @BindableState var sliderValue = 5.0 |
| 19 | + @BindableState var stepCount = 10 |
| 20 | + @BindableState var text = "" |
| 21 | + @BindableState var toggleIsOn = false |
| 22 | +} |
| 23 | + |
| 24 | +enum BindingFormAction: BindableAction, Equatable { |
| 25 | + case binding(BindingAction<BindingFormState>) |
| 26 | + case resetButtonTapped |
| 27 | +} |
| 28 | + |
| 29 | +struct BindingFormEnvironment {} |
| 30 | + |
| 31 | +let bindingFormReducer = Reducer< |
| 32 | + BindingFormState, BindingFormAction, BindingFormEnvironment |
| 33 | +> { |
| 34 | + state, action, _ in |
| 35 | + switch action { |
| 36 | + case .binding(\.$stepCount): |
| 37 | + state.sliderValue = .minimum(state.sliderValue, Double(state.stepCount)) |
| 38 | + return .none |
| 39 | + |
| 40 | + case .binding: |
| 41 | + return .none |
| 42 | + |
| 43 | + case .resetButtonTapped: |
| 44 | + state = BindingFormState() |
| 45 | + return .none |
23 | 46 | }
|
| 47 | +} |
| 48 | +.binding() |
| 49 | + |
| 50 | +struct BindingFormView: View { |
| 51 | + let store: Store<BindingFormState, BindingFormAction> |
| 52 | + |
| 53 | + var body: some View { |
| 54 | + WithViewStore(self.store) { viewStore in |
| 55 | + Form { |
| 56 | + Section(header: Text(template: readMe, .caption)) { |
| 57 | + HStack { |
| 58 | + TextField("Type here", text: viewStore.binding(\.$text)) |
| 59 | + .disableAutocorrection(true) |
| 60 | + .foregroundColor(viewStore.toggleIsOn ? .gray : .primary) |
| 61 | + |
| 62 | + Text(alternate(viewStore.text)) |
| 63 | + } |
| 64 | + .disabled(viewStore.toggleIsOn) |
24 | 65 |
|
25 |
| - enum BindingFormAction: BindableAction, Equatable { |
26 |
| - case binding(BindingAction<BindingFormState>) |
27 |
| - case resetButtonTapped |
28 |
| - } |
| 66 | + Toggle( |
| 67 | + "Disable other controls", |
| 68 | + isOn: viewStore.binding(\.$toggleIsOn) |
| 69 | + .resignFirstResponder() |
| 70 | + ) |
29 | 71 |
|
30 |
| - struct BindingFormEnvironment {} |
| 72 | + Stepper(value: viewStore.binding(\.$stepCount), in: 0...100) { |
| 73 | + Text("Max slider value: \(viewStore.stepCount)") |
| 74 | + .font(.body.monospacedDigit()) |
| 75 | + } |
| 76 | + .disabled(viewStore.toggleIsOn) |
31 | 77 |
|
32 |
| - let bindingFormReducer = Reducer< |
33 |
| - BindingFormState, BindingFormAction, BindingFormEnvironment |
34 |
| - > { |
35 |
| - state, action, _ in |
36 |
| - switch action { |
37 |
| - case .binding(\.$stepCount): |
38 |
| - state.sliderValue = .minimum(state.sliderValue, Double(state.stepCount)) |
39 |
| - return .none |
| 78 | + HStack { |
| 79 | + Text("Slider value: \(Int(viewStore.sliderValue))") |
| 80 | + .font(.body.monospacedDigit()) |
40 | 81 |
|
41 |
| - case .binding: |
42 |
| - return .none |
| 82 | + Slider(value: viewStore.binding(\.$sliderValue), in: 0...Double(viewStore.stepCount)) |
| 83 | + } |
| 84 | + .disabled(viewStore.toggleIsOn) |
43 | 85 |
|
44 |
| - case .resetButtonTapped: |
45 |
| - state = BindingFormState() |
46 |
| - return .none |
47 |
| - } |
48 |
| - } |
49 |
| - .binding() |
50 |
| - |
51 |
| - struct BindingFormView: View { |
52 |
| - let store: Store<BindingFormState, BindingFormAction> |
53 |
| - |
54 |
| - var body: some View { |
55 |
| - WithViewStore(self.store) { viewStore in |
56 |
| - Form { |
57 |
| - Section(header: Text(template: readMe, .caption)) { |
58 |
| - HStack { |
59 |
| - TextField("Type here", text: viewStore.binding(\.$text)) |
60 |
| - .disableAutocorrection(true) |
61 |
| - .foregroundColor(viewStore.toggleIsOn ? .gray : .primary) |
62 |
| - |
63 |
| - Text(alternate(viewStore.text)) |
64 |
| - } |
65 |
| - .disabled(viewStore.toggleIsOn) |
66 |
| - |
67 |
| - Toggle( |
68 |
| - "Disable other controls", |
69 |
| - isOn: viewStore.binding(\.$toggleIsOn) |
70 |
| - .resignFirstResponder() |
71 |
| - ) |
72 |
| - |
73 |
| - Stepper(value: viewStore.binding(\.$stepCount), in: 0...100) { |
74 |
| - Text("Max slider value: \(viewStore.stepCount)") |
75 |
| - .font(.body.monospacedDigit()) |
76 |
| - } |
77 |
| - .disabled(viewStore.toggleIsOn) |
78 |
| - |
79 |
| - HStack { |
80 |
| - Text("Slider value: \(Int(viewStore.sliderValue))") |
81 |
| - .font(.body.monospacedDigit()) |
82 |
| - |
83 |
| - Slider(value: viewStore.binding(\.$sliderValue), in: 0...Double(viewStore.stepCount)) |
84 |
| - } |
85 |
| - .disabled(viewStore.toggleIsOn) |
86 |
| - |
87 |
| - Button("Reset") { |
88 |
| - viewStore.send(.resetButtonTapped) |
89 |
| - } |
90 |
| - .foregroundColor(.red) |
| 86 | + Button("Reset") { |
| 87 | + viewStore.send(.resetButtonTapped) |
91 | 88 | }
|
| 89 | + .foregroundColor(.red) |
92 | 90 | }
|
93 | 91 | }
|
94 |
| - .navigationBarTitle("Bindings form") |
95 | 92 | }
|
| 93 | + .navigationBarTitle("Bindings form") |
96 | 94 | }
|
97 |
| - |
98 |
| - private func alternate(_ string: String) -> String { |
99 |
| - string |
100 |
| - .enumerated() |
101 |
| - .map { idx, char in |
102 |
| - idx.isMultiple(of: 2) |
103 |
| - ? char.uppercased() |
104 |
| - : char.lowercased() |
105 |
| - } |
106 |
| - .joined() |
107 |
| - } |
108 |
| - |
109 |
| - struct BindingFormView_Previews: PreviewProvider { |
110 |
| - static var previews: some View { |
111 |
| - NavigationView { |
112 |
| - BindingFormView( |
113 |
| - store: Store( |
114 |
| - initialState: BindingFormState(), |
115 |
| - reducer: bindingFormReducer, |
116 |
| - environment: BindingFormEnvironment() |
117 |
| - ) |
| 95 | +} |
| 96 | + |
| 97 | +private func alternate(_ string: String) -> String { |
| 98 | + string |
| 99 | + .enumerated() |
| 100 | + .map { idx, char in |
| 101 | + idx.isMultiple(of: 2) |
| 102 | + ? char.uppercased() |
| 103 | + : char.lowercased() |
| 104 | + } |
| 105 | + .joined() |
| 106 | +} |
| 107 | + |
| 108 | +struct BindingFormView_Previews: PreviewProvider { |
| 109 | + static var previews: some View { |
| 110 | + NavigationView { |
| 111 | + BindingFormView( |
| 112 | + store: Store( |
| 113 | + initialState: BindingFormState(), |
| 114 | + reducer: bindingFormReducer, |
| 115 | + environment: BindingFormEnvironment() |
118 | 116 | )
|
119 |
| - } |
| 117 | + ) |
120 | 118 | }
|
121 | 119 | }
|
122 |
| -#endif |
| 120 | +} |
0 commit comments