I'm creating a basic app that needs state variables to be used in multiple parts. It's a multiplication practice app. I intend to gamify it a bit but for now, I'm setting up the basics.
My initial thought is to set up an Observer Pattern for state management, but I'm having trouble doing that in swiftUI.
I have a Number Pad component that holds a number pad I created as well as the state variable for the user's answer.
I have a number Window component that holds 2 numbers from the game logic. Finding the product of these numbers is supposed to increment the score and change the numbers.
My content view contains the 2 components and a box that displays the current number entered by the user. They are all inside one Vstack. I also import my game logic class as a stateObject in the content view
The layout looks alright. I can get 2 random numbers to show up on start of the app. I also have the user input from the number pad displaying in a box in the content view.
But getting the userInput to match the game logics expected answer is where I'm running into trouble. And when I change something everything else breaks. I'm pretty sure it's my state management.
I've done some reading about Observable Object, state, binding, published and stateobject imports. So I'm semi-familiar with them, just not how to use them effectively.
How would you approach getting your game logic state variables (leftNum, rightNum, and Answer) to display to the number window? While also waiting for the user input to match the Answer variable and run some methods?
What components would you have extending Observable object vs just using state or binding?
Thanks for your time
Below is a visual of my app preview