1
00:00:04,340 --> 00:00:08,800
G'day everyone, welcome back. The last video finished with a challenge,

2
00:00:08,820 --> 00:00:13,940
to define a callback interface and use it to successfully edit tasks.

3
00:00:13,940 --> 00:00:19,700
There are often different ways to do things and your solution may look a bit different to mine,

4
00:00:19,700 --> 00:00:22,260
but it should be roughly along the same lines,

5
00:00:22,260 --> 00:00:35,220
I'll start by defining the interface in
MainActivityFragment.

6
00:00:35,220 --> 00:00:49,300
Then we'll call the activity's onTaskEdit function in onEditClick.

7
00:00:49,300 --> 00:00:53,700
That's all the changes we have to make to MainActivityFragment,

8
00:00:53,700 --> 00:01:00,620
but there's another change that we should make. We should make sure the activity really does implement the interface.

9
00:01:00,620 --> 00:01:07,260
That causes our code to crash straight away, rather than appearing to work until the user taps a button.

10
00:01:07,260 --> 00:01:12,740
If you can't even run your app without it crashing,
you'll spot there's a problem straight away.

11
00:01:12,740 --> 00:01:16,220
I'll demonstrate that after making the change.

12
00:01:16,220 --> 00:01:20,440
We can't get a reference to the activity before onAttach,

13
00:01:20,440 --> 00:01:32,620
so that's where I'll add the code.

14
00:01:32,620 --> 00:01:38,200
Okay, to see how this can help to spot
problems more easily, I'll run the app.

15
00:01:38,200 --> 00:01:53,240
Remember that we haven't made
MainActivity implement the interface yet.

16
00:01:53,240 --> 00:01:56,460
As soon as the app runs, it crashes.

17
00:01:56,460 --> 00:02:02,760
The message in the log is
"MainActivity must implement onTaskEdit",

18
00:02:02,760 --> 00:02:08,300
which makes it very easy for the programmer to work out what's wrong, and of course to fix it.

19
00:02:08,300 --> 00:02:16,060
That leads nicely onto the next step in the challenge,
implementing the interface in MainActivity.

20
00:02:16,060 --> 00:02:30,640
We'll start by declaring that it does implement the interface.

21
00:02:30,640 --> 00:02:43,460
I'll add the necessary function after onOptionsItemsSelected, before the taskEditRequest function.

22
00:02:43,460 --> 00:02:51,780
Remember, control I will let Android Studio generate the stub for us,

23
00:02:51,780 --> 00:02:56,760
and we can replace the TODO.

24
00:02:56,760 --> 00:03:02,920
It doesn't have a lot to do, because all
the work's already being performed by taskEditRequest.

25
00:03:02,920 --> 00:03:09,160
You may be wondering why we didn't just rename taskEditRequest, to onTaskEdit.

26
00:03:09,160 --> 00:03:12,080
That would work, but it's not a good idea

27
00:03:12,080 --> 00:03:20,540
taskEditRequest accepts a nullable task type, which it has to have when we call it from the plus icon on the menu.

28
00:03:20,540 --> 00:03:24,840
When we're adding a new task,
we pass null for the task argument.

29
00:03:24,840 --> 00:03:29,260
If we called the function that takes a nullable type from our onClickListener,

30
00:03:29,260 --> 00:03:33,060
we'd lose the null type safety that Kotlin provides.

31
00:03:33,060 --> 00:03:39,980
A bug in our code could result in the user
adding a new task when they wanted to edit an existing one.

32
00:03:39,980 --> 00:03:44,140
This way, if we do try to pass a null to onTaskEdit,

33
00:03:44,140 --> 00:03:48,360
the compiler will complain, and we'll detect
the problem straight away.

34
00:03:48,360 --> 00:03:57,280
Ok does it work.

35
00:03:57,280 --> 00:04:08,400
I'll edit task 5. I'll change the description
so that it reads fifth as a word, instead of as a number,

36
00:04:08,400 --> 00:04:16,200
and I'll change the sort order to 7.

37
00:04:16,200 --> 00:04:22,720
Okay, we save and return to the list
and we can see that task 5 has been updated.

38
00:04:22,720 --> 00:04:26,620
It's got the new description and it's moved further down the list,

39
00:04:26,620 --> 00:04:30,900
so the new sort order's taken effect. So that looks like it's working.

40
00:04:30,900 --> 00:04:42,900
What if we change the task name?
I'll edit task 1 and change the name to task 10.

41
00:04:42,900 --> 00:04:47,360
Then I save, and tasks 1's now task 10.

42
00:04:47,360 --> 00:04:50,480
We can tell it's the same task by the description,

43
00:04:50,540 --> 00:04:55,160
and that's one of the reasons that we've been boring in our
choice of names and descriptions.

44
00:04:55,160 --> 00:05:00,500
At some point, you'll want to test the app with more realistic task names and descriptions,

45
00:05:00,500 --> 00:05:05,880
including making sure that those max length values we used are working.

46
00:05:05,880 --> 00:05:09,460
You'll also want to check the display, to see how it looks

47
00:05:09,460 --> 00:05:13,400
when names that use the max length number of characters are entered.

48
00:05:13,400 --> 00:05:18,660
But when you're still developing, it's much easier to verify that things are working as you expect,

49
00:05:18,660 --> 00:05:21,780
if you follow some sort of pattern, like this.

50
00:05:21,780 --> 00:05:25,180
We can also tell what the ID should be from the name,

51
00:05:25,180 --> 00:05:30,440
our new task 10 should have an ID of 1 in the database,

52
00:05:30,440 --> 00:05:35,660
and that can be useful if strange things happen,
and you need to work out what's going on.

53
00:05:35,660 --> 00:05:40,000
So that's the edit button working and the end of the challenge.

54
00:05:40,000 --> 00:05:44,280
Remember, you may not have done things exactly the same way as I did,

55
00:05:44,280 --> 00:05:50,580
but if your app works and you can successfully edit tasks,
then well done.

56
00:05:50,580 --> 00:05:52,480
I'll stop this video here.

57
00:05:52,480 --> 00:05:58,500
In the next video, we'll look at deleting tasks when the delete button's tapped. See you then.

