Imagine the following state and views composition:
AppState
Shows: IdentifiedArrayOf<Show>
Tags: IdentifiedArrayOf<Tag>
Screen 1
List of
Show
states using
ForEachStore
.
Screen 2
Details of selected Show
state, and a list of tags from a global Tags
state, with the ability to attach tags to the selected show.
I want Tags
to be a global state because it holds information about all tags available, which should be shared across all Show
states.
I tried passing the Tags
state to the Show
state initializer, but any changes made to the Tags
state are of course not propagated back up with this approach.
My question is, how could one structure the state to achieve the goal described above with TCA?
What about storing this states separately? Do you use Rx / Combine?
Using Rx, I do the following:
let tags: Observable = ...
let showsSource: Observable = ...
let shows = Observable.combineLatest(tags, showsSource).map { tags, showsSource -> T in
// mix Shows & Tags
}
The same can be done without Rx, may be with more difficulties
@Dmitriy_Ignatyev thanks for sharing, but I'm using The Composable Architecture (TCA).
Oh, sorry, I missed that. Seems like Redux.
Is there an action like 'case tagsUpdate(tags: [Tag])'?
If yes, it can be done like this:
let reducer = Reducer<AppState, AppAction, AppEnvironment> { state, action, _ in
switch action {
case tagsUpdate(let tags):
state.tags = tags
state.shows = state.shows.map { $0.copy(withNewTags: tags) }
}
}
extension Show {
copy(withNewTags: [Tag]) -> Self {
Self.init(...)
}
}