1
00:00:04,300 --> 00:00:08,300
Welcome back. In our duration's report activity,

2
00:00:08,300 --> 00:00:11,500
we've still got the delete menu option to implement.

3
00:00:12,200 --> 00:00:15,200
The first step is to show the DatePickerDialog.

4
00:00:43,860 --> 00:00:48,060
I'll use the light bulb to convert the dialog title to a string resource

5
00:00:48,060 --> 00:00:52,060
called date_title_delete.

6
00:01:03,160 --> 00:01:05,960
Deleting the rows is fairly straightforward.

7
00:01:06,260 --> 00:01:08,860
We just tell the content provider to delete

8
00:01:08,860 --> 00:01:11,360
all records earlier than a certain date.

9
00:01:12,350 --> 00:01:16,450
A slight complication is we want to get confirmation first.

10
00:01:17,250 --> 00:01:21,350
The user could potentially be deleting thousands of records.

11
00:01:21,750 --> 00:01:23,750
It's only fair to warn them first.

12
00:01:24,740 --> 00:01:28,540
What we'll do is respond to the date they choose in our

13
00:01:28,540 --> 00:01:30,440
OnDateSet function,

14
00:01:30,990 --> 00:01:35,490
that will use our AppDialog class to display the confirmation dialog,

15
00:01:35,990 --> 00:01:38,490
and we'll perform the deletion in the

16
00:01:38,490 --> 00:01:40,990
OnPositiveDialogResult function.

17
00:01:41,390 --> 00:01:43,990
Let's start with the OnDateSet function

18
00:01:43,990 --> 00:01:46,290
in our durations report activity.

19
00:01:49,290 --> 00:01:51,650
When OnDateSet's called,

20
00:01:51,650 --> 00:01:53,450
we're provided with the year,

21
00:01:53,450 --> 00:01:57,250
month and day that a user selected from the DatePicker.

22
00:01:58,140 --> 00:02:01,740
Rather than just go ahead and delete all the rows before that date,

23
00:02:02,140 --> 00:02:03,640
we'll pop up a dialog

24
00:02:03,640 --> 00:02:07,140
to let the user confirm that they really want to delete the rows.

25
00:02:08,039 --> 00:02:11,540
Displaying the dialog is easy, we've done it quite a few times now.

26
00:02:12,090 --> 00:02:15,290
A slight complication is that we should show the date

27
00:02:15,290 --> 00:02:17,090
in the user's preferred format.

28
00:02:17,890 --> 00:02:19,490
That should be easy, shouldn't it.

29
00:02:19,740 --> 00:02:23,740
We've already done something similar in the DurationsViewModel class.

30
00:02:24,620 --> 00:02:27,720
Unfortunately, the date format API

31
00:02:27,720 --> 00:02:31,620
doesn't provide a suitable function when we haven't got a date.

32
00:02:32,120 --> 00:02:35,620
We've got three values: the year, month and day.

33
00:02:36,280 --> 00:02:39,580
Before we can format that in a way that the user is familiar with,

34
00:02:39,940 --> 00:02:42,600
we need to convert those three values into a date.

35
00:02:53,100 --> 00:02:56,900
If you're prompted for the imports, the Gregorian calendar

36
00:02:56,900 --> 00:02:59,200
comes from java.util

37
00:02:59,560 --> 00:03:04,160
There is a Gregorian calendar class in android.icu.util,

38
00:03:04,710 --> 00:03:07,700
but that requires a minimum SDK of 24.

39
00:03:08,580 --> 00:03:11,680
Our app runs on all android versions back to 17,

40
00:03:11,680 --> 00:03:13,380
so we can't use that class.

41
00:03:20,280 --> 00:03:25,770
The date format class we want is in android.text.format.

42
00:03:29,370 --> 00:03:32,140
We create a new Gregorian calendar object

43
00:03:32,140 --> 00:03:35,240
and use that to convert our three values into a date.

44
00:03:35,790 --> 00:03:38,090
In this case, the time in milliseconds.

45
00:03:38,970 --> 00:03:42,750
That gives us something we can pass to date format's format function.

46
00:03:43,740 --> 00:03:46,540
Now we've got a string that makes sense to the user.

47
00:03:47,530 --> 00:03:50,330
All right. The remaining code

48
00:03:50,330 --> 00:03:52,590
sets up the arguments for our dialog.

49
00:04:05,890 --> 00:04:08,990
The light bulb doesn't give us the option to extract the message

50
00:04:08,990 --> 00:04:10,240
into a string resource.

51
00:04:10,790 --> 00:04:13,390
We have to edit strings.xml,

52
00:04:13,390 --> 00:04:15,090
and add the resource manually.

53
00:04:15,890 --> 00:04:20,290
Cut all the text inside the speech marks without the speech marks,

54
00:04:21,649 --> 00:04:24,150
then open strings.xml.

55
00:04:34,750 --> 00:04:36,740
I'll call the new resource

56
00:04:36,740 --> 00:04:40,340
delete_timings_message.

57
00:05:02,340 --> 00:05:04,540
We also need to change the placeholder.

58
00:05:05,200 --> 00:05:07,900
At the moment, it's using a Kotlin variable,

59
00:05:08,150 --> 00:05:10,280
and we need a resource placeholder.

60
00:05:19,530 --> 00:05:22,530
Back in our code, we'll use the resource ID.

61
00:05:47,630 --> 00:05:49,630
Next, we put the date value,

62
00:05:49,930 --> 00:05:53,920
the full date time in milliseconds into the args bundle.

63
00:05:54,470 --> 00:05:58,870
We're going to need that later when we react to the positive dialog button

64
00:05:59,120 --> 00:06:00,780
and perform the deletion.

65
00:06:12,580 --> 00:06:16,080
I've used a constant to store the value in the args bundle.

66
00:06:16,630 --> 00:06:19,930
We'll use the same constant to get the value back out again

67
00:06:20,290 --> 00:06:22,890
so I'd better declare it up at the start of the file.

68
00:06:42,890 --> 00:06:46,390
All right. If the user confirms the deletion,

69
00:06:46,390 --> 00:06:50,690
app dialog will call our on PositiveDialogResult function.

70
00:06:51,490 --> 00:06:54,990
I'll make the class implement the dialog events interface.

71
00:06:55,980 --> 00:06:58,980
We'll get an error until we implement the interface functions.

72
00:07:14,480 --> 00:07:18,580
Android studio can generate the stub for the implementation function

73
00:07:18,580 --> 00:07:21,080
just below the OnDateSet function,

74
00:07:21,330 --> 00:07:22,530
then I'll add the code.

75
00:07:58,730 --> 00:08:00,390
If we get confirmation

76
00:08:00,690 --> 00:08:03,920
and the dialog calls OnPositiveDialogResult,

77
00:08:04,420 --> 00:08:07,080
we can tell the view model to delete the records.

78
00:08:08,070 --> 00:08:11,170
We'll get an error until we write the delete function

79
00:08:11,170 --> 00:08:13,670
in the DurationsViewModel class, of course,

80
00:08:14,070 --> 00:08:15,370
I'll put it right at the end.

81
00:09:14,870 --> 00:09:16,270
That's the code finished.

82
00:09:16,730 --> 00:09:20,170
In the next video, we'll run the app and test it.

83
00:09:20,170 --> 00:09:21,070
I'll see you there.

