1
00:00:05,300 --> 00:00:06,290
Welcome back.

2
00:00:06,290 --> 00:00:09,490
I'm going to fly through these changes with very little explanation

3
00:00:09,490 --> 00:00:11,890
because it's all code we've written a few times now.

4
00:00:12,790 --> 00:00:16,790
Create a new Kotlin file called ParametersContract,

5
00:00:30,790 --> 00:00:33,790
and paste in the code from the resources in this video.

6
00:00:37,290 --> 00:00:40,190
Remember to change the package name on the first line

7
00:00:40,490 --> 00:00:42,390
to match the package name for your app.

8
00:00:42,890 --> 00:00:45,790
If you want to practice creating a contract class again,

9
00:00:46,090 --> 00:00:50,190
then you could copy the TimingsContract class and modify that.

10
00:00:51,290 --> 00:00:55,290
The column names are the two columns that we used in the previous video.

11
00:00:55,890 --> 00:00:59,250
_ID and Value with a capital V.

12
00:01:00,250 --> 00:01:03,850
Next, we need to change the AppDatabase class.

13
00:01:07,850 --> 00:01:11,350
I'll start by updating the version number on line 20.

14
00:01:14,350 --> 00:01:18,650
I'll use a function to perform the update from version 4 to version 5.

15
00:01:18,850 --> 00:01:21,550
I'll call it parameterize view

16
00:01:21,910 --> 00:01:25,310
and add it at the end of the class before the companion object.

17
00:01:33,610 --> 00:01:37,610
Once again, you can find the code in the resources for this video.

18
00:01:38,610 --> 00:01:42,370
Note that we drop the existing view before creating it again.

19
00:01:42,370 --> 00:01:46,770
Line 159 shows how we're using a subquery

20
00:01:46,770 --> 00:01:49,770
to provide the value that the duration will be compared to.

21
00:01:50,370 --> 00:01:53,970
I'm not entirely happy about using the literal value 1

22
00:01:53,970 --> 00:01:55,470
at the end of line

23
00:01:56,770 --> 00:01:58,570
177.

24
00:01:58,930 --> 00:02:01,930
I don't like remembering what number something is.

25
00:02:02,330 --> 00:02:03,330
In this case,

26
00:02:03,630 --> 00:02:07,630
that's the number of the _id column for our parameter

27
00:02:07,630 --> 00:02:09,130
in the parameters table.

28
00:02:10,380 --> 00:02:12,630
If we add more parameters in the future,

29
00:02:12,630 --> 00:02:15,530
then we'd have to remember which id each one had.

30
00:02:16,330 --> 00:02:19,830
That's a good way to create work and to introduce bugs.

31
00:02:21,030 --> 00:02:24,430
If you looked at the ParametersContract class from the resources,

32
00:02:24,830 --> 00:02:27,630
you'll have noticed an extra constant in there.

33
00:02:28,230 --> 00:02:29,730
I'll switch to that file.

34
00:02:32,930 --> 00:02:35,930
I've defined a constant on line 13.

35
00:02:36,430 --> 00:02:38,130
We can use that in our code

36
00:02:38,130 --> 00:02:41,430
rather than having to remember the id that we've used in the table.

37
00:02:42,420 --> 00:02:45,820
Back in app database, I'll use that constant

38
00:02:45,820 --> 00:02:47,920
instead of the literal value 1.

39
00:03:07,420 --> 00:03:10,020
We'll use that constant in our code too.

40
00:03:10,680 --> 00:03:12,480
It'll make the code more readable

41
00:03:12,480 --> 00:03:15,980
because we'll see straight away which parameter we're referring to.

42
00:03:16,640 --> 00:03:18,140
Before I move on,

43
00:03:18,140 --> 00:03:22,540
notice that we add a row to the parameters table at the end of this function.

44
00:03:25,140 --> 00:03:28,440
That's lines 183 to 185.

45
00:03:29,640 --> 00:03:33,300
The view will fail if there's no value in our parameters table.

46
00:03:33,900 --> 00:03:36,400
So our function makes sure the row exists.

47
00:03:37,280 --> 00:03:41,280
Because the only reason for this table is to provide parameters to a view,

48
00:03:41,640 --> 00:03:44,640
you should generally do this if you use this technique.

49
00:03:45,740 --> 00:03:49,740
You'll either be updating or creating a view when you use a parameter like this,

50
00:03:50,040 --> 00:03:52,940
so it makes sense to create the parameters row

51
00:03:52,940 --> 00:03:54,440
when upgrading the database.

52
00:03:55,640 --> 00:03:57,840
Okay. We've written the function,

53
00:03:57,840 --> 00:04:02,040
now we need to call it in OnCreate and OnUpgrade.

54
00:04:39,540 --> 00:04:42,840
So there's all the old ones covered and of course we've got the new case

55
00:04:42,840 --> 00:04:45,140
we're upgrading from version 4.

56
00:04:55,540 --> 00:04:58,530
The final changes to support the new table

57
00:04:58,530 --> 00:05:00,830
are in the AppProvider class.

58
00:05:10,430 --> 00:05:14,630
All the changes are just copy and paste of the timings entries,

59
00:05:14,930 --> 00:05:18,330
changing the name of the contract class and the column names.

60
00:05:19,230 --> 00:05:23,480
I won't do that, I'll paste the code in to save time in the video.

61
00:05:37,180 --> 00:05:39,380
Next, the URI matcher.

62
00:05:50,980 --> 00:05:53,480
I'll also add them to the GetType function.

63
00:06:27,480 --> 00:06:31,140
All right. We finished by providing support for querying

64
00:06:31,140 --> 00:06:33,140
and updating our parameters table.

65
00:06:33,940 --> 00:06:36,640
I'm not going to allow rows to be deleted.

66
00:06:37,140 --> 00:06:40,500
Deleting the parameter from the table would break the database view.

67
00:06:41,300 --> 00:06:44,300
I'm also not going to allow new parameters to be inserted.

68
00:06:44,960 --> 00:06:48,460
As I mentioned, the parameters will be used by a view

69
00:06:48,460 --> 00:06:51,160
and we should add a row for any new parameters

70
00:06:51,160 --> 00:06:53,560
at the same time as we upgrade the database.

71
00:06:54,220 --> 00:06:58,220
The code in on query is the same as for the other tables.

72
00:07:05,620 --> 00:07:07,720
And the code for the update function

73
00:07:07,720 --> 00:07:11,520
is also just a copy and paste, changing the contract name.

74
00:07:27,520 --> 00:07:30,520
Note that we only allow an update by ID,

75
00:07:31,070 --> 00:07:33,430
we don't allow an update of multiple rows.

76
00:07:33,930 --> 00:07:35,930
That would change several parameters

77
00:07:35,930 --> 00:07:39,730
and is probably an indication that our code is using the wrong table.

78
00:07:40,930 --> 00:07:44,430
The only use case I can think of for updating multiple rows here

79
00:07:44,730 --> 00:07:47,430
is if you wanted to 0 all the parameters.

80
00:07:48,310 --> 00:07:52,110
I'm quite happy to force the code to set each parameter individually

81
00:07:52,110 --> 00:07:55,710
rather than implement something that probably won't be used.

82
00:07:56,510 --> 00:07:59,310
There was nothing earth-shattering in any of these changes,

83
00:07:59,610 --> 00:08:01,110
they're just more of the same.

84
00:08:01,990 --> 00:08:04,350
Adding new tables to a content provider

85
00:08:04,350 --> 00:08:06,900
is generally just boilerplate code

86
00:08:06,900 --> 00:08:10,800
with a bit of thought about what operations you're going to allow on the new table.

87
00:08:11,350 --> 00:08:14,680
In our case, we only allow the table to be queried

88
00:08:14,680 --> 00:08:16,680
or a single row to be updated.

89
00:08:17,340 --> 00:08:19,940
We'll change our view models to use this parameter

90
00:08:20,380 --> 00:08:22,580
in the next video. I'll see you there.

