For the last few months, I have been writing about State Machine. As the final article of this series and the first article of the year, I’ll show you how to implement State Machine in a real-life scenario, using as an example an Uber Clone App.
A few years ago, I did this sample of an Uber Clone app, so I’ll take this sample and modify it to use a State Machine.
Let’s start
As I mentioned in a previous article, the first step when working with State Machines is to define the states that will be handled.
These states can be represented using an Enum:
Create another Enum to define the triggers for each state:
Let’s code
After installing the Stateless package, we create the State machine and configure the initial state:
To connect the State with the UI, we expose a State property that will represent the current state:
Now that we have the UI + ViewModel connected, we can define the rest of our states:
Result:
That’s all for now.
Check the full source code here.
NOTE: This code has a lot of room for improvement, I made this quick sample just to show the concept.
Happy Stateless.