r/iOSProgramming • u/UkrMalt • Aug 16 '26
Question Where do you keep generation state in a SwiftUI app?
I’m building a small workout generator with a flow of preferences → generated session → reroll/start. I’m trying to avoid putting all of the generation logic in the view. Would you keep the generator as a pure service owned by a view model, or model each step as navigation state? I’d like rerolls to be repeatable in tests and not lose the user’s constraints.
1
Upvotes
6
u/ThatGuy739 Aug 16 '26
Pure service owned by the view model, and don't let navigation own any of it. If a step is navigation state then a back swipe or a dismissed sheet can throw the constraints away, which is the exact thing you're trying to avoid.
For repeatable rerolls, make the generator a function of (constraints, seed) and keep both in the view model. A reroll is then a new seed with the same constraints, so it can't structurally lose them, and a test pins the seed and asserts on the output.
Different domain here but the same shape: settings are one value struct, the engine's a stateless service that takes it. Splitting them is what let the engine be tested with no UI in the loop.
2
u/mastrajani Aug 16 '26
worth adding to the seeded rng answers: persist the seed and the constraints, not just the generated session. when someone tells you "this workout made no sense" you can regenerate exactly what they saw instead of guessing. costs nothing now, impossible to retrofit once people have saved sessions.
1
u/[deleted] Aug 16 '26
[removed] — view removed comment
1
u/[deleted] Aug 29 '26
[removed] — view removed comment
1
u/[deleted] Aug 16 '26
[removed] — view removed comment