1
00:00:05,200 --> 00:00:08,200
Welcome back. All right let's review our code

2
00:00:08,500 --> 00:00:12,300
and see if there's anything we can improve or anything we shouldn't be doing.

3
00:00:13,000 --> 00:00:16,560
This is also a good chance to check any suggestions or warnings

4
00:00:16,560 --> 00:00:19,460
that android studio's lint checker is giving us.

5
00:00:20,360 --> 00:00:24,760
The first one's in AddEditFragment. So open that file up.

6
00:00:34,560 --> 00:00:38,160
I'll close the project pane to make more room on the screen.

7
00:00:40,150 --> 00:00:44,450
A new version of the androidx lifecycle extensions library was released

8
00:00:44,700 --> 00:00:46,700
towards the end of January 2020.

9
00:00:47,600 --> 00:00:51,400
You'll have seen me change my version of that library in the last video.

10
00:00:52,300 --> 00:00:54,100
That causes a small problem

11
00:00:54,400 --> 00:00:58,600
because Google have deprecated the ViewModelProviders class.

12
00:00:59,500 --> 00:01:02,500
You'll have a warning about that on line 12

13
00:01:02,500 --> 00:01:05,800
and then again when we use it on line 33.

14
00:01:06,300 --> 00:01:08,500
It's no longer necessary to use it.

15
00:01:08,800 --> 00:01:12,400
We can call the ViewModelProvider constructor directly.

16
00:01:28,650 --> 00:01:31,850
If your Kotlin classes aren't importing automatically,

17
00:01:32,100 --> 00:01:37,000
go to settings then editor general auto import.

18
00:01:45,360 --> 00:01:49,350
The recent versions of android studio now have a separate section

19
00:01:49,350 --> 00:01:52,950
where you can specify the auto import behavior for Kotlin.

20
00:01:53,550 --> 00:01:55,150
Tick both these boxes

21
00:01:55,950 --> 00:01:59,750
if you want android studio to auto import into your Kotlin code.

22
00:02:02,740 --> 00:02:07,370
We need to make the same change in our other three activity and fragment classes,

23
00:02:07,870 --> 00:02:09,669
but we'll do that when we come to them.

24
00:02:10,870 --> 00:02:13,470
There's a suggestion on line 58,

25
00:02:13,470 --> 00:02:15,670
in the OnViewCreated function.

26
00:02:16,470 --> 00:02:19,470
We should use Kotlin's ToString function

27
00:02:19,470 --> 00:02:22,470
rather than calling the function in the integer class.

28
00:02:26,470 --> 00:02:28,970
Click the ToString function in the code

29
00:02:28,970 --> 00:02:31,470
and use the lightbulb to accept the suggestion.

30
00:02:33,670 --> 00:02:35,970
The next change is a bit more serious.

31
00:02:36,470 --> 00:02:38,970
Have a look in on activity created

32
00:02:39,170 --> 00:02:41,070
and see if you can spot what it is.

33
00:02:43,570 --> 00:02:48,470
The function works but the code is doing something that it really shouldn't be doing.

34
00:02:48,830 --> 00:02:51,630
What we've done is assumed that the listener

35
00:02:51,630 --> 00:02:55,430
on line 106 will always be our activity.

36
00:02:55,430 --> 00:02:59,630
Most of the time, it probably will be but it doesn't have to be.

37
00:02:59,990 --> 00:03:03,390
Looking at the declaration of listener up on line 32

38
00:03:05,190 --> 00:03:07,990
is declared as an OnSaveClicked object.

39
00:03:08,870 --> 00:03:12,860
The only thing that we can be sure of is that it implements our

40
00:03:12,860 --> 00:03:14,750
OnSavedClick interface.

41
00:03:15,350 --> 00:03:18,350
We shouldn't be making any other assumptions about it,

42
00:03:18,550 --> 00:03:21,750
and we certainly shouldn't be assuming that it can be safely cast

43
00:03:21,750 --> 00:03:23,450
to an appcompat activity.

44
00:03:23,750 --> 00:03:26,350
If we use another object as our listener,

45
00:03:26,350 --> 00:03:28,950
we'll fail to set the activities action bar.

46
00:03:31,250 --> 00:03:33,750
It's the activity that will have the action bar,

47
00:03:33,750 --> 00:03:37,650
so we should be using the activity there. I'll change the code.

48
00:03:47,550 --> 00:03:50,250
We're indebted to Sebastian Arbogast,

49
00:03:50,250 --> 00:03:52,250
another instructor here on Udemy

50
00:03:52,450 --> 00:03:55,050
for spotting that potential issue with the code.

51
00:03:55,650 --> 00:03:58,250
We set listener in the OnAttach function,

52
00:03:58,250 --> 00:03:59,500
just below this one.

53
00:04:00,050 --> 00:04:02,050
As our code is at the moment

54
00:04:02,050 --> 00:04:05,410
using listener instead of activity to get the action bar

55
00:04:05,410 --> 00:04:07,010
wouldn't have caused a problem.

56
00:04:07,610 --> 00:04:10,290
But if we change the code to use some other object,

57
00:04:10,290 --> 00:04:11,890
we would have got a problem.

58
00:04:13,490 --> 00:04:15,790
The final change to AddEditFragment

59
00:04:15,790 --> 00:04:19,790
involves the lifecycle functions that are only used for logging.

60
00:04:20,290 --> 00:04:23,290
This is one of the classes I want to make some changes to,

61
00:04:23,590 --> 00:04:25,090
so I won't delete them yet.

62
00:04:25,890 --> 00:04:28,770
But I don't want to release the app with them still in.

63
00:04:29,670 --> 00:04:32,270
This is a good use for a to-do comment.

64
00:04:48,270 --> 00:04:49,970
Before releasing the app,

65
00:04:49,970 --> 00:04:54,330
we can use the to do tab down at the bottom of the android studio window

66
00:04:54,330 --> 00:04:57,130
to review all these notes that we've got for ourselves.

67
00:04:58,130 --> 00:05:00,830
As you can see, when you expand the entries,

68
00:05:00,830 --> 00:05:02,430
we've got a few things to do.

69
00:05:04,230 --> 00:05:06,230
You can double click one of the entries

70
00:05:06,530 --> 00:05:09,130
to go directly to that code in the editor.

71
00:05:16,490 --> 00:05:19,790
Okay. That's AddEditFragment sorted out.

72
00:05:20,290 --> 00:05:22,790
Next, we'll look at the AppDialog class.

73
00:05:28,990 --> 00:05:32,790
I've got three suggestions in here and I'll start at the bottom.

74
00:05:35,640 --> 00:05:39,440
The third one says variable dialog ID

75
00:05:39,440 --> 00:05:40,440
is never used.

76
00:05:40,990 --> 00:05:45,190
In fact, that line should have been commented out along with the line below.

77
00:05:45,390 --> 00:05:49,190
We did that back in section 13 but this line was left in.

78
00:05:49,590 --> 00:05:53,580
We only added it to see how we'd handle the dialogue being cancelled

79
00:05:53,780 --> 00:05:55,280
if we wanted to do that.

80
00:05:55,680 --> 00:05:57,480
We're not handling that in this app

81
00:05:57,480 --> 00:06:00,480
and the entire OnCancel function isn't needed.

82
00:06:00,980 --> 00:06:04,180
So we should delete it. I won't delete it though.

83
00:06:04,980 --> 00:06:07,750
As the app is at the moment, we should delete

84
00:06:08,000 --> 00:06:11,400
it but we're going to add it back in when we make some other changes.

85
00:06:12,000 --> 00:06:15,800
I'll comment the function out instead to save typing again later.

86
00:06:20,200 --> 00:06:24,400
Just to be clear, the only reason that I know that we need this function

87
00:06:24,400 --> 00:06:28,900
is because we've already developed all the code you'll be seeing in the next few videos.

88
00:06:29,500 --> 00:06:32,800
During the review, we did delete the OnCancel function

89
00:06:33,000 --> 00:06:34,600
but then we had to put it back again.

90
00:06:35,260 --> 00:06:37,260
The next two suggestions are similar.

91
00:06:37,660 --> 00:06:40,660
In the end of the OnCreateDialog function,

92
00:06:41,060 --> 00:06:43,660
that's on lines 96 to 98,

93
00:06:45,260 --> 00:06:48,760
though it's usually easier to click the ticks in the right margin to get to them.

94
00:06:49,560 --> 00:06:52,460
We don't use the dialog interface variable

95
00:06:52,460 --> 00:06:55,760
and android studio suggests we can replace it with an underscore.

96
00:06:56,260 --> 00:06:59,160
Doing that allows the compiler to optimize the code

97
00:06:59,560 --> 00:07:01,960
if we tell it that we're not going to use the argument.

98
00:07:02,460 --> 00:07:04,260
We talked about that in an earlier video.

99
00:07:04,760 --> 00:07:09,360
Use the light bulb to rename dialog interface to underscore for both those lines.

100
00:07:19,160 --> 00:07:22,520
After doing each line, you'll get a similar suggestion

101
00:07:22,520 --> 00:07:24,520
that which is also not used,

102
00:07:24,920 --> 00:07:27,220
so rename that one to an underscore as well.

103
00:07:36,580 --> 00:07:39,780
Okay. That's AddEditFragment

104
00:07:39,780 --> 00:07:42,980
and AppDialog finished. In the next video,

105
00:07:42,980 --> 00:07:46,180
we'll review the AppProvider class. I'll see you there.

