1
00:00:04,440 --> 00:00:06,900
G'day everyone, welcome back.

2
00:00:06,900 --> 00:00:09,820
We're ready to test our AppDialog class,

3
00:00:09,820 --> 00:00:19,720
but first, I'll review the code that creates our dialog in the onDeleteClick function, on MainActivityFragment.

4
00:00:19,720 --> 00:00:25,680
The code adds the DIALOG_ID, the DIALOG_MESSAGE,

5
00:00:25,680 --> 00:00:32,860
and the caption for the DIALOG_POSITIVE_RID key of the bundle.

6
00:00:32,860 --> 00:00:38,360
After setting up the args bundle,
it then creates a new AppDialog instance.

7
00:00:38,360 --> 00:00:42,020
The next two lines are very similar
to what we did for our fragment,

8
00:00:42,020 --> 00:00:47,260
and we are using a DialogFragment after all,
so it's not really surprising.

9
00:00:47,260 --> 00:00:54,540
AppDialog extends DialogFragment,
so it's a fragment hosting the AlertDialog.

10
00:00:54,540 --> 00:00:58,820
The first argument to the dot show function is a bit different.

11
00:00:58,820 --> 00:01:06,240
Google's examples show how to show a dialog from an activity, but we're showing it from a fragment.

12
00:01:06,240 --> 00:01:12,820
That means we pass a childFragmentManager,
rather than a supportFragmentManager,

13
00:01:12,820 --> 00:01:15,580
which is what Google's examples show.

14
00:01:15,580 --> 00:01:23,920
That second argument to the dialog.show function is an optional tag that we can use if we need to find the fragment again.

15
00:01:23,920 --> 00:01:27,600
I can't think of a reason why you'd want to do that with a dialog,

16
00:01:27,600 --> 00:01:32,400
but it's just providing the same capabilities
that any other fragment would have.

17
00:01:32,400 --> 00:01:35,720
As we're not going to use it, we pass in null.

18
00:01:35,720 --> 00:01:42,240
Okay, that should show the dialog to request
confirmation, before deleting a task.

19
00:01:42,240 --> 00:01:46,980
It's worth running the app at this stage.
We can check that the dialog does appear,

20
00:01:46,980 --> 00:01:50,520
and dismisses itself when we
click one of the buttons.

21
00:01:50,520 --> 00:01:53,020
We'll start by testing our exceptions,

22
00:01:53,020 --> 00:01:59,580
if we try to use the AppDialog class from an activity
that doesn't implement the required interface.

23
00:01:59,580 --> 00:02:06,240
When I run the app and delete a task, we get an error:

24
00:02:28,240 --> 00:02:36,380
ClassCastException: Fragment MainActivityFragment
must implement AppDialog.DialogEvents interface.

25
00:02:36,380 --> 00:02:37,820
So that's pretty good.

26
00:02:37,820 --> 00:02:41,380
The app crashed, but told us exactly why.

27
00:02:41,380 --> 00:02:45,480
In fact, we can copy the full
interface name from the error message,

28
00:02:45,480 --> 00:02:50,540
and add it to the declaration of MainActivityFragment.

29
00:03:04,540 --> 00:03:10,080
We've got the usual error, because we're
not implementing the required function.

30
00:03:10,080 --> 00:03:18,940
I'll get Android Studio to generate the stub using Ctrl I,
after the onTaskLongClick function.

31
00:03:30,940 --> 00:03:42,300
Then add logging, so we can see
the function is being called.

32
00:03:42,300 --> 00:03:50,260
Before I run the app again, remember that we put in a check
that both an ID and a message were provided in the bundle.

33
00:03:50,260 --> 00:03:54,480
We should really test that.

34
00:03:54,480 --> 00:04:01,940
I'll comment out the line that adds the ID to the bundle.

35
00:04:01,940 --> 00:04:15,440
Then run the app again.

36
00:04:15,440 --> 00:04:27,300
Once again, I get an error when we delete
a task.

37
00:04:27,300 --> 00:04:31,180
This time it's complaining that I've got
an illegal argument exception:

38
00:04:31,180 --> 00:04:35,960
DIALOG_ID and/or DIALOG_MESSAGE
are not present in the bundle.

39
00:04:35,960 --> 00:04:41,520
Now really, we should comment out the line that adds
the message, and check that we get the same error.

40
00:04:41,520 --> 00:04:47,680
Then uncomment the ID line, and test we get
the error if only the message isn't present.

41
00:04:47,680 --> 00:04:52,860
I'm not going to do that in this video because
I'm going to run out of tasks to delete,

42
00:04:52,860 --> 00:05:00,160
but you should certainly test all three combinations,
to make sure that our check works in all three situations.

43
00:05:00,160 --> 00:05:03,840
Hang on, I'm going to run out of tasks to delete?

44
00:05:03,840 --> 00:05:10,960
But surely, the tasks will only be deleted if
I confirm it by tapping the dialog's delete button.

45
00:05:10,960 --> 00:05:15,000
Unfortunately not, and I'll be coming to that shortly.

46
00:05:15,000 --> 00:05:20,360
First, I'll uncomment the line that's causing the problem,

47
00:05:20,360 --> 00:05:26,580
and run the app again.

48
00:05:26,580 --> 00:05:29,760
I'm going to delete task 10,

49
00:05:29,780 --> 00:05:36,100
so make a mental note of the task
name as I tap the delete button.

50
00:05:36,100 --> 00:05:37,980
And there's our dialog.

51
00:05:37,980 --> 00:05:42,240
It clearly states what the impact of deleting the record will be,

52
00:05:42,240 --> 00:05:46,940
and offers the user options to go ahead,
or cancel the operation.

53
00:05:46,940 --> 00:05:48,400
Okay.

54
00:05:48,400 --> 00:05:51,860
Back to my running out of tasks comment.

55
00:05:51,860 --> 00:05:55,800
When you shift the focus from the dialog to the lists behind it,

56
00:05:55,800 --> 00:05:59,340
notice that task 10 has gone from the list.

57
00:05:59,340 --> 00:06:01,140
It's already deleted.

58
00:06:01,140 --> 00:06:06,640
Now, if you're new to programming,
or new to programming in a GUI environment,

59
00:06:06,640 --> 00:06:09,660
you'll probably see nothing strange about that.

60
00:06:09,660 --> 00:06:15,620
After all, if I switch back to look
at the onDeleteClick function,

61
00:06:15,620 --> 00:06:23,720
we can see it sets up the dialog, shows it, and then
calls the ViewModel's delete function to delete the task.

62
00:06:23,720 --> 00:06:29,140
On the other hand, if you've got experience programming for Windows, for example,

63
00:06:29,140 --> 00:06:34,980
then you may have expected the code in
onDeleteClick to pause after showing the dialog,

64
00:06:34,980 --> 00:06:38,449
and only resume once a dialog's dismissed.

65
00:06:38,449 --> 00:06:43,280
The term for the Windows behavior I've
just described, is modal.

66
00:07:09,760 --> 00:07:13,160
Obviously, we have to make a change.

67
00:07:13,160 --> 00:07:20,560
We have to move that call to the ViewModel's
delete function into onPositiveDialogResult.

68
00:11:27,900 --> 00:11:32,560
Okay, we still haven't managed to
confirm the deletion of tasks,

69
00:11:32,560 --> 00:11:36,360
but it looks like our AppDialog class is working fine.

70
00:11:36,360 --> 00:11:40,440
The next step is to get MainActivity to use it properly,

71
00:11:40,440 --> 00:11:43,280
and we'll do that in the next video.

72
00:11:43,280 --> 00:11:45,820
See you then.

