1
00:00:05,150 --> 00:00:06,450
And welcome back, everyone.

2
00:00:07,000 --> 00:00:10,700
We've just seen our task vanished from the RecyclerView when we swipe it.

3
00:00:11,100 --> 00:00:12,500
That's normal behavior.

4
00:00:13,200 --> 00:00:17,840
What we have to do is make it appear again if the user cancels the deletion.

5
00:00:18,820 --> 00:00:19,810
It's easy to do.

6
00:00:20,360 --> 00:00:23,060
We need to get the position of the item that was swiped

7
00:00:23,360 --> 00:00:25,760
then tell the RecyclerView to put it back.

8
00:00:26,950 --> 00:00:29,850
We do that by calling the adapter's 

9
00:00:29,850 --> 00:00:31,650
notifyItemChanged function

10
00:00:32,009 --> 00:00:35,010
and passing in the position of the item that was swiped.

11
00:00:35,370 --> 00:00:37,370
We'll do that in the dialogue's

12
00:00:37,370 --> 00:00:40,030
onNegativeDialogResult callback.

13
00:00:41,020 --> 00:00:44,020
Unfortunately, we decided not to implement that.

14
00:00:44,520 --> 00:00:47,520
So we'll have to make a couple of changes to AppDialog.

15
00:00:48,770 --> 00:00:52,270
Let's have a look in there at our dialog events interface

16
00:00:52,270 --> 00:00:53,660
at the start of the class.

17
00:00:59,160 --> 00:01:02,060
Now we've seen that interfaces are contracts,

18
00:01:02,860 --> 00:01:06,060
and you shouldn't change an interface once you've published it.

19
00:01:07,060 --> 00:01:10,060
If we add an OnNegativeDialogResult function

20
00:01:10,060 --> 00:01:12,060
to the DialogEvents interface,

21
00:01:12,060 --> 00:01:14,060
we'll break our existing classes.

22
00:01:15,420 --> 00:01:18,020
Fortunately, the android JVM

23
00:01:18,020 --> 00:01:20,720
now supports some Java 8 features.

24
00:01:21,220 --> 00:01:25,120
Not all of them, but it does support default implementations

25
00:01:25,120 --> 00:01:26,110
in interfaces.

26
00:01:26,990 --> 00:01:28,590
I'll create the problem,

27
00:01:28,590 --> 00:01:32,190
then we'll see how a default implementation can solve it.

28
00:01:33,180 --> 00:01:37,640
Start by uncommenting the two functions we decided not to implement.

29
00:01:43,140 --> 00:01:46,020
Anything that declares it implements this interface

30
00:01:46,020 --> 00:01:49,420
now has to implement those two additional functions.

31
00:01:50,410 --> 00:01:51,710
Rebuild the project,

32
00:01:55,210 --> 00:01:57,210
and see how much havoc we've caused.

33
00:02:00,210 --> 00:02:03,710
The build fails, and we've broken three classes.

34
00:02:04,610 --> 00:02:07,910
You can expand the messages in the build output pane

35
00:02:07,910 --> 00:02:10,210
to see that they all have the same error.

36
00:02:10,870 --> 00:02:14,570
We're not implementing the OnNegativeDialogResult function.

37
00:02:15,370 --> 00:02:16,370
That's not good.

38
00:02:17,030 --> 00:02:20,330
We don't really want to go changing unrelated classes

39
00:02:20,330 --> 00:02:23,330
just to make a change to the way we delete tasks.

40
00:02:24,630 --> 00:02:27,630
This is where a default implementation can save the day.

41
00:02:28,330 --> 00:02:32,330
If we provide a default implementation for the functions in an interface,

42
00:02:32,330 --> 00:02:34,830
java will use that implementation

43
00:02:34,830 --> 00:02:38,430
and not insist that our classes override the functions.

44
00:02:39,330 --> 00:02:41,690
Our default implementations won't do anything,

45
00:02:42,090 --> 00:02:45,790
we'll put an empty code block after the interface methods.

46
00:03:00,490 --> 00:03:03,990
Rebuild the project again, and we no longer get the errors.

47
00:03:09,980 --> 00:03:12,340
Our interface is now easier to use.

48
00:03:12,840 --> 00:03:16,240
Classes can override only those functions that they need,

49
00:03:16,240 --> 00:03:19,740
and don't have to provide a dummy implementation of the others.

50
00:03:20,620 --> 00:03:25,120
Our original code for AppDialog was written before Java 8 became available

51
00:03:25,120 --> 00:03:28,820
in the android JVM. We've now improved it,

52
00:03:28,820 --> 00:03:33,080
making it easy to use while still providing the ability to respond

53
00:03:33,080 --> 00:03:36,380
to any of the three ways the user may close the dialog.

54
00:03:37,480 --> 00:03:40,470
Android studio versions 3.whatever

55
00:03:40,470 --> 00:03:44,070
now support some of the new features added in Java 8.

56
00:03:44,730 --> 00:03:48,030
Default method implementations in interfaces is supported

57
00:03:48,390 --> 00:03:51,490
and that allows us to make our AppDialog class

58
00:03:51,490 --> 00:03:52,850
a bit easier to use.

59
00:03:54,210 --> 00:03:56,010
Before we finish with this class,

60
00:03:56,010 --> 00:04:00,010
we should uncomment the call to the listener's NegativeResult function

61
00:04:00,410 --> 00:04:02,710
and support the dialog being cancelled.

62
00:04:09,510 --> 00:04:13,310
That's down near the end of the class, near the CreateDialog function.

63
00:04:17,709 --> 00:04:20,110
I'll uncomment the OnCancel function.

64
00:04:22,410 --> 00:04:23,660
We weren't using it,

65
00:04:23,660 --> 00:04:27,160
but now we're notifying our listener if the dialog gets cancelled.

66
00:04:36,460 --> 00:04:39,960
The import you want is android.content

67
00:04:39,960 --> 00:04:42,460
.dialog interface if you're prompted.

68
00:04:42,820 --> 00:04:46,020
We saw that OnCancel function in an earlier video

69
00:04:46,380 --> 00:04:49,880
when we discussed making all three functions part of the interface.

70
00:04:50,540 --> 00:04:53,840
Now with default implementations in java 8,

71
00:04:53,840 --> 00:04:55,640
we've got the best of both worlds.

72
00:04:56,240 --> 00:05:00,340
Our interface can support positive and negative results and cancel

73
00:05:00,740 --> 00:05:04,140
without forcing classes to implement all three functions.

74
00:05:04,640 --> 00:05:07,640
We've got a strong warning on line 116.

75
00:05:08,440 --> 00:05:12,440
I mentioned this a few videos ago when we talked about RequireContext.

76
00:05:12,800 --> 00:05:13,700
This is similar.

77
00:05:14,690 --> 00:05:17,490
We've had to use arguments bang bang

78
00:05:17,850 --> 00:05:21,350
because the compiler can't be sure that our arguments aren't null.

79
00:05:22,010 --> 00:05:26,010
We know it's not null. We check that in the OnCreateDialog function,

80
00:05:26,370 --> 00:05:27,970
but the compiler can't be sure.

81
00:05:28,960 --> 00:05:33,360
Using the bang bang operator is how we tell the compiler that we know what we're doing.

82
00:05:34,350 --> 00:05:36,550
Now there's a slightly neater way.

83
00:05:36,910 --> 00:05:40,110
Click on arguments on line 116,

84
00:05:40,510 --> 00:05:43,410
and accept the suggestion to replace with

85
00:05:43,410 --> 00:05:45,400
require arguments instead.

86
00:05:49,280 --> 00:05:53,080
I won't go into it all again, refer to the video entitled

87
00:05:53,080 --> 00:05:56,440
"Another Breaking Change" earlier in this section

88
00:05:56,440 --> 00:05:58,940
if you want to remind yourself what this is all about.

89
00:05:59,820 --> 00:06:03,720
In the next video we'll see how to use the NegativeDialogResult

90
00:06:03,720 --> 00:06:06,720
to make the task appear in the RecyclerView again.

91
00:06:06,720 --> 00:06:07,820
I'll see you over there.

