Collection of Reducers, Scope and item access #2492
-
Hi everyone, still continuing to learn TCA, and I encoutered a weird case, I don't how to resolve this one 😓 I ordered my objects like this basically enum IngredientType {
case flour
case sugar
case chocolate
case aubergine
case courgette
case butternut
// yes, very eclectic :D :D
}
struct Ingredient {
let type: IngredientType
var nbOfItems = 0
}
struct IngredientSection {
let title: String
var ingredients: [Ingredient]
} I have a view with a stepper that allows me to increment the nb of a particular item. So I created a Reducer that holds an Ingredient as a State and it works fine. Now I would like to put it all together with a double scroll view (an horizontal list of Ingredients ("Section") and a vertical list of sections, just like the App Store App for example). So I have a reducer's State that will look like this struct State: Equatable {
@BindingState var sections: [IngredientSection]
} I managed to create the view, but I'm stuck when I have to A Stepper needs a binding, how can I use it with a Collection (Array, set, ...) ForEach(viewStore.section) { section in
VStack {
Text(section.title)
HStack {
ForEach(section.ingredient) ( ingredient in
Stepper("",
value: viewStore.$section[???] // <-- how can I use index here ? $section is a Binding, so $section[index] does not work
)
}
}
}
} Is it an architectural issue or more likely, the geniuses of PointFree have developed a stuff I'm not aware of :D Thanks for your help <3 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @JeromeTonnelierOgury, I think you can handle this just like you would in a regular, vanilla SwiftUI application. At the root reducer feature level you can add a ForEach(viewStore.binding(get: \.section, send: Feature.Action.setSection) { $section in
…
} This uses the initializer of Then you can just keep deriving bindings to smaller and smaller pieces of the state by using dynamic member lookup on ForEach(viewStore.binding(get: \.section, send: Feature.Action.setSection) { $section in
VStack {
Text(section.title)
HStack {
ForEach($section.ingredient) ( $ingredient in
Stepper("", value: $ingredient)
}
}
}
} If the elements of your |
Beta Was this translation helpful? Give feedback.
Hi @JeromeTonnelierOgury, I think you can handle this just like you would in a regular, vanilla SwiftUI application.
At the root reducer feature level you can add a
setSection
action for updating the section (or you can use@BindingState
, but that may be more work than necessary). Once that is done you can derive a binding to thesection
field:This uses the initializer of
ForEach
that takes a binding to a collection and products a binding to each individual element (hence there$section
argument).Then you can just keep deriving bindings to smaller and smaller pieces of the state by using dyna…