If I have some state that holds an array of items, I can create a scoped store that operates on a single item of that list using the forEach function on the reducer. And when I change the single item, it also updates inside of the list. Great!
struct Model: Equatable, Identifiable {
let id: Int
}
struct AppState: Equatable {
var models: IdentifiedArrayOf<Model> = .init()
}
enum AppAction: Equatable {
case model(id: Int, action: ModelAction)
}
enum ModelAction: Equatable {}
let modelReducer = Reducer<Model, ModelAction, AppEnvironment> { state, action, _ in
return .none
}
let appReducer = Reducer<AppState, AppAction, AppEnvironment>.combine(
Reducer { state, action, _ in
return .none
},
modelReducer(
state: \.models,
action: /AppAction.model(id:action:),
environment: { _ in AppEnvironment() }
)
)
But what if I want the single item state to have a bit more.. state? Not just the bare Model. For example:
struct ModelState: Equatable {
var model: Model
var somethingElse: Bool = false
}
let modelReducer = Reducer<ModelState, ModelAction, AppEnvironment> { state, action, _ in
return .none
}
I can't use a forEach function anymore, since IdentifiedArrayOf<Model> doesn't map to ModelState. So I guess I need to use the pullback function to manually transform this? How can I do that in a way so that I can still create a scoped store where modifying the single Model also gets updated in the AppState's array?