We've got a few more suggestions to sort out before we look at improving our app.
In the CursorRecyclerViewAdapter class,
there's a suggestion that we can inline count
on line 88.
We can use the light bulb to do that.
We've got some unnecessary duplication in the
OnBindViewHolder function.
In the block of code on lines 76 to 82,
we have to repeat cursor on each line.
Kotlin provides the with statement
to let us write that more concisely.
That's a lot tidier.
If your performing repeated property access isn't the same object,
then "with" avoids having to repeat the object name each time.
In durations report, we've got the same warning that we've already seen.
ViewModelProviders has been deprecated.
The import on line 13 should now be provider.
There are two variables that can be inlined.
Click on the ticks that are light gray
in the right margin and use the light bulb to change the code.
And there's an unnecessary cast on line 45.
Finally, we left the commented out
onDestroy function at the end of the class.
This can also be deleted.
These have all been simple changes.
The warning in the DurationsViewModel is a bit more serious.
It's saying that android.preference
.preferencemanager has been deprecated.
We've got those warnings on line 9
and on line 43.
You may not have this.
If you created your project using androidx and didn't convert it to androidx,
ideally, the conversion process would have fixed it to import from androidx.
Before we fix this, we have to add the library to the dependencies
section in the modules build.gradle file.
Sync now,
and we can change the import in DurationsViewModel.
We're also potentially leaking a resource.
Have a look at the LoadData function on line 182.
In there, we create a cursor
and store it in the database cursor MutableLiveData object.
The durations activity does close this cursor
when we add new data,
but the cursor doesn't get closed otherwise.
We really should close it when the ViewModel is no longer needed.
The place to do that is in OnCleared, right at the end of the code.
When we're finished with the ViewModel,
we close the cursor before it's destroyed.
I'm being a bit paranoid here.
The cursor is owned by this class
and will be destroyed when the instance of this class is destroyed.
The only thing holding a reference to the cursor is the adapter.
That's created by the durations report activity
and will be destroyed when durations report is destroyed.
A ViewModel outlives its activities and fragments,
that's one of the reasons we use them.
So there should be nothing that can be holding a reference to this cursor
when the ViewModel gets destroyed.
But it's always a good idea to be aware of the life cycles
of the resources you create and tidy up after yourself.
Next, open the SettingsDialog file.
Our SettingsDialog class is also using the deprecated preference manager.
Once again, change the import to come from androidx.
The other change in here also probably happened when I converted the project.
It was mentioned in the video at the time but this one was missed.
We've got a redundant qualifier on line 32
that can be deleted.
Our SettingsDialog is an AppcompatDialogFragment
and there's no need to qualify the reference to style_normal.
And finally, in SettingsDialog,
delete the OnDestroy function at the end of the class.
It's only there so that we could check the dialogue was being destroyed in the logcat.
There's a small change to make in the task file.
We've got an empty class body in the task class
and that can be deleted.
The last class I'll tidy up in this video is the TaskTimerViewModel.
That's also using the deprecated preference manager.
Change the import to fix that.
We can also lift an assignment out of the if
in the TimeTask function.
You can use a light bulb to do that.
Finally, we should close the cursor.
We did that in the DurationsViewModel class,
and we should be consistent and make that same change
in the TaskTimerViewModel.
In the next video, we'll tidy up main activity
and a couple of the contract classes. I'll see you there.