1
00:00:05,500 --> 00:00:09,650
We've got a few more suggestions to sort out before we look at improving our app.

2
00:00:10,640 --> 00:00:13,640
In the CursorRecyclerViewAdapter class,

3
00:00:16,640 --> 00:00:19,940
there's a suggestion that we can inline count

4
00:00:20,300 --> 00:00:21,960
on line 88.

5
00:00:30,760 --> 00:00:32,360
We can use the light bulb to do that.

6
00:00:32,720 --> 00:00:35,120
We've got some unnecessary duplication in the

7
00:00:35,120 --> 00:00:37,120
OnBindViewHolder function.

8
00:00:39,720 --> 00:00:43,710
In the block of code on lines 76 to 82,

9
00:00:43,710 --> 00:00:46,370
we have to repeat cursor on each line.

10
00:00:47,170 --> 00:00:49,770
Kotlin provides the with statement

11
00:00:49,770 --> 00:00:51,770
to let us write that more concisely.

12
00:01:28,070 --> 00:01:29,270
That's a lot tidier.

13
00:01:30,070 --> 00:01:33,570
If your performing repeated property access isn't the same object,

14
00:01:33,870 --> 00:01:37,470
then "with" avoids having to repeat the object name each time.

15
00:01:38,370 --> 00:01:42,030
In durations report, we've got the same warning that we've already seen.

16
00:01:46,930 --> 00:01:50,130
ViewModelProviders has been deprecated.

17
00:01:51,430 --> 00:01:55,790
The import on line 13 should now be provider.

18
00:02:00,290 --> 00:02:03,190
There are two variables that can be inlined.

19
00:02:04,190 --> 00:02:06,290
Click on the ticks that are light gray

20
00:02:06,290 --> 00:02:09,289
in the right margin and use the light bulb to change the code.

21
00:02:20,690 --> 00:02:24,290
And there's an unnecessary cast on line 45.

22
00:02:34,890 --> 00:02:37,390
Finally, we left the commented out

23
00:02:37,390 --> 00:02:39,890
onDestroy function at the end of the class.

24
00:02:40,390 --> 00:02:41,690
This can also be deleted.

25
00:02:48,590 --> 00:02:50,390
These have all been simple changes.

26
00:02:50,940 --> 00:02:54,600
The warning in the DurationsViewModel is a bit more serious.

27
00:03:04,400 --> 00:03:07,400
It's saying that android.preference

28
00:03:07,400 --> 00:03:10,400
.preferencemanager has been deprecated.

29
00:03:10,900 --> 00:03:12,800
We've got those warnings on line 9

30
00:03:14,360 --> 00:03:15,960
and on line 43.

31
00:03:16,760 --> 00:03:18,010
You may not have this.

32
00:03:18,010 --> 00:03:22,410
If you created your project using androidx and didn't convert it to androidx, 

33
00:03:23,010 --> 00:03:27,310
ideally, the conversion process would have fixed it to import from androidx.

34
00:03:27,710 --> 00:03:31,710
Before we fix this, we have to add the library to the dependencies

35
00:03:31,710 --> 00:03:35,010
section in the modules build.gradle file.

36
00:04:03,370 --> 00:04:04,470
Sync now,

37
00:04:06,350 --> 00:04:09,150
and we can change the import in DurationsViewModel.

38
00:04:22,150 --> 00:04:24,650
We're also potentially leaking a resource.

39
00:04:25,550 --> 00:04:29,810
Have a look at the LoadData function on line 182.

40
00:04:32,310 --> 00:04:34,510
In there, we create a cursor

41
00:04:34,510 --> 00:04:38,810
and store it in the database cursor MutableLiveData object.

42
00:04:39,060 --> 00:04:42,360
The durations activity does close this cursor

43
00:04:42,360 --> 00:04:43,860
when we add new data,

44
00:04:44,460 --> 00:04:46,460
but the cursor doesn't get closed otherwise.

45
00:04:47,210 --> 00:04:51,110
We really should close it when the ViewModel is no longer needed.

46
00:04:51,810 --> 00:04:56,110
The place to do that is in OnCleared, right at the end of the code.

47
00:05:07,410 --> 00:05:09,210
When we're finished with the ViewModel,

48
00:05:09,210 --> 00:05:11,810
we close the cursor before it's destroyed.

49
00:05:12,310 --> 00:05:14,210
I'm being a bit paranoid here.

50
00:05:14,210 --> 00:05:16,410
The cursor is owned by this class

51
00:05:16,410 --> 00:05:19,730
and will be destroyed when the instance of this class is destroyed.

52
00:05:20,130 --> 00:05:23,730
The only thing holding a reference to the cursor is the adapter.

53
00:05:24,430 --> 00:05:27,430
That's created by the durations report activity

54
00:05:27,430 --> 00:05:30,680
and will be destroyed when durations report is destroyed.

55
00:05:31,230 --> 00:05:34,430
A ViewModel outlives its activities and fragments,

56
00:05:34,630 --> 00:05:36,630
that's one of the reasons we use them.

57
00:05:36,930 --> 00:05:40,730
So there should be nothing that can be holding a reference to this cursor

58
00:05:41,030 --> 00:05:43,030
when the ViewModel gets destroyed.

59
00:05:43,430 --> 00:05:46,530
But it's always a good idea to be aware of the life cycles

60
00:05:46,530 --> 00:05:50,030
of the resources you create and tidy up after yourself.

61
00:05:51,230 --> 00:05:53,830
Next, open the SettingsDialog file.

62
00:05:58,330 --> 00:06:02,730
Our SettingsDialog class is also using the deprecated preference manager.

63
00:06:03,530 --> 00:06:07,030
Once again, change the import to come from androidx.

64
00:06:12,390 --> 00:06:16,890
The other change in here also probably happened when I converted the project.

65
00:06:17,390 --> 00:06:20,940
It was mentioned in the video at the time but this one was missed.

66
00:06:26,740 --> 00:06:30,400
We've got a redundant qualifier on line 32

67
00:06:30,400 --> 00:06:31,600
that can be deleted.

68
00:06:34,400 --> 00:06:38,000
Our SettingsDialog is an AppcompatDialogFragment

69
00:06:38,400 --> 00:06:42,000
and there's no need to qualify the reference to style_normal.

70
00:06:42,800 --> 00:06:45,600
And finally, in SettingsDialog,

71
00:06:45,600 --> 00:06:48,600
delete the OnDestroy function at the end of the class.

72
00:06:49,300 --> 00:06:53,100
It's only there so that we could check the dialogue was being destroyed in the logcat.

73
00:06:58,460 --> 00:07:01,260
There's a small change to make in the task file.

74
00:07:03,360 --> 00:07:06,560
We've got an empty class body in the task class

75
00:07:06,860 --> 00:07:08,220
and that can be deleted.

76
00:07:11,520 --> 00:07:16,420
The last class I'll tidy up in this video is the TaskTimerViewModel.

77
00:07:20,520 --> 00:07:23,320
That's also using the deprecated preference manager.

78
00:07:24,980 --> 00:07:26,780
Change the import to fix that.

79
00:07:28,580 --> 00:07:31,380
We can also lift an assignment out of the if

80
00:07:31,740 --> 00:07:33,340
in the TimeTask function.

81
00:07:47,440 --> 00:07:49,100
You can use a light bulb to do that.

82
00:07:50,200 --> 00:07:52,500
Finally, we should close the cursor.

83
00:07:53,160 --> 00:07:56,160
We did that in the DurationsViewModel class,

84
00:07:56,160 --> 00:07:59,660
and we should be consistent and make that same change

85
00:07:59,660 --> 00:08:01,360
in the TaskTimerViewModel.

86
00:08:15,860 --> 00:08:19,160
In the next video, we'll tidy up main activity

87
00:08:19,160 --> 00:08:22,160
and a couple of the contract classes. I'll see you there.

