1
00:00:05,000 --> 00:00:06,820
G'day everyone, welcome back.

2
00:00:06,820 --> 00:00:11,280
In the last video, we saw how to use our AppDialog class

3
00:00:11,280 --> 00:00:15,920
to get the user to confirm that they
really do want to delete a record.

4
00:01:08,080 --> 00:01:15,640
I'll add the function to AddEditFragment,
just before saveTask, and call it isDirty.

5
00:01:28,640 --> 00:01:33,720
After creating a new task, using the taskFromUi
function that we used earlier,

6
00:01:33,720 --> 00:01:39,500
the function returns true, if the new task
doesn't equal a task that we're editing.

7
00:01:39,500 --> 00:01:44,940
In the case of adding a task, task will
be null and won't equal newTask.

8
00:01:44,940 --> 00:01:46,760
That can cause a problem,

9
00:01:46,760 --> 00:01:52,520
and the function deals with that by also checking
that the name and description aren't empty,

10
00:01:52,520 --> 00:01:56,880
and if the sort order has changed from the default of zero.

11
00:01:56,880 --> 00:02:03,760
Without those checks, we'd get the dialogue if we tried
to leave a new task, even if nothing had been typed.

12
00:02:03,760 --> 00:02:09,039
Try removing that second compound condition,
if you're not sure why it's necessary.

13
00:02:09,039 --> 00:02:16,040
If newTask and task are equal, the user hasn't
made any changes and the function returns false.

14
00:02:16,040 --> 00:02:22,460
Alright. Back in MainActivity, we can use that function
to decide whether to prompt the user or not.

15
00:02:22,460 --> 00:02:24,900
There are two places we need to do that,

16
00:02:24,900 --> 00:02:28,520
and it makes sense to create a function
to show the dialogue.

17
00:02:28,520 --> 00:02:35,800
I'm going to create it as a Kotlin extension function,
and I'll talk about them in the video after next.

18
00:02:35,800 --> 00:02:45,060
Create a new Kotlin file called ExtensionFunctions.

19
00:02:56,060 --> 00:03:01,780
Kotlin extension functions can be great if
you avoid the temptation to overuse them.

20
00:03:01,340 --> 00:03:07,360
I'll discuss what we're doing here,
once we've seen it working.

21
00:03:20,360 --> 00:03:28,220
The function's code is simple enough. It just puts the
required values into the bundle and creates a dialog.

22
00:03:28,220 --> 00:03:33,760
Something we haven't done before, is used
default values for the function arguments.

23
00:03:33,760 --> 00:03:37,960
That's something that Kotlin lets us do,
that you can't do in Java.

24
00:03:37,960 --> 00:03:46,280
If we just want the default, OK and cancel buttons, we don't
have to specify values for the positive and negative captions.

25
00:03:46,280 --> 00:03:49,160
The default values will be used automatically.

26
00:03:49,160 --> 00:03:53,300
You can find out more about them,
in the Kotlin language reference

27
00:03:53,300 --> 00:04:04,600
at https://kotlinlang.org/docs/reference/functions.html

28
00:04:04,600 --> 00:04:09,420
Alright, I'll add the code to call this function in MainActivity.

29
00:04:09,420 --> 00:04:18,880
As we've done before, create a constant to hold the dialog id.

30
00:04:18,880 --> 00:04:22,960
We'll be using the confirmation dialogue in two places.

31
00:04:22,960 --> 00:04:30,860
I'll deal with onOptionsItemSelected first.

32
00:04:30,860 --> 00:04:34,120
We need to comment out this line,
because we're going to replace it.

33
00:04:53,120 --> 00:05:00,800
We're being quite defensive here, checking to
make sure that the fragment is an AddEditFragment.

34
00:05:00,800 --> 00:05:06,100
At the moment, there's only one fragment that
could result in the Up button being displayed,

35
00:05:06,100 --> 00:05:13,820
but if we add another fragment in the future, and try to
cast it to be an AddEditFragment, we'll get an exception.

36
00:05:13,820 --> 00:05:20,200
If you're going to cast something to another type, then
it's a good idea to make sure it can be cast to that type.

37
00:05:20,200 --> 00:05:26,780
Note that Kotlin's performed a smart cast for us, once
we've checked that the fragment is the correct type.

38
00:05:26,780 --> 00:05:31,240
We've got a couple of errors because we
haven't created the string resources yet.

39
00:05:31,240 --> 00:05:34,600
I'll get the light bulb to create all three.

40
00:05:39,600 --> 00:05:52,940
The message resource will be called
cancelEditDiag_message.

41
00:05:52,940 --> 00:06:01,140
The positive caption will be called
cancelEditDiag_positive_caption,

42
00:06:15,140 --> 00:06:21,480
and the negative one will be
cancelEditDiag_negative_caption.

43
00:06:34,480 --> 00:06:40,700
It can take a couple of goes, sometimes, to get the
lightbulb to appear for the last two, because of the error.

44
00:06:40,700 --> 00:06:43,640
Click in the strings a couple of times if you need to,

45
00:06:43,640 --> 00:06:48,760
until you can select the extract string
resource option from the light bulb.

46
00:06:48,760 --> 00:06:55,240
We also have to remove the getString calls, because
we're passing the resource ID to the DialogFunction.

47
00:07:08,240 --> 00:07:20,640
We can use exactly the same code in the onBackPressed
function, so let's copy the entire if/else block,

48
00:07:29,640 --> 00:07:31,420
and paste it in place.

49
00:07:31,420 --> 00:07:35,180
Okay, I'll finish the video with a challenge.

50
00:07:49,560 --> 00:07:53,700
Have a go at that, and I'll see in the next video.

