FREELessons: 19Length: 2.3 hours

• Overview
• Transcript

# 4.1 Add State to the App

The overall design of this app is going to be based on states. At any given time, our app will be in one of four states, so it will need to be written so that it can transition between any of those states.

## 5.Conclusion1 lesson, 00:38

### 4.1 Add State to the App

Now, let's start to introduce the idea of transitioning between states within our application. So one of the more common ways that you can do this is using some sort of enumeration to define what state you are currently in within your application. So what I'm gonna do is I'm going to come out here on top of my class definition. And you can do this in a separate file if you want, but just to keep things simple for now, I'm gonna create a new enumeration and I'm gonna call it reminder states. So then within here I can create a number of case statements that are gonna define what these states are within my application, and as of right now, there's going to be four to start with. Now, you could add to this, obviously, and make it a little bit more granular, but we're gonna keep things simple, so we're gonna consider ourselves in a ready state when we first come into our application when nothing's really happening. When no active stage is currently in process. And then we're gonna transition from there into a running state. So that means at any point, in time a given stage within my reminder can be running. And then we can go from there to being stage done, which means whatever stage was currently running is now done. And then the entire reminder can be done in and of itself. So these are just four that I've defined right now, but like I've said before, you can change this to be just about anything that you want. So now what we're going to do is we're going to say that when we initially come into our reminder interface controller and we're getting started with everything, the initial state is going to be ready. So we're just going to create a private variable here. A private variable that's going to be state, and I'm gonna set that equal to ReminderStates.Ready. So that's where we are going to start, and we're actually going to make this active index private as well. And we're also going to set the activeStage equal to private as well. We're gonna keep those things private, so that we're only accessing those things inside our ReminderInterfaceController. And nobody on the outside can really dictate these variables. So that's where we're gonna start at. So now I want to start to transition in between all of these different states and how am I going to do that? Well, I'm going to be doing that typically with an action button clicked event. So now that I have this function in here I can search and tie these things together. So when a button is clicked, I'm going to run a switch statement here, and I'm going to switch on whatever my current state is. So that way we can come in here, and start to fill in the different cases in which we're going to handle these things. So we have a ready state. And I can already see that it's not liking state, because I wrote stage down here below. This is actually going to be state, and then from here I can have a ready state. I can copy this and drop down here, so I can go into a running state. I can be from there, I can go into a stage done. So we'll say stage done. And then, we'll go into, A done. Now, one of the nice things about using an enumeration for something like this is that when I come into my switch statement, there is a predefined number of states, or number of values, or cases within that enumeration. So, I can actually get rid of this default, because I know I'm handling all the different situations, or all the different scenarios. Okay. So now I can start to handle these different states in certain ways. So how can we do that? Well, one of the ways that you can do that is via some sort of transitioning functions. So what I want to do now, is I want to have a couple of different functions that are going to help me transition between the different states. So I'm going to come down here and I'm going to create a couple of different private functions. So another private function here, and the first one is going to be say start. And I wanna call start when I'm in the ready state and I click this button. So I'm going to execute the start function at this point, and the next one that I'm gonna want is going to be end stage. So I can short circuit where I'm at, within the process. So I'll say, End Stage. And this is going to happen when I'm in a running state. So I'll call End Stage running. Let's create another private function here. And this one is going to be, when I am currently in a stage-done state, then I wanna be able to advance to the next stage. So we'll just say, we'll call this advance, so if I am at a StageDone state. Then I am going to call the advanced function. And then, when I am in a done state, I am going to simply do a reset. So we'll do private function. We'll do reset like this. And if I am in a done state. I will do reset just like this. Okay. So let's go ahead and save that. So as you can start to see we're starting to get a little bit of a flow here within our application. We have four different stages, I'm gonna transition between those stages based on some button click events as well as some timer things that we're gonna work on in the next lesson. And we're gonna be able to use all these things to transition between different states and provide different functionality and a different user experience based on those different states of our application.

Back to the top