Replies: 1 comment
-
I found a solution, but am not completely happy with it -> Reducer: @Reducer
struct ListFeature {
struct State: Equatable {
var items: IdentifiedArrayOf<ItemFeature.State> = []
}
enum Action {
case items(IdentifiedActionOf<ItemFeature>)
}
@Reducer
struct ItemFeature {
enum State: Identifiable {
case movieItem(MovieItemFature.State)
case seriesItem(SeriesItemFeature.State)
var id: Int {
switch self {
case let .movieItem(state):
return state.id
case let .seriesItem(state):
return state.id
}
}
}
enum Action {
case movieItem(MovieItemFature.Action)
case seriesItem(SeriesItemFeature.Action)
}
var body: some ReducerOf<Self> {
Scope(state: \.movieItem, action: \.movieItem) {
MovieDownloadItemFeature()
}
Scope(state: \.seriesItem, action: \.seriesItem) {
SeriesDownloadItemFeature()
}
}
}
var body: some ReducerOf<Self> {
Reduce { state, action in return .none }
.forEach(\.items, action: \.items) {
ItemFeature()
}
}
} View: ForEachStore(store.scope(state: \.items, action: \.items)) { itemStore in
SwitchStore(itemStore) { downloadItem in
switch downloadItem {
case .movieItem:
CaseLet(
\ListFeature.ItemFeature.State.movieItem,
action: ListFeature.ItemFeature.Action.movieItem
) { movieStore in
MovieDownloadItemView(store: movieStore)
}
case .seriesItem:
CaseLet(
\ListFeature.ItemFeature.State.seriesItem,
action: ListFeature.ItemFeature.Action.seriesItem
) { seriesStore in
SeriesDownloadItemView(store: seriesStore)
}
}
}
} The problem with this solution is that I am able to send MovieItemFeature.Action into the SeriesItemFeature reducer and vice versa. if let item = state.items[id: someId], item.is(\.movieItem) {
return .send(.items(.element(id: someId, action: .movieItem(.wantedAction))))
} I am now wondering if there is some other solution to presenting list of items with different reducers, but in a way that I avoid having the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone. I am facing an issue where I have a
ListFeature
that holds an array of Items in its state. That list should be able to render two types of items. One item is a plain View, and another one is powered with TCA. My question is how could I scope only the TCA feature items to use their reducer?I have found a similar discussion but I was not able to find a solution to my code.
Thank you 🙏
Sample code:
Beta Was this translation helpful? Give feedback.
All reactions