4.4 More State Transitions
We are getting near to the finish line and only have a couple more state transitions to take care of. No time to rest now. Let's get to work!
1.Introduction2 lessons, 05:00
2.Building the Foundation3 lessons, 27:38
3.Creating the User Interface7 lessons, 1:06:19
4.Application Logic6 lessons, 37:10
5.Conclusion1 lesson, 00:38
4.4 More State Transitions
As you can see here, we have a couple more functions that we need to implement. And in the last lesson, we got to a place where we ended a particular stage. And now we wanna be able to get to the next stage. So in that case, if you see up here, once we get to that StageDone state, we want to advance. So let's work on that function here. Now this should be fairly simplistic. So what we wanna do is we want to increment our activeStageIndex variable by one. And then we need to check to see if another stage exists at that particular index. Then we can do one operation, and if it doesn't, then we'll do another. So let's begin by saying our activeStageIndex, and let's go ahead and increment that by 1. Now we need to check a couple things. We could say if the activeReminder?.stages.count is greater than the activeStageIndex, then that means we have at least one more stage. So we can say, let stage = activeReminder.stages. And we can get the activeStageIndex, so that should give us the next stage in line. Then we'll set the activeStage = stage. Then we can set our state = .Ready again because we're ready to run. And then we'll change the actionButton. We'll use the setTitle property again to say "Start". We’ll go ahead and save that. So now what do we do if there are no more stages? At this point we’re at the end of a stage, and if there are no more stages, then that means we're at the end of the actual reminder itself. So in this case I can say else, and then within here I'll say, actionButton.setTitle. And we'll just say that this is going to be "Reset", because remember that's what we wanna do if we ever get to that particular phase. And then we'll say the state here is gonna be equal to .Done. We'll go ahead and save that. And then placing this line right here, where we set the action title to "Start". Let's go ahead and copy that. Because I know one thing that we need to do, that we haven't done as of yet, is we haven't set that to be the initial button text upon awake. So once we've done that, we can go ahead and drop that same line in here within the awake function. Let's go ahead and save all of that, and we'll stop our debugger. And while we're doing this, let's go ahead and adjust these timers just a little bit so that we can see the difference. So let's just put this at 5 seconds just to make it a little bit quicker. And then we'll go ahead and run our application again. So now at this point, we should see that we do our initial stage. It should get down to 0. We should change the button text to say, Next Stage. And then once we hit that, it should transition to the next one. So let's go ahead and check out Morning Wakeup. We see we have 5 seconds here. We have our Start button. Let’s click Start. We’re counting down 3, 2, 1, 0. Now we should say Next Stage. If I hit Next Stage, I have now transitioned over to the next one, which is a Light Jog, for 60 seconds. And now I should be able to hit this again, and it's gonna start counting down all the way again. So we're starting to get to this point where we have these multiple stages and we can transition in and out of them. And I should also be able to hit End Stage at this point, but that's not doing anything just yet. So let's go ahead and handle that scenario right now. So when I hit that End Stage, what I'd like to do is I would like to come in here and I would like to call my endStage function right here. And this is gonna be a fairly simplistic one. We're just going to kind of fast forward. So we're gonna set the state = .StageDone. And then we just kinda have to do a little bit of resetting here. So we're gonna say actionButton.setTitle. And we're gonna put this to "Next Stage" because we're going to assume at this point that there is one, even though there might not be. But we're gonna handle that checking on the advance here. Then we have to clean up our timers a little bit, so we're going to stop our interface timer. We'll say, let now = Date, with an IntervalSinceNow of 0. So this is how we're going to reset it back down to 0 to kind of fast forward it. We'll say interfaceTimer.setDate = now. And then we'll also invalidate our timer. That way that's going to stop as well so we don't get any additional calls to our timerEnded function to make it seem like something else has happened. So let's go ahead and save that. So let's go ahead and rerun our application one more time. And once our simulator has come up, we should see a list of our reminders. I can select this one. I'll hit Start. We should count down in 3, 2, 1, 0. We should get Next Stage. I can transition to the next one. And at this point, I can start this stage. And then I can also hit End Stage, and that should reset everything down to 0 so it looks as though there’s nothing left. And at this point, I wanna hit Next Stage again. And I want there to be a reset, which there is, because I got into the state where there are no more stages. But at this point now, we have nothing happening. And we're gonna finish this state up, as well as provide a little bit of a notification to the end user at the end of each of these stages, in the next lesson.