1
00:00:05,500 --> 00:00:06,500
Welcome back.

2
00:00:06,800 --> 00:00:10,300
Our second improvement is to prevent a task from being deleted

3
00:00:10,500 --> 00:00:12,000
while it's being edited.

4
00:00:12,700 --> 00:00:14,600
That's only possible in landscape.

5
00:00:14,960 --> 00:00:17,660
So run the app then rotate into landscape.

6
00:00:26,260 --> 00:00:29,510
There's nothing to prevent the user from editing a task

7
00:00:29,510 --> 00:00:32,710
then deleting the same task by clicking its button.

8
00:00:33,310 --> 00:00:37,210
We'll add two more tasks so we've got something consistent to look at.

9
00:00:37,760 --> 00:00:40,660
By now you'll probably have a different set of tasks to me,

10
00:00:40,960 --> 00:00:44,560
especially if you've been experimenting with the interface and I hope you have.

11
00:00:45,160 --> 00:00:48,360
I'll call the first new task "Edit me"

12
00:00:59,660 --> 00:01:01,660
and the second one "Delete me".

13
00:01:08,960 --> 00:01:12,520
The description and sort order can be blank for both tasks,

14
00:01:12,520 --> 00:01:14,520
but make sure of course that you hit Save.

15
00:01:15,070 --> 00:01:18,070
Okay. Edit the Edit me task

16
00:01:18,870 --> 00:01:22,870
still in landscape, then click the button to delete

17
00:01:22,870 --> 00:01:24,670
the Delete me task.

18
00:01:27,470 --> 00:01:29,270
Choose Delete from the dialog,

19
00:01:30,670 --> 00:01:32,070
and the task is deleted.

20
00:01:32,730 --> 00:01:34,230
That seems to work fine.

21
00:01:34,730 --> 00:01:37,390
To be sure, enter a description

22
00:01:47,590 --> 00:01:48,580
and hit Save.

23
00:01:50,780 --> 00:01:51,780
That worked fine.

24
00:01:52,330 --> 00:01:56,030
So there's no problem allowing the user to delete one task

25
00:01:56,030 --> 00:01:57,630
while editing another one.

26
00:01:58,330 --> 00:02:00,630
Edit the Edit me task again.

27
00:02:01,680 --> 00:02:03,280
Tap the Delete button

28
00:02:05,270 --> 00:02:08,370
to delete the Edit me task that we're editing

29
00:02:08,370 --> 00:02:09,870
then choose Delete again.

30
00:02:11,120 --> 00:02:13,120
Now the app's in a strange state.

31
00:02:13,370 --> 00:02:16,870
It's editing a task that no longer exists in the database,

32
00:02:17,470 --> 00:02:19,770
and that's probably not going to go too well.

33
00:02:20,270 --> 00:02:22,070
So click Save to check.

34
00:02:23,670 --> 00:02:27,370
Sure enough, we lose any edits that the user may have made.

35
00:02:28,030 --> 00:02:30,030
Data loss is unforgivable,

36
00:02:30,230 --> 00:02:32,730
so we really need to do something about that one.

37
00:02:33,730 --> 00:02:36,930
You may think that the user would be fully aware of what they've done.

38
00:02:37,230 --> 00:02:40,430
We did ask for confirmation of the delete after all.

39
00:02:41,090 --> 00:02:44,590
But there's a big difference between running our apps on an emulator

40
00:02:44,590 --> 00:02:48,790
on a computer monitor and using the apps on a real mobile device.

41
00:02:49,590 --> 00:02:51,850
Users can pass a phone back and forth

42
00:02:52,250 --> 00:02:55,150
and tap things by accident when they do.

43
00:02:55,850 --> 00:02:58,450
So bear that in mind when testing on an emulator.

44
00:02:58,450 --> 00:03:02,950
It's not a true reflection of what happens to a handheld device with a touch screen.

45
00:03:04,150 --> 00:03:07,510
Editing a task is triggered by MainActivityFragment,

46
00:03:08,060 --> 00:03:11,060
that's the same class that responds to the delete button.

47
00:03:11,560 --> 00:03:15,560
So you may think it's going to be easy to track which task is being edited.

48
00:03:16,360 --> 00:03:18,860
Unfortunately, it's not that easy.

49
00:03:19,760 --> 00:03:22,960
MainActivityFragment knows when an edit starts

50
00:03:22,960 --> 00:03:24,960
and can store the task ID somewhere,

51
00:03:25,760 --> 00:03:29,120
but it doesn't know when a task is no longer being edited.

52
00:03:30,120 --> 00:03:33,020
We need some way for AddEditFragment

53
00:03:33,020 --> 00:03:36,020
to communicate with MainActivityFragment.

54
00:03:37,220 --> 00:03:38,720
We're using a ViewModel

55
00:03:38,970 --> 00:03:41,870
and that's the most reliable way to implement fragment

56
00:03:41,870 --> 00:03:43,370
to fragment communication.

57
00:03:44,270 --> 00:03:48,530
In TaskTimerModel, we'll create a read-only attribute

58
00:03:48,530 --> 00:03:50,530
called edited task ID.

59
00:03:51,230 --> 00:03:53,430
I'll put it just before the init block.

60
00:04:24,030 --> 00:04:26,630
Making the set function as private

61
00:04:26,930 --> 00:04:30,030
prevents external code from modifying the attribute.

62
00:04:32,030 --> 00:04:35,330
I'm going to use a pair of functions to change the value.

63
00:04:35,990 --> 00:04:39,590
Getters and setters aren't as common in Kotlin as they are in java,

64
00:04:39,890 --> 00:04:42,090
but they still exist and they have their uses.

65
00:04:42,590 --> 00:04:46,490
I'll paste the function in below the SaveTasks function

66
00:04:46,490 --> 00:04:48,690
then explain why I've used them.

67
00:04:57,790 --> 00:05:00,290
StopEditing is straightforward.

68
00:05:00,290 --> 00:05:04,190
It just sets the edit task ID attribute to zero.

69
00:05:04,990 --> 00:05:09,590
StartEditing includes some code that will only be used in our debug build.

70
00:05:10,590 --> 00:05:13,490
We want to be sure that there isn't a task being edited

71
00:05:13,490 --> 00:05:15,590
when we StartEditing another one.

72
00:05:16,690 --> 00:05:20,190
If we forget to StopEditing a task when the user's finished editing it,

73
00:05:20,590 --> 00:05:22,890
then that's a bug in the logic of our code.

74
00:05:23,490 --> 00:05:26,990
We want to detect that and fix the code if it happens.

75
00:05:28,190 --> 00:05:30,990
Okay. Our AddEditFragment

76
00:05:30,990 --> 00:05:34,790
will tell the ViewModel when it started and stops editing a task.

77
00:05:35,190 --> 00:05:36,790
Let's make that change next.

78
00:06:03,150 --> 00:06:06,810
We only need to do that when editing not when adding a task.

79
00:06:07,910 --> 00:06:10,210
A user can't delete a new task

80
00:06:10,210 --> 00:06:12,710
because it doesn't exist in the RecyclerView,

81
00:06:13,010 --> 00:06:15,010
so there's no delete button to tap.

82
00:06:15,260 --> 00:06:18,360
We StopEditing in the OnDetach function.

83
00:06:29,020 --> 00:06:32,820
Now we can test that value in MainActivityFragment

84
00:06:33,220 --> 00:06:36,220
and display a toast message instead of deleting.

85
00:07:54,220 --> 00:07:57,820
Use the light bulb to extract the text on line 71

86
00:08:02,220 --> 00:08:06,520
and call the resource delete_edited_task.

87
00:08:14,120 --> 00:08:17,120
That's a change completed, and we can test it.

88
00:08:17,780 --> 00:08:20,780
Run the app, and edit the task in landscape.

89
00:08:39,679 --> 00:08:43,179
Then use its delete button to try to delete it.

90
00:08:45,480 --> 00:08:48,480
We get the toast message instead and that's much better.

91
00:08:48,980 --> 00:08:51,340
Make sure you test deleting other tasks

92
00:08:51,640 --> 00:08:53,640
to make sure we haven't broken anything.

93
00:09:06,940 --> 00:09:09,300
There's a very important point to consider here.

94
00:09:09,960 --> 00:09:11,960
Remember back in section 12,

95
00:09:11,960 --> 00:09:16,110
we talked about sharing the ViewModelInstance between the fragments.

96
00:09:16,110 --> 00:09:19,010
It's crucial that you pass the activity

97
00:09:19,010 --> 00:09:22,310
when getting your ViewModelInstances in the fragments.

98
00:09:22,670 --> 00:09:23,970
That's our code

99
00:09:25,770 --> 00:09:27,270
up on line 32.

100
00:09:28,070 --> 00:09:31,870
If each fragment is using its own instance of the ViewModel,

101
00:09:31,870 --> 00:09:34,070
then this communication won't work.

102
00:09:34,620 --> 00:09:38,620
It's important to use by ActivityViewModels here

103
00:09:38,620 --> 00:09:40,920
and in the AddEditFragment class.

104
00:09:41,720 --> 00:09:45,620
If you need to get two or more fragments to communicate with each other,

105
00:09:45,620 --> 00:09:48,980
then doing it via the view model is a great way to achieve that.

106
00:09:49,780 --> 00:09:51,180
You may want to turn this around.

107
00:09:51,580 --> 00:09:55,580
At the moment, we've disabled deleting if a task is being edited.

108
00:09:56,240 --> 00:10:00,040
You may prefer to close the editing pane if the task is deleted.

109
00:10:00,640 --> 00:10:02,840
In that case, AddEditFragment

110
00:10:02,840 --> 00:10:05,740
would observe a LiveData object in the ViewModel

111
00:10:06,040 --> 00:10:07,640
and quietly close itself

112
00:10:08,140 --> 00:10:10,340
if the task it's editing gets deleted.

113
00:10:11,340 --> 00:10:14,940
That may be more appropriate. When creating your apps, you decide how they're going to function. 

114
00:10:17,420 --> 00:10:21,570
In the next video, we'll look at removing the delete button from the layout

115
00:10:21,570 --> 00:10:25,670
and we'll implement delete by swiping instead. I'll see you there.

