Welcome back.
In this video, we are going to build a timer and it's going to be a countdown timer to be precise.
So the idea is that we are going to need a countdown timer for our seven minute workout app.
And therefore, we should check out the demo first in order to understand how timers work, because
we're going to need that in the app.
So the idea here by is also that I'm going to add a pause button, which is not going to be required
in the app that we are going to build.
But I still think it makes sense for you to also see how you could implement that.
All right.
So we have these three buttons start, pause and reset and start, of course, starts to timer.
Then we have palls, which pauses the timer and reset will reset the timer.
I think it's obvious.
So let's check it out past the timer.
You can see the value was 53 then when we first started.
It just goes on from where it left off and then reset will reset the timer pressing.
Start with restart the timer.
OK, so let's have a look at the code for that.
So what we have is we have this countdown timer variable, which we're going to need.
It's a tough time.
Countdown timer nullable.
So I set that to NUL here because we are going to set that once we start to count on timer and you will
see how that looks.
Then we have this timer duration, which is a long value.
So here you enter the timer duration that you want to have in milliseconds.
So not in seconds but in milliseconds.
What that means is just add three zeros to the seconds amount that you want to have and you are going
to be fine.
Then we have this private pause offset, which is going to store the time that passed from starting
the timer to pausing the timer.
So that's pretty much the idea behind this pause offset.
And I'm going to set that to zero because at this point I don't really need to set it to anything.
It will be set once we get to using it.
OK, then we have this uncreate method, as we always do.
And in here I'm setting the TV timer text to the timer duration divided by one thousand two string,
of course, because it has to be of type string.
So what is going to do is it's going to set this text here, which is currently 60.
It's going to set that to whatever the duration is set.
So in this case, it's the same value.
It's going to be 60 either way.
But if you change this value here, then your uncreate will take care of setting the right value to
the text property of the TV timer.
So it will always have the correct value there when starting.
Then we have this button start, which will call the start timer method, which I created, and it will
pass the offset value here.
We're going to look at start timer in a second with the pause button.
When one set is click pause timer is called and with the stop button, once that is click, the reset
timer is called.
OK, so we have this start timer function, which is what's important, right.
So here I'm saying, OK, I want to have a long value when you call this method.
So I'm going to pass and pause.
Also here I call the police FSL just to have a slightly different name, not to create any confusion
here.
All right.
And then I create something for the countdown timer.
So here you can see we are assigning a value to countdown timer.
So before that it was null, but now we are assigning an object to it, which is of type countdown timer.
OK, so this count on timer that we're passing is a whole class pretty much.
So what we're saying is go ahead and override the function that we have here.
So on tick and on finish.
OK, so what I'm doing here, I say I pass in time of duration minus palls offset l.
So what is that.
Well in here you could also just pause and the time of duration.
So for example, 60 seconds but also pass and Paul's offset in order to be able to restart the timer.
Because what happens when we restart a timer is that we actually create a new countdown timer.
So it's not like we are using the old countdown timer object and we're using it's old value, but we
actually resetting it completely.
So we are deleting the old countdown timer and creating a new countdown timer object.
And that's why I'm passing the APOs also in order to maintain that value that I had.
And then we have this countdown interval and here I'm setting that to 1000 milliseconds, which means
the countdown timer interval will be one second so you can set whatever well you want.
But then, of course, the tick will be very different.
So let's look at this on tick.
On tick is what is called every.
Countdown Intervale.
All right, so maybe I should put that in another line, it's a little easier to read them.
So we have this countdown interval, which is said to 1000 milliseconds.
So every 1000 milliseconds on take will be called.
And Unteach also gives us this value called Milice until finish.
So this is the duration that is still left for the whole timer and we can use that.
So what I'm doing is I say, OK, minutes until finish is going to be the whole timer duration, which
is an hour sixty thousand minus the milliseconds until finished, which is whatever time has passed
since the timer has started.
And then we have timer texts and so we set that again to Milice until finished.
So the milliseconds that are still left in the timer divided by one thousand and then we settle to a
string because that's what texts we expect.
OK, and then finally we have this unfinished and unfinished functions are called once something is
finished and in our case once our countdown timer is finished.
So once the time has run up, which means once the 60 seconds have run up or in our case, what I do
here is I just write a little toast here saying timer is finished and it's going to be displayed on
the screen once the time is passed.
OK, and then, of course, we need to start that countdown timer because otherwise nothing will happen.
We will just prepare it.
But we actually want to start it.
So we press start here or we call start.
All right, now let's look at Paul's timer, so we have this Paul's timer function and here I'm checking
if the countdown timer is not now, if it's not null, then cancel it.
So if there is no count on timer, then there's no use in doing anything.
And then we have reset timer.
So here we are resetting the timer, meaning we check if the countdown timer is not now.
If it's not now, then cancel the current timer.
Set it back to now.
So empty the current countdown timer variable and set the TV timer text back to 60 seconds.
OK, so the text to your timer will be set back to 60 seconds.
And of course, we may not forget to set pause of set back to zero because otherwise things will be
very confusing in our application.
All right.
And that's already it.
So now you know how to create a timer.
And of course, there are other ways to doing it.
But that's basically the one that I came up with.
And I think it's a pretty basic one which really does everything we need when it comes to timers.
All right.
So I hope you enjoyed this video.
And now let's go ahead and use timers in all of our apps in the future.
So we'll see you in the next video.