Welcome back in this video, we're going to implement the timer and you can see the timer now works,
while at least that's the result of the video.
So at the end of the video, you will have this date here where the time goes down and you see that
the gray bar is going bigger and the green bar is going smaller, and that's basically how the progress
bars are working.
And now it probably also makes sense for you to see why we created all of these different XML files
in the previous video or the video before the previous video, actually.
So we have all of these progress bar.
You see progress.
Trouble is the normal progress bar and the background is the sector.
The progress bar great.
So now when the progress bar, the circular progress bar is getting less, you see the gray is just
being exposed and it's seen more.
So now let's go ahead and implement that.
And you have seen in the last video how to create a timer.
So I would recommend that you try to build this by yourself, so you try to implement all of this stuff
by yourself, even though I understand it won't be easy, but good luck with it.
OK, so first of all, we will need to have a variable that will take care of the rest timer.
OK, so the rest timer will be the timer for how much time you want to rest.
It's not how much rest is left, but it's more about resting and relaxing.
OK, so you have a little bit of relaxing time and I'm going to create a countdown timer here.
OK, so countdown timer is going to be.
And this arrest timer will be of type countdown timer.
And as we are setting up the countdown timer globally, we are going to set that to null first and for
the rest progress.
So how far we've come?
I'm going to create another variable here, rest progress, which I'm going to set to zero.
OK, so this will be an integer by default.
And now we're going to require a couple of functions that are going to help us.
So first of all, let's create a function which will set the rest progress bar.
OK.
So private fun set.
Rest Progress Bar.
All right.
So this method here we're going to, first of all, set our progress bar and therefore we can use our
data binding, which is amazing.
So the progress bar and it's this one here I can set, it's progress to be the rest progress.
So however much the rest progress will be, so how far we have come, that will be the progress of the
progress bar.
OK, and now we can create the rest timer because we have set up this timer variable here rate, which
is a countdown timer, and now we're just going to create the new object.
And how do you do that is used to object keyword and then countdown timer where you pass in, how much
time you want this to last.
You want this to last.
So in my case, I'm going to set that to 10000, which is 10000 milliseconds in the future.
And then I can define what's the countdown interval should be.
So how often do I want something to happen?
And in my case, I'm going to say every 1000 milliseconds, I want something to happen, and 1000 milliseconds
is one seconds or every second.
I want this code that will be in these curly brackets to execute something.
And here you need to implement the members for this countdown timer object.
So one thing that should happen every tick, which means every 1000 milliseconds and then one thing
that should happen once the countdown timer is done, in my case, after 10000 milliseconds.
So what do we want to do with every single tick?
So I'm going to increase the rest progress by one second or by one value.
It's it's just not an integer, right?
So it's just a number and I'm going to change to progress.
Bars progress.
So here progress to be 10 minus the rest progress.
OK?
And you see, I'm using hardcoded numbers here.
Of course, you could create global variables here and then assign those.
I would recommend to do that if this was a production application.
OK, and then I'm going to use the timer of the text view or the text view timer variable to set the
text to be and here 10 minus the rest progress again.
But as we cannot assign a number to our text, we need to convert that into a string.
OK.
So the result of this calculation converted into a string is what I want to assign to to text your timer.
And then on finish once the timer is done, I just want to go ahead and write a toast on the screen.
OK, so I'm just going to say, you know, we will start the exercise.
And by the way, what is this text, if you were wondering what is TBE time or not the text itself,
this TV timer is this text view timer, which we have here, which is just this number inside of our
timer.
So this one here?
OK, so.
That's what I am setting up here, so I'm setting the Progress Bar, which starts a timer and every
tick, so every 1000 milliseconds, so whatever you set here will be the interval after which this method
will be called.
So after every 1000 milliseconds, call this onto code.
And once you're done, call this unfinished code.
And what I'm just doing is I'm increasing the rest progress by one, and I'm displaying the timer accordingly.
And once the 10000 milliseconds are over, this toast will be displayed.
And of course, that by itself will not be enough.
We also need to start that timer.
So here it's really important to add the start at the end of this countdown timer object.
So here at this point.
So let me.
Make this a little clearer here.
So here this is where we then set the start method.
OK, so now we can set the progress bar in the concrete method at the very beginning, like so or not,
the beginning of the on create method, but at the beginning of when the activity is loaded.
And by the way, we are using view binding and we are using a timer, so we need to make sure that we
use the on destroying method here.
So in the on destroying method, I will check if the rest timer is not null, then I'm going to cancel
it.
So rest, timer, dot cancel.
And this timer is, of course, the Nullarbor.
So I need to add the question mark here.
But then on top of that, I'm going to reset the rest progress.
OK, I'm going to set it back to zero like so.
And of course, I mustn't forget get to set my binding variable to null.
OK, so that's in the on destroy method.
And now let's test this, OK, so we set the rest progress bar.
Let's run our code.
And by the way, it will automatically start the timer once we jump over, you see 10 nine, it directly
goes down, even though we don't see the gray backgrounds.
That's something we might have to look into.
But overall, you see here now we will start the exercise.
So that is one way of doing it.
But I also want to have an extra method that will set up the.
Rescue.
OK.
So private fund set up rest view, so if we want to make any changes later on, we can just called this
method instead in a while and then we can make the changes to our rescue.
So what I want to do is I want to basically do the same thing here.
So if I were to go back from the screen.
So let's say I start the timer and then I go back.
I want to make sure that the timer is actually reset, OK?
And I do that in here where I said, Well, check whether the timer is not now.
And then I cancel it and I set the rest progress to zero.
And then I call this set of progress bar method.
OK.
Just to make sure that we are not running into a timer that is already running it and then we could
break the entire application.
So here, instead of setting the rest progress bar, we're going to set up the rest of you, which in
turn me also calls to set the progress bar.
OK, so let's test it again.
It should still work if it's still working all the same.
Let's see.
So we see we don't get this gray background.
OK, so we we have the timer that works.
That's amazing.
And the progress bar is changing as well.
But the gray background is not there.
And we also see I don't know what the exercise will start, so everything seems to work.
But then this this little setting and use level, I need to set that to false for my circular progress
gray so that it will be visible because otherwise, this gray background that I have here see wouldn't
be appearing.
So inside of the XML file, make sure that use level is set to false and then we will have this gray
background.
So if you want to have that, that's how you can achieve it.
So this is quite a lot of extra code that we need to touch just to get this specific appearance.
But I really wanted to show you with a more difficult appearance that somebody might want you to build.
So some particular design, I wanted to show you how you can do a lot of custom stuff.
OK, so see you in the next video.