1
00:00:01,120 --> 00:00:06,760
Welcome back in this video, we're going to implement the timer and you can see the timer now works,

2
00:00:06,760 --> 00:00:08,800
while at least that's the result of the video.

3
00:00:09,160 --> 00:00:15,220
So at the end of the video, you will have this date here where the time goes down and you see that

4
00:00:15,400 --> 00:00:23,440
the gray bar is going bigger and the green bar is going smaller, and that's basically how the progress

5
00:00:23,440 --> 00:00:24,640
bars are working.

6
00:00:24,850 --> 00:00:31,870
And now it probably also makes sense for you to see why we created all of these different XML files

7
00:00:31,870 --> 00:00:35,590
in the previous video or the video before the previous video, actually.

8
00:00:36,130 --> 00:00:38,470
So we have all of these progress bar.

9
00:00:38,470 --> 00:00:39,370
You see progress.

10
00:00:39,400 --> 00:00:43,840
Trouble is the normal progress bar and the background is the sector.

11
00:00:43,840 --> 00:00:45,220
The progress bar great.

12
00:00:45,520 --> 00:00:54,130
So now when the progress bar, the circular progress bar is getting less, you see the gray is just

13
00:00:54,130 --> 00:00:56,650
being exposed and it's seen more.

14
00:00:57,160 --> 00:00:58,750
So now let's go ahead and implement that.

15
00:00:59,050 --> 00:01:04,030
And you have seen in the last video how to create a timer.

16
00:01:04,180 --> 00:01:08,800
So I would recommend that you try to build this by yourself, so you try to implement all of this stuff

17
00:01:08,800 --> 00:01:12,520
by yourself, even though I understand it won't be easy, but good luck with it.

18
00:01:13,660 --> 00:01:20,830
OK, so first of all, we will need to have a variable that will take care of the rest timer.

19
00:01:21,490 --> 00:01:28,090
OK, so the rest timer will be the timer for how much time you want to rest.

20
00:01:28,270 --> 00:01:34,120
It's not how much rest is left, but it's more about resting and relaxing.

21
00:01:34,130 --> 00:01:41,350
OK, so you have a little bit of relaxing time and I'm going to create a countdown timer here.

22
00:01:42,280 --> 00:01:45,550
OK, so countdown timer is going to be.

23
00:01:48,140 --> 00:01:52,220
And this arrest timer will be of type countdown timer.

24
00:01:53,870 --> 00:02:00,800
And as we are setting up the countdown timer globally, we are going to set that to null first and for

25
00:02:00,890 --> 00:02:02,790
the rest progress.

26
00:02:02,810 --> 00:02:04,250
So how far we've come?

27
00:02:04,520 --> 00:02:10,250
I'm going to create another variable here, rest progress, which I'm going to set to zero.

28
00:02:10,280 --> 00:02:12,380
OK, so this will be an integer by default.

29
00:02:12,620 --> 00:02:16,400
And now we're going to require a couple of functions that are going to help us.

30
00:02:16,790 --> 00:02:22,310
So first of all, let's create a function which will set the rest progress bar.

31
00:02:22,520 --> 00:02:22,910
OK.

32
00:02:23,000 --> 00:02:25,370
So private fun set.

33
00:02:25,640 --> 00:02:27,800
Rest Progress Bar.

34
00:02:28,010 --> 00:02:28,390
All right.

35
00:02:28,400 --> 00:02:34,880
So this method here we're going to, first of all, set our progress bar and therefore we can use our

36
00:02:34,880 --> 00:02:36,230
data binding, which is amazing.

37
00:02:36,560 --> 00:02:44,420
So the progress bar and it's this one here I can set, it's progress to be the rest progress.

38
00:02:44,630 --> 00:02:50,150
So however much the rest progress will be, so how far we have come, that will be the progress of the

39
00:02:50,150 --> 00:02:50,870
progress bar.

40
00:02:51,860 --> 00:02:59,240
OK, and now we can create the rest timer because we have set up this timer variable here rate, which

41
00:02:59,240 --> 00:03:02,870
is a countdown timer, and now we're just going to create the new object.

42
00:03:02,990 --> 00:03:09,890
And how do you do that is used to object keyword and then countdown timer where you pass in, how much

43
00:03:09,890 --> 00:03:12,030
time you want this to last.

44
00:03:12,050 --> 00:03:12,670
You want this to last.

45
00:03:12,680 --> 00:03:17,900
So in my case, I'm going to set that to 10000, which is 10000 milliseconds in the future.

46
00:03:18,260 --> 00:03:22,690
And then I can define what's the countdown interval should be.

47
00:03:22,700 --> 00:03:25,310
So how often do I want something to happen?

48
00:03:25,670 --> 00:03:32,480
And in my case, I'm going to say every 1000 milliseconds, I want something to happen, and 1000 milliseconds

49
00:03:32,480 --> 00:03:34,240
is one seconds or every second.

50
00:03:34,250 --> 00:03:38,720
I want this code that will be in these curly brackets to execute something.

51
00:03:39,020 --> 00:03:43,910
And here you need to implement the members for this countdown timer object.

52
00:03:43,940 --> 00:03:49,280
So one thing that should happen every tick, which means every 1000 milliseconds and then one thing

53
00:03:49,280 --> 00:03:54,680
that should happen once the countdown timer is done, in my case, after 10000 milliseconds.

54
00:03:55,340 --> 00:03:57,800
So what do we want to do with every single tick?

55
00:03:58,340 --> 00:04:04,100
So I'm going to increase the rest progress by one second or by one value.

56
00:04:04,100 --> 00:04:05,810
It's it's just not an integer, right?

57
00:04:05,810 --> 00:04:08,600
So it's just a number and I'm going to change to progress.

58
00:04:08,600 --> 00:04:09,530
Bars progress.

59
00:04:09,860 --> 00:04:15,050
So here progress to be 10 minus the rest progress.

60
00:04:16,279 --> 00:04:16,550
OK?

61
00:04:16,579 --> 00:04:18,880
And you see, I'm using hardcoded numbers here.

62
00:04:18,890 --> 00:04:22,130
Of course, you could create global variables here and then assign those.

63
00:04:22,580 --> 00:04:27,680
I would recommend to do that if this was a production application.

64
00:04:28,310 --> 00:04:37,760
OK, and then I'm going to use the timer of the text view or the text view timer variable to set the

65
00:04:38,090 --> 00:04:44,060
text to be and here 10 minus the rest progress again.

66
00:04:44,480 --> 00:04:50,750
But as we cannot assign a number to our text, we need to convert that into a string.

67
00:04:51,050 --> 00:04:51,470
OK.

68
00:04:52,010 --> 00:04:59,090
So the result of this calculation converted into a string is what I want to assign to to text your timer.

69
00:04:59,870 --> 00:05:06,560
And then on finish once the timer is done, I just want to go ahead and write a toast on the screen.

70
00:05:07,010 --> 00:05:10,700
OK, so I'm just going to say, you know, we will start the exercise.

71
00:05:11,670 --> 00:05:15,870
And by the way, what is this text, if you were wondering what is TBE time or not the text itself,

72
00:05:16,200 --> 00:05:22,380
this TV timer is this text view timer, which we have here, which is just this number inside of our

73
00:05:22,380 --> 00:05:22,830
timer.

74
00:05:23,310 --> 00:05:24,330
So this one here?

75
00:05:24,750 --> 00:05:26,340
OK, so.

76
00:05:27,440 --> 00:05:35,720
That's what I am setting up here, so I'm setting the Progress Bar, which starts a timer and every

77
00:05:35,720 --> 00:05:43,190
tick, so every 1000 milliseconds, so whatever you set here will be the interval after which this method

78
00:05:43,190 --> 00:05:43,850
will be called.

79
00:05:44,180 --> 00:05:47,840
So after every 1000 milliseconds, call this onto code.

80
00:05:48,440 --> 00:05:50,750
And once you're done, call this unfinished code.

81
00:05:51,260 --> 00:05:57,380
And what I'm just doing is I'm increasing the rest progress by one, and I'm displaying the timer accordingly.

82
00:05:57,800 --> 00:06:02,510
And once the 10000 milliseconds are over, this toast will be displayed.

83
00:06:03,800 --> 00:06:06,060
And of course, that by itself will not be enough.

84
00:06:06,080 --> 00:06:08,390
We also need to start that timer.

85
00:06:08,540 --> 00:06:13,250
So here it's really important to add the start at the end of this countdown timer object.

86
00:06:13,250 --> 00:06:14,840
So here at this point.

87
00:06:15,740 --> 00:06:16,580
So let me.

88
00:06:18,150 --> 00:06:20,040
Make this a little clearer here.

89
00:06:20,400 --> 00:06:25,080
So here this is where we then set the start method.

90
00:06:26,640 --> 00:06:34,380
OK, so now we can set the progress bar in the concrete method at the very beginning, like so or not,

91
00:06:34,380 --> 00:06:38,370
the beginning of the on create method, but at the beginning of when the activity is loaded.

92
00:06:39,940 --> 00:06:47,200
And by the way, we are using view binding and we are using a timer, so we need to make sure that we

93
00:06:47,200 --> 00:06:49,030
use the on destroying method here.

94
00:06:49,720 --> 00:06:59,260
So in the on destroying method, I will check if the rest timer is not null, then I'm going to cancel

95
00:06:59,260 --> 00:06:59,440
it.

96
00:06:59,560 --> 00:07:02,410
So rest, timer, dot cancel.

97
00:07:03,190 --> 00:07:05,470
And this timer is, of course, the Nullarbor.

98
00:07:05,470 --> 00:07:07,420
So I need to add the question mark here.

99
00:07:07,930 --> 00:07:11,410
But then on top of that, I'm going to reset the rest progress.

100
00:07:11,830 --> 00:07:14,980
OK, I'm going to set it back to zero like so.

101
00:07:15,940 --> 00:07:20,890
And of course, I mustn't forget get to set my binding variable to null.

102
00:07:22,120 --> 00:07:24,190
OK, so that's in the on destroy method.

103
00:07:24,640 --> 00:07:28,720
And now let's test this, OK, so we set the rest progress bar.

104
00:07:29,050 --> 00:07:30,300
Let's run our code.

105
00:07:30,310 --> 00:07:35,680
And by the way, it will automatically start the timer once we jump over, you see 10 nine, it directly

106
00:07:35,680 --> 00:07:38,860
goes down, even though we don't see the gray backgrounds.

107
00:07:39,190 --> 00:07:41,710
That's something we might have to look into.

108
00:07:42,220 --> 00:07:45,730
But overall, you see here now we will start the exercise.

109
00:07:46,090 --> 00:07:47,620
So that is one way of doing it.

110
00:07:47,620 --> 00:07:53,200
But I also want to have an extra method that will set up the.

111
00:07:54,180 --> 00:07:54,810
Rescue.

112
00:07:55,140 --> 00:07:55,540
OK.

113
00:07:55,650 --> 00:08:03,270
So private fund set up rest view, so if we want to make any changes later on, we can just called this

114
00:08:03,270 --> 00:08:08,310
method instead in a while and then we can make the changes to our rescue.

115
00:08:08,820 --> 00:08:11,940
So what I want to do is I want to basically do the same thing here.

116
00:08:12,150 --> 00:08:16,290
So if I were to go back from the screen.

117
00:08:16,350 --> 00:08:20,310
So let's say I start the timer and then I go back.

118
00:08:20,640 --> 00:08:24,240
I want to make sure that the timer is actually reset, OK?

119
00:08:24,270 --> 00:08:30,420
And I do that in here where I said, Well, check whether the timer is not now.

120
00:08:30,690 --> 00:08:33,600
And then I cancel it and I set the rest progress to zero.

121
00:08:34,289 --> 00:08:37,890
And then I call this set of progress bar method.

122
00:08:38,700 --> 00:08:38,950
OK.

123
00:08:39,230 --> 00:08:44,130
Just to make sure that we are not running into a timer that is already running it and then we could

124
00:08:44,400 --> 00:08:45,780
break the entire application.

125
00:08:46,200 --> 00:08:51,930
So here, instead of setting the rest progress bar, we're going to set up the rest of you, which in

126
00:08:51,930 --> 00:08:54,990
turn me also calls to set the progress bar.

127
00:08:55,920 --> 00:08:57,450
OK, so let's test it again.

128
00:08:57,570 --> 00:09:01,020
It should still work if it's still working all the same.

129
00:09:01,350 --> 00:09:01,950
Let's see.

130
00:09:02,700 --> 00:09:06,420
So we see we don't get this gray background.

131
00:09:06,510 --> 00:09:09,030
OK, so we we have the timer that works.

132
00:09:09,030 --> 00:09:09,750
That's amazing.

133
00:09:10,050 --> 00:09:12,470
And the progress bar is changing as well.

134
00:09:12,480 --> 00:09:14,430
But the gray background is not there.

135
00:09:14,430 --> 00:09:18,600
And we also see I don't know what the exercise will start, so everything seems to work.

136
00:09:18,840 --> 00:09:26,430
But then this this little setting and use level, I need to set that to false for my circular progress

137
00:09:26,520 --> 00:09:33,390
gray so that it will be visible because otherwise, this gray background that I have here see wouldn't

138
00:09:33,390 --> 00:09:34,230
be appearing.

139
00:09:34,710 --> 00:09:40,830
So inside of the XML file, make sure that use level is set to false and then we will have this gray

140
00:09:40,830 --> 00:09:41,430
background.

141
00:09:41,790 --> 00:09:44,340
So if you want to have that, that's how you can achieve it.

142
00:09:44,910 --> 00:09:53,010
So this is quite a lot of extra code that we need to touch just to get this specific appearance.

143
00:09:53,280 --> 00:10:00,150
But I really wanted to show you with a more difficult appearance that somebody might want you to build.

144
00:10:00,270 --> 00:10:05,700
So some particular design, I wanted to show you how you can do a lot of custom stuff.

145
00:10:06,000 --> 00:10:08,370
OK, so see you in the next video.

