1
00:00:00,960 --> 00:00:06,000
Welcome back, so we can select the date now, and that works.

2
00:00:06,270 --> 00:00:11,490
But now let's actually calculate how much time has passed ever since.

3
00:00:12,090 --> 00:00:13,860
So let's go back to coding.

4
00:00:15,090 --> 00:00:22,020
So we have the date now, and this is just using this simple date format that we have prepared.

5
00:00:22,290 --> 00:00:26,520
And it's parsing that into the data variable.

6
00:00:27,000 --> 00:00:30,990
But I wants to know the selected state in minutes.

7
00:00:31,200 --> 00:00:35,230
So today that we're getting there is not giving us.

8
00:00:35,250 --> 00:00:41,440
So the selected date in minutes, so how much time has passed ever since and minutes?

9
00:00:42,090 --> 00:00:50,250
So what I want to use for that is going to be a variable that will be called selected date in minutes.

10
00:00:50,970 --> 00:00:58,410
And in order to get that, I can use to date variable here and get its time property and then divide

11
00:00:58,410 --> 00:01:00,270
that by 60000.

12
00:01:00,780 --> 00:01:03,360
So why would I divide it by 60000?

13
00:01:05,440 --> 00:01:16,330
Because date so the date property for look at it here gives us the details and to time that has passed

14
00:01:16,690 --> 00:01:22,210
ever since a given moment, so here does get time.

15
00:01:22,210 --> 00:01:29,320
Method, for example, gives us the number of milliseconds to January the 1st 1970 at midnight.

16
00:01:30,140 --> 00:01:36,160
Okay, so when you use get time, you get a super long value.

17
00:01:36,220 --> 00:01:38,380
This is a long data type.

18
00:01:38,770 --> 00:01:39,880
It's a very long value.

19
00:01:39,880 --> 00:01:46,450
And if you want to get minutes instead of milliseconds, you, first of all, have to divide by thousand

20
00:01:46,570 --> 00:01:52,180
to get two seconds and then another time by 60 in order to get from seconds to minutes.

21
00:01:52,510 --> 00:01:58,930
So if you do both in one step, you divide by 60000 and then you get the time that has passed since

22
00:01:58,930 --> 00:02:01,660
1970 in minutes.

23
00:02:02,920 --> 00:02:08,800
And this time property is the same thing as if we were asking for get time.

24
00:02:09,039 --> 00:02:13,060
So it's the same as if I would use get time here, for example.

25
00:02:13,420 --> 00:02:20,620
Like So but this is this great time is the old fashioned way we can just get this time by using the

26
00:02:21,010 --> 00:02:21,640
property.

27
00:02:21,640 --> 00:02:24,400
Access syntax does time, as you can see here.

28
00:02:26,750 --> 00:02:34,670
So now we have the selected date, and we need to calculate how much time has passed between the selected

29
00:02:34,670 --> 00:02:35,840
date and the current date.

30
00:02:36,140 --> 00:02:37,880
So let's get to current date.

31
00:02:38,270 --> 00:02:42,410
Current date is going to use to SDF pass.

32
00:02:43,850 --> 00:02:50,420
And here we need to use the format, so we could, of course, create the format object here and so

33
00:02:50,420 --> 00:02:59,030
forth, but I'm just going to do it in one step as the format where I use system that current millis.

34
00:02:59,540 --> 00:03:08,900
So what this will do is it will give me the time that has passed since the 1st of January 1970 in milliseconds.

35
00:03:09,470 --> 00:03:11,870
That's how I can basically use this approach here.

36
00:03:12,070 --> 00:03:19,310
Here we get how much time has passed since the ball between the selected date and 1970 1st of January.

37
00:03:19,550 --> 00:03:22,460
And here we get the current time.

38
00:03:22,470 --> 00:03:26,300
So how much time has passed between now and 1970?

39
00:03:26,450 --> 00:03:28,760
Okay, so that's the current date that we get.

40
00:03:29,150 --> 00:03:33,470
And now we need to calculate the difference in minutes.

41
00:03:33,950 --> 00:03:36,530
OK, so let's get the current date.

42
00:03:38,010 --> 00:03:40,410
And we need to calculate that in minutes, actually.

43
00:03:40,740 --> 00:03:47,310
So let's create a vault current date in minutes.

44
00:03:49,000 --> 00:03:55,330
Which will be the current date time divided by 60000.

45
00:03:56,570 --> 00:04:01,130
And now we can use the current date in minutes minus the selected date in minutes.

46
00:04:02,330 --> 00:04:06,950
And this will give us how much time is between those two time points.

47
00:04:07,400 --> 00:04:11,510
So between the selected and the current date?

48
00:04:13,630 --> 00:04:18,579
OK, so now we can use this difference in minutes and actually assign it to our text view that wants

49
00:04:18,579 --> 00:04:23,290
to know about it, and it's this text view here which currently doesn't have an ID, so let's give it

50
00:04:23,290 --> 00:04:23,590
an ID.

51
00:04:24,280 --> 00:04:26,560
You probably know how to do it.

52
00:04:26,620 --> 00:04:34,210
You just use this Android ID at plus ID slash, or you could have used a designer to find ID here or

53
00:04:34,210 --> 00:04:35,170
here directly.

54
00:04:35,470 --> 00:04:37,780
OK, well, I'm going to show you how to do it in the code.

55
00:04:38,140 --> 00:04:41,380
It's called a TV age in minutes.

56
00:04:43,160 --> 00:04:51,650
OK, so now let's access this text view in our code, and therefore we need to create another variable

57
00:04:51,650 --> 00:04:52,070
up here.

58
00:04:52,160 --> 00:05:00,750
So private VAR and I'm going to call the TV age in minutes, which will also be a text you optional.

59
00:05:00,770 --> 00:05:06,230
I'm going to set it to null and then I can initialize it here in my own create.

60
00:05:07,620 --> 00:05:14,310
Method, and I will find it by its side, which is our Dart ID, dot tbe h in minutes.

61
00:05:15,390 --> 00:05:24,330
OK, so here we are signing the text use and this age in minutes is this text view here, by the way,

62
00:05:24,330 --> 00:05:28,740
you can get there by holding the control key, clicking into it and you see which text you.

63
00:05:28,740 --> 00:05:30,210
It is in the split view.

64
00:05:30,480 --> 00:05:33,570
You can click into here and you will see that it's this one here.

65
00:05:33,750 --> 00:05:35,220
What currently says 14 million?

66
00:05:35,220 --> 00:05:37,140
So that's the one that I want to overwrite.

67
00:05:38,010 --> 00:05:38,390
OK.

68
00:05:38,700 --> 00:05:45,270
So in the main activity, now we have access to this TV age in minutes and we can actually assign it.

69
00:05:45,280 --> 00:05:47,690
So here TV age in minutes.

70
00:05:48,690 --> 00:05:52,320
That text should now be the difference in minutes.

71
00:05:52,950 --> 00:05:58,890
Now, unfortunately, the difference in minutes is of type along, so we need to convert it into a string

72
00:05:58,890 --> 00:05:59,790
for this to work.

73
00:06:00,790 --> 00:06:08,140
And TV age in minutes here, because it's another bill, we need to add the question mark here for type

74
00:06:08,140 --> 00:06:08,620
safety.

75
00:06:11,680 --> 00:06:14,740
OK, so at this point, let's actually test us.

76
00:06:15,340 --> 00:06:16,900
So let's test once again.

77
00:06:20,160 --> 00:06:24,240
And that selected date, let's select between.

78
00:06:25,240 --> 00:06:32,680
Yesterday and today, it's not yesterday, but it's a little longer, so you see here, 14, 40 minutes

79
00:06:32,680 --> 00:06:33,910
have passed.

80
00:06:34,240 --> 00:06:37,780
Ever since that day that I've selected and no.

81
00:06:37,930 --> 00:06:43,600
So let me select the first of September ever since 20 minutes have passed, but me select the date in

82
00:06:43,600 --> 00:06:45,400
the future because today's the 21st.

83
00:06:45,520 --> 00:06:47,260
So let me select the 22nd.

84
00:06:47,260 --> 00:06:53,530
And you see, it's some sometime in the past, even though for some reason my phone thinks it's a different

85
00:06:53,530 --> 00:06:59,860
date, I believe it thinks that it's September the 15th, so definitely something wrong with my phone

86
00:06:59,860 --> 00:07:06,070
here and still stuck at the 15th and thinks the current date is 15th, even though it's the 21st.

87
00:07:07,940 --> 00:07:11,390
But if I were to test this on a real phone, this would work.

88
00:07:11,870 --> 00:07:19,730
So how can I limit the selection to dates in the past because they cannot be born in the future, right?

89
00:07:20,450 --> 00:07:25,040
Well, that is something that we can do with a neat little trick.

90
00:07:25,310 --> 00:07:32,150
So instead of showing this date picker dialogue here directly, what we can do is we can put this entire

91
00:07:32,150 --> 00:07:34,220
date picker dialogue, which is all of this code.

92
00:07:34,590 --> 00:07:42,470
This is all of the code that this part of our date picker dialogue, so we can put that into a variable

93
00:07:42,650 --> 00:07:46,790
so I can call the DPD, for example, and paste all of that in there.

94
00:07:47,210 --> 00:07:52,780
So this DPD value more variable will now be our data picker dialog.

95
00:07:52,790 --> 00:07:54,710
So let me shrink it down.

96
00:07:55,310 --> 00:08:00,470
And now, while in here with this minus, I can collapse the code.

97
00:08:00,470 --> 00:08:06,950
You see, all of this code now is going to be reduced, at least from a visible point of view.

98
00:08:07,190 --> 00:08:11,370
And now we can show the date picker dialog.

99
00:08:11,390 --> 00:08:18,260
Let's just test if the date picker dialog will still appear as we wanted it so we can see it's the works.

100
00:08:18,260 --> 00:08:19,580
Let me select the date here.

101
00:08:20,210 --> 00:08:21,470
OK, that still works.

102
00:08:21,860 --> 00:08:26,570
And now, instead of just showing it, I want to add another property to it.

103
00:08:26,930 --> 00:08:30,260
And here I can set the date picker property.

104
00:08:31,790 --> 00:08:40,580
To be set to a specific max date, so here does this mix date, and I can assign he makes date to it.

105
00:08:47,230 --> 00:08:55,000
And here, in order to set the max date to be yesterday, I can just go ahead and set it to a system

106
00:08:55,660 --> 00:09:06,990
that current time in milliseconds minus eighty six four zero zero, so 86 million, 400000 milliseconds.

107
00:09:07,660 --> 00:09:11,860
So there are 3.6 million milliseconds in one hour.

108
00:09:11,860 --> 00:09:17,620
So multiply that by 24 and you get eighty six point four million milliseconds.

109
00:09:17,860 --> 00:09:22,360
So let's test that once again, and I'll select the date in the future.

110
00:09:22,360 --> 00:09:27,070
While you can see I cannot select dates in the future anymore, I can only select dates in the past.

111
00:09:27,070 --> 00:09:35,080
So, for example, one day in the past and you can see now it's 14 40, that's how many minutes I'm

112
00:09:35,080 --> 00:09:35,440
old.

113
00:09:35,440 --> 00:09:36,700
If I'm one day old.

114
00:09:36,700 --> 00:09:37,360
Exactly.

115
00:09:38,290 --> 00:09:43,900
OK, so now we can make a couple of changes here to our code.

116
00:09:46,430 --> 00:09:49,250
Because we are using to date here.

117
00:09:52,760 --> 00:09:59,210
And now, if you look at our code, you will see that there are some parts of the code marked, OK,

118
00:09:59,210 --> 00:10:05,870
so this dead date, for example, is highlighted or has a background color here, and that is because

119
00:10:05,870 --> 00:10:07,400
we're not using type safety.

120
00:10:07,700 --> 00:10:15,980
So what you can do here instead, is you can make it null safe by using the following approach by basically

121
00:10:16,640 --> 00:10:19,730
creating this selected date in minutes.

122
00:10:20,000 --> 00:10:24,890
If the date is not empty, so here's the date question mark.

123
00:10:25,010 --> 00:10:25,670
Don't let.

124
00:10:27,380 --> 00:10:32,030
And now do all of this part here inside of it.

125
00:10:34,380 --> 00:10:37,530
OK, so only if the date is not empty, then run this code.

126
00:10:37,860 --> 00:10:39,300
The same goes for the current date.

127
00:10:39,390 --> 00:10:41,970
So here we can use current date.

128
00:10:42,720 --> 00:10:43,340
Question mark.

129
00:10:43,350 --> 00:10:44,220
Don't let.

130
00:10:45,240 --> 00:10:49,770
And then execute the rest only if current date is not empty.

131
00:10:50,250 --> 00:10:58,230
So this date let is just an approach to make sure that you we only execute this code if the date is

132
00:10:58,230 --> 00:10:59,670
in fact not empty.

133
00:11:00,000 --> 00:11:04,800
If the date is empty for some reason, then we will not execute the code.

134
00:11:05,160 --> 00:11:08,520
So this way we're making sure that our code is not crashing.

135
00:11:09,530 --> 00:11:13,330
And this is just null safety that we are implementing here.

136
00:11:15,170 --> 00:11:22,190
And then another thing that we can optimize is that we can make sure that this click data picker is

137
00:11:22,520 --> 00:11:28,450
private as well, so we can make it private so that it will only be accessible from this class and from

138
00:11:28,460 --> 00:11:32,870
the other class, which may totally make sense for our particular case.

139
00:11:33,050 --> 00:11:39,440
So if we ever want to use this method from somewhere else, we would just have to make it public again

140
00:11:39,650 --> 00:11:41,930
and then we can access it from the other classes.

141
00:11:41,930 --> 00:11:46,670
But in our case, as this is never going to be required for now, we can replace that.

142
00:11:46,910 --> 00:11:52,610
And another thing you see here, this view is never used, so we can basically just replace it with

143
00:11:52,610 --> 00:11:56,990
an underscore and the code will work exactly as it did before.

144
00:11:58,280 --> 00:12:05,060
OK, so that's the highly optimized code that we have here and now we can feel confident when we published

145
00:12:05,060 --> 00:12:05,840
this application.

146
00:12:06,200 --> 00:12:11,810
So in the next lecture, I have a little challenge for you where I would like you to rebuild this application,

147
00:12:11,810 --> 00:12:16,760
not displaying the age in minutes, but actually the age and hours.

148
00:12:16,970 --> 00:12:19,150
So good luck with that and see you in the next.

