1
00:00:04,860 --> 00:00:12,800
G'day everyone, welcome back. Okay, we've got our edit button working so let's look at the delete button.

2
00:00:12,800 --> 00:00:17,680
It's tempting to dive straight in and add some code to MainActivityFragment.

3
00:00:17,680 --> 00:00:25,520
After all, it's now got an onDeleteClick function that gets called when the delete button is tapped.

4
00:00:25,520 --> 00:00:29,520
If we delete the tasks from there, it will work fine.

5
00:00:29,520 --> 00:00:35,140
The problem is that our app's architecture is starting to get a bit messy.

6
00:00:35,140 --> 00:00:40,360
With the introduction of the ViewModel class,
LiveData and data binding,

7
00:00:40,360 --> 00:00:46,560
Google are making it much easier
to implement an MVVM architecture.

8
00:00:46,560 --> 00:00:52,360
I should point out that they've made sure
these components will also work with other architectures.

9
00:00:52,360 --> 00:00:56,340
Google aren't suggesting you should change your app's architecture.

10
00:00:56,340 --> 00:01:02,120
We're sort of using MVVM in this app,
so let's have a quick look at what it's all about.

11
00:01:02,120 --> 00:01:07,600
This Wikipedia page gives a brief overview of MVVM.

12
00:01:07,600 --> 00:01:13,880
It's not strange that the word, separation, appears very early on in their description.

13
00:01:13,880 --> 00:01:20,180
All the architectures; mvvm, MVP, MVC and so on,

14
00:01:20,180 --> 00:01:23,560
all advocate separation of functionality.

15
00:01:23,560 --> 00:01:27,940
The UI component should deal only with the user interface,
for example.

16
00:01:27,940 --> 00:01:31,760
I'll click on that diagram at the top right,

17
00:01:31,760 --> 00:01:38,140
and the first thing you may think is that
Model View ViewModel isn't the best name.

18
00:01:38,140 --> 00:01:44,560
View ViewModel Model more accurately reflects how the parts of the system communicate with each other,

19
00:01:44,560 --> 00:01:47,000
but the acronym is not as good.

20
00:01:47,000 --> 00:01:53,280
The View is the user interface,
and it gets the data to display from the ViewModel.

21
00:01:53,280 --> 00:01:56,460
The ViewModel requests data from the Model.

22
00:01:56,460 --> 00:02:01,720
Looking at that diagram, the Model is also responsible for the business logic.

23
00:02:01,720 --> 00:02:08,199
We haven't got a lot of business logic at the moment, but what there is exists in our database.

24
00:02:08,199 --> 00:02:11,840
For example, the task name can't be blank.

25
00:02:11,840 --> 00:02:16,260
That's business logic, and our database enforces that.

26
00:02:16,260 --> 00:02:23,540
Okay, that's the theory of MVVM.
Let's have a look at how our app fits into it.

27
00:06:41,760 --> 00:06:49,960
Now that we've decided how things will work, we can add a function to the TaskTimerViewModel class, to delete a task

28
00:06:49,960 --> 00:06:54,740
I'll add it after the loadTasks function.

29
00:06:54,740 --> 00:07:03,760
We'll pass in the ID of the task to delete, and call the
ContentResolver's delete function to perform the deletion.

30
00:07:03,760 --> 00:07:11,580
Our ContentProvider will take care of
deleting an individual task, if its ID is provided in the URI.

31
00:07:11,580 --> 00:07:17,580
The buildURIFromId function that we added to the TasksContract class,

32
00:07:17,580 --> 00:07:21,300
will return a URI with the ID appended.

33
00:07:21,300 --> 00:07:26,400
Because we're providing the ID, we don't need to use the last two parameters;

34
00:07:26,400 --> 00:07:30,040
the where clause and selection args.

35
00:07:30,040 --> 00:07:35,180
We just build up the URI from the ID of the task that's been passed to the function,

36
00:07:35,180 --> 00:07:38,860
then call the delete function of the Content resolver.

37
00:07:38,860 --> 00:07:46,360
The onDeleteClick function in MainActivityFragment will then call
the ViewModel's delete task function,

38
00:07:46,360 --> 00:07:59,300
to tell the ViewModel to delete the task.

39
00:07:59,300 --> 00:08:07,520
Let's see if it works. I'll restart the app, which, as I've mentioned is a good idea when working with databases,

40
00:08:07,520 --> 00:08:18,960
when you've got several classes all working together like this.

41
00:08:18,960 --> 00:08:23,860
Android Studio's instant run tries to avoid restarting the app,

42
00:08:23,860 --> 00:08:29,800
and can speed up the development and testing cycle,
but when anything asynchronous is going on,

43
00:08:29,800 --> 00:08:33,220
I think it's a good idea to always start from scratch.

44
00:08:33,220 --> 00:08:41,419
Ok, I'll click the delete button for task 5,
and it vanishes from the list.

45
00:08:41,419 --> 00:08:43,440
So that's looking good.

46
00:08:43,440 --> 00:08:49,220
It's a bit drastic, and there's no prompt to make sure that you really wanted to delete the task.

47
00:08:49,220 --> 00:08:53,440
In fact, there are a few enhancements we should make to
the user interface,

48
00:08:53,440 --> 00:08:56,580
and we'll be looking at them a bit later on.

49
00:08:56,580 --> 00:09:02,860
Now, to test this more fully, we should go
on to delete random tasks.

50
00:09:02,860 --> 00:09:09,200
We need to make sure that things like deleting the first and the last one in the list, work successfully,

51
00:09:09,200 --> 00:09:13,700
then keep going to make sure that deleting the final task works as well,

52
00:09:13,700 --> 00:09:17,960
and the results in the instructions are being displayed again.

53
00:09:17,960 --> 00:09:26,560
It does, but I'm sure you don't want to spend time watching me
type in all the tasks again, so I'll stop the video here.

54
00:09:26,560 --> 00:09:30,240
In the next video, we've got a bit of tidying up to do.

55
00:09:30,240 --> 00:09:34,980
We saw from the diagrams that
AddEditFragment needs to be changed.

56
00:09:34,980 --> 00:09:38,500
It should be delegating the
saving to the ViewModel.

57
00:09:38,500 --> 00:09:43,460
There's something else wrong with our app too.
You may have already spotted it,

58
00:09:43,460 --> 00:09:48,660
because we're doing something that the
documentation says we shouldn't do.

59
00:09:48,660 --> 00:09:51,580
I hinted at this a few videos ago.

60
00:09:51,580 --> 00:09:55,960
See if you can spot what it is and we'll fix it in the next video.

61
00:09:55,960 --> 00:09:58,640
I'll see you then.

