Welcome back. All right let's review our code
and see if there's anything we can improve or anything we shouldn't be doing.
This is also a good chance to check any suggestions or warnings
that android studio's lint checker is giving us.
The first one's in AddEditFragment. So open that file up.
I'll close the project pane to make more room on the screen.
A new version of the androidx lifecycle extensions library was released
towards the end of January 2020.
You'll have seen me change my version of that library in the last video.
That causes a small problem
because Google have deprecated the ViewModelProviders class.
You'll have a warning about that on line 12
and then again when we use it on line 33.
It's no longer necessary to use it.
We can call the ViewModelProvider constructor directly.
If your Kotlin classes aren't importing automatically,
go to settings then editor general auto import.
The recent versions of android studio now have a separate section
where you can specify the auto import behavior for Kotlin.
Tick both these boxes
if you want android studio to auto import into your Kotlin code.
We need to make the same change in our other three activity and fragment classes,
but we'll do that when we come to them.
There's a suggestion on line 58,
in the OnViewCreated function.
We should use Kotlin's ToString function
rather than calling the function in the integer class.
Click the ToString function in the code
and use the lightbulb to accept the suggestion.
The next change is a bit more serious.
Have a look in on activity created
and see if you can spot what it is.
The function works but the code is doing something that it really shouldn't be doing.
What we've done is assumed that the listener
on line 106 will always be our activity.
Most of the time, it probably will be but it doesn't have to be.
Looking at the declaration of listener up on line 32
is declared as an OnSaveClicked object.
The only thing that we can be sure of is that it implements our
OnSavedClick interface.
We shouldn't be making any other assumptions about it,
and we certainly shouldn't be assuming that it can be safely cast
to an appcompat activity.
If we use another object as our listener,
we'll fail to set the activities action bar.
It's the activity that will have the action bar,
so we should be using the activity there. I'll change the code.
We're indebted to Sebastian Arbogast,
another instructor here on Udemy
for spotting that potential issue with the code.
We set listener in the OnAttach function,
just below this one.
As our code is at the moment
using listener instead of activity to get the action bar
wouldn't have caused a problem.
But if we change the code to use some other object,
we would have got a problem.
The final change to AddEditFragment
involves the lifecycle functions that are only used for logging.
This is one of the classes I want to make some changes to,
so I won't delete them yet.
But I don't want to release the app with them still in.
This is a good use for a to-do comment.
Before releasing the app,
we can use the to do tab down at the bottom of the android studio window
to review all these notes that we've got for ourselves.
As you can see, when you expand the entries,
we've got a few things to do.
You can double click one of the entries
to go directly to that code in the editor.
Okay. That's AddEditFragment sorted out.
Next, we'll look at the AppDialog class.
I've got three suggestions in here and I'll start at the bottom.
The third one says variable dialog ID
is never used.
In fact, that line should have been commented out along with the line below.
We did that back in section 13 but this line was left in.
We only added it to see how we'd handle the dialogue being cancelled
if we wanted to do that.
We're not handling that in this app
and the entire OnCancel function isn't needed.
So we should delete it. I won't delete it though.
As the app is at the moment, we should delete
it but we're going to add it back in when we make some other changes.
I'll comment the function out instead to save typing again later.
Just to be clear, the only reason that I know that we need this function
is because we've already developed all the code you'll be seeing in the next few videos.
During the review, we did delete the OnCancel function
but then we had to put it back again.
The next two suggestions are similar.
In the end of the OnCreateDialog function,
that's on lines 96 to 98,
though it's usually easier to click the ticks in the right margin to get to them.
We don't use the dialog interface variable
and android studio suggests we can replace it with an underscore.
Doing that allows the compiler to optimize the code
if we tell it that we're not going to use the argument.
We talked about that in an earlier video.
Use the light bulb to rename dialog interface to underscore for both those lines.
After doing each line, you'll get a similar suggestion
that which is also not used,
so rename that one to an underscore as well.
Okay. That's AddEditFragment
and AppDialog finished. In the next video,
we'll review the AppProvider class. I'll see you there.