1
00:00:05,200 --> 00:00:06,200
Welcome back.

2
00:00:06,500 --> 00:00:10,000
We're now ready to change our MainActivityFragment class

3
00:00:10,400 --> 00:00:14,600
to restore the SwipeTask if the user cancels the deletion.

4
00:00:20,850 --> 00:00:23,150
The RecyclerView will do most of the work.

5
00:00:23,650 --> 00:00:27,350
All we have to do is call NotifyItemChanged

6
00:00:27,350 --> 00:00:28,350
on our adapter.

7
00:00:29,050 --> 00:00:31,350
The RecyclerView observes its adapter

8
00:00:31,350 --> 00:00:34,250
and will redraw the item that was removed from the list.

9
00:00:35,050 --> 00:00:37,350
We need to tell it which item was changed,

10
00:00:37,710 --> 00:00:40,010
and we do that by passing the position

11
00:00:40,310 --> 00:00:43,310
when we call NotifyItemChanged.

12
00:00:44,200 --> 00:00:46,100
I'm going to write this code backwards.

13
00:00:46,760 --> 00:00:49,960
Now I don't mean I'm going to be typing nuf instead of fun.

14
00:00:50,360 --> 00:00:54,660
I'm going to assume we have the items position and write the code to use it.

15
00:00:55,210 --> 00:00:59,760
We'll worry about getting the position once we've seen how we're going to use it.

16
00:01:00,460 --> 00:01:03,450
I think it will be easier to understand that bit of code

17
00:01:03,450 --> 00:01:05,650
if we've seen what we're trying to do.

18
00:01:06,850 --> 00:01:09,850
We'll restore the item the DeletedTask

19
00:01:09,850 --> 00:01:13,420
in the dialogues onNegativeDialogResult function

20
00:01:14,020 --> 00:01:17,220
that's called when the user taps the dialog's cancel button.

21
00:01:18,420 --> 00:01:22,220
I'll use control O to generate the function after

22
00:01:22,220 --> 00:01:24,220
OnPositiveDialogResult.

23
00:02:51,220 --> 00:02:53,120
On line 126,

24
00:02:53,270 --> 00:02:56,770
we'll call the adapter's NotifyItemChanged function

25
00:02:57,170 --> 00:02:59,570
passing in the position of the item that changed.

26
00:03:00,270 --> 00:03:02,630
That's the position of the task that was swiped.

27
00:03:03,290 --> 00:03:05,890
It should be obvious from line 123

28
00:03:05,890 --> 00:03:09,090
that we're going to pass the position in the dialog's arguments.

29
00:03:09,970 --> 00:03:13,470
The arguments are passed into this function in the args bundle.

30
00:03:13,970 --> 00:03:16,170
We've seen this before a few times now.

31
00:03:17,170 --> 00:03:20,570
I'll create the DialogTaskPosition constant

32
00:03:20,930 --> 00:03:23,130
up with the others at the top of the file.

33
00:03:44,130 --> 00:03:48,230
Okay. That's fixed the error. Of course, the code won't work yet.

34
00:03:48,730 --> 00:03:51,720
We haven't added the position to the dialog's arguments.

35
00:03:52,380 --> 00:03:56,380
The dialog is created in our OnDeleteClick function,

36
00:03:56,980 --> 00:03:59,180
so that's where we'll add the position to the bundle.

37
00:04:27,780 --> 00:04:30,780
The OnDeleteClick will get the position as an argument,

38
00:04:31,440 --> 00:04:34,540
it then puts it into the bundle ready for us to retrieve

39
00:04:34,540 --> 00:04:36,340
if the deletion is cancelled.

40
00:04:37,540 --> 00:04:40,240
Now we're left with the problem of how to get the position.

41
00:04:41,120 --> 00:04:43,120
Fortunately, that's easy.

42
00:04:43,120 --> 00:04:45,420
It's an attribute of the ViewHolder

43
00:04:45,420 --> 00:04:48,020
that's passed to the OnSwiped function.

44
00:04:48,620 --> 00:04:52,420
Let's have a look at that function. Up on line 74,

45
00:04:54,620 --> 00:04:56,920
we get past two arguments;

46
00:04:57,520 --> 00:04:59,720
the ViewHolder of the item that was swiped

47
00:05:00,270 --> 00:05:02,270
and the direction in which it was swiped.

48
00:05:03,070 --> 00:05:07,170
Passing the ViewHoldersAdapter position to OnDeleteClick

49
00:05:07,470 --> 00:05:08,470
will fix the error.

50
00:05:08,830 --> 00:05:10,080
On line 78.

51
00:05:22,880 --> 00:05:25,180
We get the position in OnSwiped

52
00:05:25,180 --> 00:05:28,180
and we pass it to our OnDeleteClicked function.

53
00:05:28,980 --> 00:05:32,480
That puts it into the dialog's bundle, so that we can retrieve it

54
00:05:32,480 --> 00:05:36,080
in the OnNegativeDialogResult function and use it

55
00:05:36,080 --> 00:05:38,880
when we call NotifyItemChanged.

56
00:05:39,680 --> 00:05:43,280
The RecyclerView and RecycleRviewAdapter classes

57
00:05:43,280 --> 00:05:45,780
were designed with this kind of use in mind,

58
00:05:46,180 --> 00:05:49,180
which makes it very easy to implement swiping operations.

59
00:05:50,170 --> 00:05:54,370
Let's see if it works. Run the application and swipe the task.

60
00:05:58,670 --> 00:06:01,570
Notice that it disappears from the RecyclerView

61
00:06:01,570 --> 00:06:04,970
before the dialog appears. That's how swiping works

62
00:06:05,370 --> 00:06:09,770
but it's still in the database and it also still exists in the adapter.

63
00:06:10,170 --> 00:06:11,770
We haven't deleted it yet.

64
00:06:12,370 --> 00:06:15,370
It's only the RecyclerViewsDisplay that's changed.

65
00:06:15,970 --> 00:06:16,960
Tap cancel

66
00:06:19,660 --> 00:06:21,160
and the task reappears.

67
00:06:21,960 --> 00:06:23,860
Okay. All that's left to do

68
00:06:23,860 --> 00:06:26,840
is edit the task list items layout and delete

69
00:06:27,140 --> 00:06:29,040
the delete button. 

70
00:06:29,740 --> 00:06:33,540
That will leave the tli edit button unconstrained

71
00:06:33,940 --> 00:06:36,640
so constrain it to the right edge of the layout

72
00:06:36,640 --> 00:06:38,300
with a margin of 8DP.

73
00:06:38,800 --> 00:06:41,900
Its top constraint should be to the top of the layout

74
00:06:41,900 --> 00:06:44,100
again with the margin of 8dp.

75
00:07:15,100 --> 00:07:19,000
Clicking the plus icons in the constraint widget diagram on the right

76
00:07:19,000 --> 00:07:21,200
is a quick way to create those constraints

77
00:07:21,600 --> 00:07:25,960
but watch the margins and change them to 8 if another value is assigned.

78
00:07:26,860 --> 00:07:29,220
Run the app again and test it thoroughly.

79
00:07:46,220 --> 00:07:49,520
When you've found the bug your challenge is to fix it.

80
00:08:03,320 --> 00:08:07,020
Here's a hint: try swiping to delete the task that's being edited

81
00:08:07,220 --> 00:08:08,720
when you're in landscape mode.

82
00:08:09,080 --> 00:08:12,580
I'll go over my solution in the next video. I'll see you there.

