|
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 = .init() |
45 |
| - return .none |
| 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 |
46 | 23 | }
|
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) |
65 | 24 |
|
66 |
| - Toggle("Disable other controls", isOn: viewStore.binding(\.$toggleIsOn)) |
| 25 | + enum BindingFormAction: BindableAction, Equatable { |
| 26 | + case binding(BindingAction<BindingFormState>) |
| 27 | + case resetButtonTapped |
| 28 | + } |
67 | 29 |
|
68 |
| - Stepper(value: viewStore.binding(\.$stepCount), in: 0...100) { |
69 |
| - Text("Max slider value: \(viewStore.stepCount)") |
70 |
| - .font(Font.body.monospacedDigit()) |
71 |
| - } |
72 |
| - .disabled(viewStore.toggleIsOn) |
| 30 | + struct BindingFormEnvironment {} |
73 | 31 |
|
74 |
| - HStack { |
75 |
| - Text("Slider value: \(Int(viewStore.sliderValue))") |
76 |
| - .font(Font.body.monospacedDigit()) |
| 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 |
77 | 40 |
|
78 |
| - Slider(value: viewStore.binding(\.$sliderValue), in: 0...Double(viewStore.stepCount)) |
| 41 | + case .binding: |
| 42 | + return .none |
| 43 | + |
| 44 | + case .resetButtonTapped: |
| 45 | + state = .init() |
| 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("Disable other controls", isOn: viewStore.binding(\.$toggleIsOn)) |
| 68 | + |
| 69 | + Stepper(value: viewStore.binding(\.$stepCount), in: 0...100) { |
| 70 | + Text("Max slider value: \(viewStore.stepCount)") |
| 71 | + .font(Font.body.monospacedDigit()) |
| 72 | + } |
| 73 | + .disabled(viewStore.toggleIsOn) |
| 74 | + |
| 75 | + HStack { |
| 76 | + Text("Slider value: \(Int(viewStore.sliderValue))") |
| 77 | + .font(Font.body.monospacedDigit()) |
| 78 | + |
| 79 | + Slider(value: viewStore.binding(\.$sliderValue), in: 0...Double(viewStore.stepCount)) |
| 80 | + } |
| 81 | + .disabled(viewStore.toggleIsOn) |
79 | 82 | }
|
80 |
| - .disabled(viewStore.toggleIsOn) |
81 | 83 | }
|
82 | 84 | }
|
| 85 | + .navigationBarTitle("Bindings form") |
83 | 86 | }
|
84 |
| - .navigationBarTitle("Bindings form") |
85 | 87 | }
|
86 |
| -} |
87 |
| - |
88 |
| -private func alternate(_ string: String) -> String { |
89 |
| - string |
90 |
| - .enumerated() |
91 |
| - .map { idx, char in |
92 |
| - idx.isMultiple(of: 2) |
93 |
| - ? char.uppercased() |
94 |
| - : char.lowercased() |
95 |
| - } |
96 |
| - .joined() |
97 |
| -} |
98 |
| - |
99 |
| -struct BindingFormView_Previews: PreviewProvider { |
100 |
| - static var previews: some View { |
101 |
| - NavigationView { |
102 |
| - BindingFormView( |
103 |
| - store: Store( |
104 |
| - initialState: BindingFormState(), |
105 |
| - reducer: bindingFormReducer, |
106 |
| - environment: BindingFormEnvironment() |
| 88 | + |
| 89 | + private func alternate(_ string: String) -> String { |
| 90 | + string |
| 91 | + .enumerated() |
| 92 | + .map { idx, char in |
| 93 | + idx.isMultiple(of: 2) |
| 94 | + ? char.uppercased() |
| 95 | + : char.lowercased() |
| 96 | + } |
| 97 | + .joined() |
| 98 | + } |
| 99 | + |
| 100 | + struct BindingFormView_Previews: PreviewProvider { |
| 101 | + static var previews: some View { |
| 102 | + NavigationView { |
| 103 | + BindingFormView( |
| 104 | + store: Store( |
| 105 | + initialState: BindingFormState(), |
| 106 | + reducer: bindingFormReducer, |
| 107 | + environment: BindingFormEnvironment() |
| 108 | + ) |
107 | 109 | )
|
108 |
| - ) |
| 110 | + } |
109 | 111 | }
|
110 | 112 | }
|
111 |
| -} |
| 113 | +#endif |
0 commit comments