Optional store & Dependent state #543
-
Let's say we have a home module (parent vc) inside of which we have a feed of groceries (1st child view controller) in a table view and then a in the bottom right corner a on sale feature (2nd child vc) that hides/shows based on if there are any products in the visible cells in the feed of groceries (from the 1st child vc) that are on sale. So we have the parent vc that contains the product feed feature and the on sale indicator feature. Ideally we would want to have the 2nd child vc to be added to the view hierarchy and then hide/show based when we have any products on sale in the feed (1st child vc). The issue is that we can no longer init the 2nd child vc with any store since this store has a state that is only there when there are products on sale in the feed. Ideally we would want to have the 2nd child vc initialised and just show/hide (e.g. animate the alpha in/out) + update the state based on the items we catch to be on sale in the feed. I can think of a 'dirty' approach to get this working however was interested to see what the others think... To be more clear I have jotted down a quick example (it can be compiled):
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
@DavidKmn The subscription block emits whenever the state switches from You can find more example uses in the TicTacToe demo application in this repo. |
Beta Was this translation helpful? Give feedback.
@DavidKmn
store.ifLet
is precisely the tool for turning a store of optional state into a store of non-optional state:swift-composable-architecture/Sources/ComposableArchitecture/UIKit/IfLetUIKit.swift
Line 46 in 68aa3c1
The subscription block emits whenever the state switches from
nil
to non-nil
and vice versa, so you can create or tear down this optional view accordingly.You can find more example uses in the TicTacToe demo application in this repo.