Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data Flow through TCACoordinators? #30

Closed
rhysm94 opened this issue Jul 14, 2022 · 4 comments
Closed

Data Flow through TCACoordinators? #30

rhysm94 opened this issue Jul 14, 2022 · 4 comments

Comments

@rhysm94
Copy link
Contributor

rhysm94 commented Jul 14, 2022

I'm having some issues coming up with a way to send data from my nested views back to my Coordinator state, when building a multi-step form.

I've created a repository with a sample SwiftPM application, with a three step form, and a submission page. Currently, they don't have any means of communication with each other.

In this example codebase, I'd like to be able to persist the values in Step 1, Step 2, and Step 3, so that the user can go back and forth between each page without losing any data, but I can't find any hook for when the current page state is updated. The updateRoutes action doesn't get called when I'd expect it to, only seemingly when the view is dismissed, not presented on top of.

Is there something I'm missing with this? It feels like I should be able to keep Step 1, 2, and 3 state in my Coordinator state (or even a parent state), so that next time the user navigates from Step 1 -> Step 2, I can call:

state.routes.push(.step2(state.step2State))

and have that view pre-populated with whatever was entered from when they were last on the Step 2 page.

Any help here would be massively appreciated! Thanks!

johnpatrickmorgan added a commit that referenced this issue Jul 14, 2022
@johnpatrickmorgan
Copy link
Owner

johnpatrickmorgan commented Jul 14, 2022

Hi @rhysm94! Thanks for raising this issue, and for the example code. I've imported your code into the example project and adapted it in this commit. Essentially, I've added state to the coordinator for each of the steps as you said. With the state held in the coordinator, the stored routes no longer need to store data, just a screen identifier - IdentifiedArrayOf<Route<AppFlowState.ID>>. But in order to hook up to the router, a IdentifiedArrayOf<Route<AppFlowState>> computed variable is used. With that, you can just push an ID to push a new screen, state.routes.push(.step2). The important changes I made are in the FormAppCoordinator.swift. There might be other approaches but I think that way works well. Hope that helps.

@rhysm94
Copy link
Contributor Author

rhysm94 commented Jul 15, 2022

Thank you very much! That worked perfectly.

I did hit a snag at one point, on the actual project this is for, which involves a few more screens. For some reason, it was compiling just fine, but I was hitting an EXC_BAD_ACCESS code=1 crash in a WithViewStore initialiser, on the removeDuplicates: == argument, as soon as I was trying to launch the coordinator. Not entirely sure why, or how it was happening, but after retrying a few times and probably fiddling with the code, it just worked 🤷🏻‍♂️

Anyway, thanks again - this has been an enormous help!

@johnpatrickmorgan
Copy link
Owner

Great, glad it helped! I might adapt this demo to include in the example app if that's ok?

@rhysm94
Copy link
Contributor Author

rhysm94 commented Jul 15, 2022

Yes, absolutely! More than happy 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants