Maybe I'm going about this in entirely the wrong way, if so, please let me know.
Anyway, in the "Home" screen of our app it essentially acts as a "Springboard" like service for other features in the app. Each view has it's own state and own loading etc...
However, some of the views may decide after their state is loaded that actually the view on the home screen shouldn't exist.
For instance, lets say you have banking app and you have a section for "Savings accounts" but you actually don't have any savings accounts open. So after any "loading" animation or whatever we will hide that view.
However... it might be the case that later on in the app you decide to open a savings account. If that happens then when you come back to the home screen it will be listed there in the "Savings Account" section.
At the moment I have each section in the view having its own child TCA "module". Such that the "Home" screen knows very little. It just loads the different modules. The modules then provide a view (or not) that the home screen will show.
For example in the hypothetical banking app...
ScrollView(.vertical, showsIndicators: false) {
VStack(spacing: 0) {
BalanceView(
store: store.scope(state: \.balanceHeading, action: HomeFeature.Action.balanceHeading)
)
SavingsAccountsView(
store: store.scope(state: \.savingsAccounts, action: HomeFeature.Action.savingsAccounts)
)
CreditCardsView(
store: store.scope(state: \.creditCards, action: HomeFeature.Action.creditCards)
)
// ... and so on
}
.frame(maxWidth: .infinity)
}
Now, inside each of these views at the moment I'm using a .task
so that it can subscribe to a local cache dependency and update itself with the relevant data. But I don't know how to make the "thie view should not appear" bit work nicely without making it a bit hacky.
At the moment I'm just telling it to display Color.clear.frame(height: 0)
if it does not have any data. But this causes issues with spacing etc...
If I change it to use EmptyView()
then the .task
no longer runs on subsequent appearances of the view.
Ideally, each time this screen appears it should reevaluate all its views and only show the ones that actually have a view to return. And update any where the data has changed.
Hmm... maybe I could have a computed var / extension of the State
that stores the full array of sections and filters it based on whether each section has the required data and then each time it appears it would do the same thing.
The problem then is that we still are not showing the view and so the view will not run the task which will not subscribe to the cache and not get the data.
I really want to avoid having all of this logic and data stored inside the HomeFeature
as it will just bloat it up to a ridiculous size. (Which is where we are in the old version of the app right now).
I hope these incoherent ramblings are making some sort of sense. It would be excellent to have any sort of input even just rough pointers of how to think about a solution here.
Thanks