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 a 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 button start, pause and reset and start, of course, starts the timer.
Then we have pause, 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 fifty three.
Then when we first start, it just goes on from where it left off and then reset will reset the timer.
Pressing start would then 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 of type countdown timer, Nullarbor.
So I set that to null here because we're going to set that once we start the countdown 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 VAR pause offset, which is going to store the time that passed from starting
to 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 concrete method as we always do.
And in here I'm setting the TV timer text to the timer duration divided by one thousand to string,
of course, because it has to be of type string.
So what it's 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 you're on.
Create will take care of setting the right value to the text property of the TV timer, so we'll 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 side 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 offset here.
I call.
The police have said l just 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 countdown timer that we're passing is a whole class, pretty much.
So what we're saying is go ahead and override the two function that we have here.
So on and on finish.
OK, so what I'm doing here, I say I pass in timer duration minus pause offset l.
So what is that?
Well, in here you could also just pause and the timer duration.
So for example, 60 seconds.
But I also parse and pause offset L 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 its 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 apostle certain 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 interval.
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 set to one thousand milliseconds, so every 1000 milliseconds
on take will be called.
And Untick also gives us this value called Milly's until the 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, medicinal finish is going to be the whole time or duration, which
is, in our case, 60000 minus the milliseconds until finished, which is whatever time has passed since
the timer has started.
And then we have timer or texts.
And so we set that again to Milly's until finished.
So the milliseconds that are still left in the timer divided by 1000 and then we set out to a string
because that's what texts 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 to 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 timer 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 pulse timer, so we have this pulse timer function.
And here I'm checking if the countdown timer is not now, if it's not now done, cancel it.
So if there is no countdown timer, then there's no use in doing anything and then we have reset the
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 for your timer will be set back to 60 seconds.
And of course, we may not forget to set pause offset 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 see you in the next video.