1
00:00:04,560 --> 00:00:06,820
G'day everyone, welcome back.

2
00:00:06,820 --> 00:00:15,320
Now that we've created our AppDialog class, let's see how to confirm deletions in MainActivityFragment.

3
00:00:15,320 --> 00:00:23,400
When the delete button's tapped, the onClickListener attached to the button in the cursorRecyclerViewAdapter class

4
00:00:23,400 --> 00:00:27,960
calls MainActivityFragment's onDeleteClick function.

5
00:00:27,960 --> 00:00:35,040
At the moment, that function uses the ViewModel's delete function to delete the task.

6
00:00:35,040 --> 00:00:41,340
If we want to request confirmation before calling delete we can create an AppDialog instance,

7
00:00:41,340 --> 00:00:45,520
and set the message string and button captions.

8
00:00:45,520 --> 00:00:51,120
We'll start by creating a couple of constants,
in MainActivityFragment.

9
00:00:51,120 --> 00:00:57,660
It's possible that we may want to create more than one dialogue, and we'll need some way to identify them.

10
00:00:57,660 --> 00:01:02,100
I mentioned this when we were creating the AppDialog class.

11
00:01:02,100 --> 00:01:09,460
We'll use a constant for the delete dialogue, to make sure we're responding to the call back from the right dialogue.

12
00:01:09,460 --> 00:01:20,020
We're also going to pass the task ID,
to let us know which task should be deleted.

13
00:01:20,020 --> 00:01:29,260
In the onDeleteClick function, we create a new AppDialog instance and show it.

14
00:01:53,260 --> 00:01:56,280
Alright, what are we doing there?

15
00:01:56,280 --> 00:02:02,640
Most of the code is creating the args bundle
to pass data to the AppDialog instance.

16
00:02:02,640 --> 00:02:10,440
We need to provide it with an ID because we wrote the class to need one, but you might still be unsure what it's for.

17
00:02:10,440 --> 00:02:16,680
Don't worry, that will become clear when we add
the code to respond to the dialog's callback.

18
00:02:16,680 --> 00:02:23,240
We pass the message that the dialog will display,
and also the text for the positive button.

19
00:02:23,240 --> 00:02:28,200
The negative button defaults to cancel,
which is why we're not setting it's text.

20
00:02:28,200 --> 00:02:35,460
Before I continue, I'll fix the errors,
starting with the delete on line 73.

21
00:02:35,460 --> 00:02:40,300
Our AppDialog expects a string resource, not a string,

22
00:02:40,300 --> 00:02:45,560
and we've got an error because I've provided a string to putInt.

23
00:02:45,560 --> 00:02:49,660
Click on the delete text,

24
00:02:49,660 --> 00:02:54,780
and use the light bulb to extract a string resource.

25
00:02:54,780 --> 00:03:01,760
I'm gonna start the names for all this
dialog's resources with the tag deldiag,

26
00:03:01,760 --> 00:03:14,700
so we'll call this one deldiag_positive_caption.

27
00:03:14,700 --> 00:03:21,600
Android studio tries to be helpful here,
and uses the new resource ID in a call to get string.

28
00:03:21,600 --> 00:03:39,360
We wrote our AppDialog class to expect resource IDs,
not strings, so I'll edit the line to just pass the ID.

29
00:03:39,360 --> 00:03:42,300
The line above is also showing an error.

30
00:03:42,300 --> 00:03:50,260
The dollar symbol is used by Kotlin to indicate a variable value,
and we don't have variables called d and s.

31
00:03:50,260 --> 00:03:55,140
In a string resource file, that's
the syntax for replaceable values,

32
00:03:55,140 --> 00:03:59,220
and I've typed that line, intending to convert it to a string resource.

33
00:03:59,220 --> 00:04:04,200
We talked about these back in section 9, the FlickrBrowser app.

34
00:04:04,200 --> 00:04:12,880
So if you're not sure about them, review the video titled
String Resources, currently lecture 164,

35
00:04:12,880 --> 00:04:17,019
but the numbering changes if I insert
additional videos and so forth,

36
00:04:17,019 --> 00:04:21,640
so it's string resources in section 9 that you want.

37
00:04:21,640 --> 00:04:27,000
Unfortunately, Android Studio won't convert
this string to a string resource.

38
00:04:27,000 --> 00:04:30,740
At the moment, we're going to have to edit the XML.

39
00:04:30,740 --> 00:04:36,320
It will convert it, if we remove the placeholders first.

40
00:04:39,320 --> 00:04:46,080
Now I can extract the string to a string resource,

41
00:04:46,080 --> 00:04:54,260
which I'll call deldiag_message.

42
00:04:54,260 --> 00:05:01,300
When I click OK the resource is added, and Android Studio
puts in the getString function call for us.

43
00:05:01,300 --> 00:05:12,900
All we have to do is add the additional arguments, the
task ID and the task name, and modify the string resource.

44
00:05:12,900 --> 00:05:23,380
In res/values/strings.xml,

45
00:05:23,380 --> 00:05:31,540
we need to add the placeholders to the deldiag_message string.

46
00:05:41,540 --> 00:05:43,460
That fixes the errors.

47
00:05:43,460 --> 00:05:45,480
I'll stop this video here.

48
00:05:45,480 --> 00:05:50,720
In the next video, I'll review what we've done,
then we'll see if it works.

49
00:05:50,720 --> 00:05:53,480
See you in the next one.

