1 00:00:03,920 --> 00:00:06,960 Alright so the Tasks Contract Class is 2 00:00:06,960 --> 00:00:09,120 a topic for this video. Now when we used 3 00:00:09,120 --> 00:00:11,400 the Content Provider in the previous app, 4 00:00:11,400 --> 00:00:13,679 we didn't know what the real columns in 5 00:00:13,679 --> 00:00:15,570 that database were called. In fact, we 6 00:00:15,570 --> 00:00:17,100 didn't even know that the data was 7 00:00:17,100 --> 00:00:19,170 coming from a database. The Content 8 00:00:19,170 --> 00:00:21,660 Provider abstracts the underlying data, 9 00:00:21,660 --> 00:00:24,689 and prevents a standard interface that 10 00:00:24,689 --> 00:00:27,599 we used to access the data we want. Now 11 00:00:27,599 --> 00:00:29,789 if you remember we use constants to 12 00:00:29,789 --> 00:00:32,098 refer to the actual fields. So our 13 00:00:32,098 --> 00:00:34,110 Content Providers are going to allow the 14 00:00:34,110 --> 00:00:36,750 same thing, so we'll start by creating a 15 00:00:36,750 --> 00:00:39,450 Contract Class to define the names that 16 00:00:39,450 --> 00:00:41,910 are used to access our data. Now Google's 17 00:00:41,910 --> 00:00:44,550 Contacts Contract class uses an 18 00:00:44,550 --> 00:00:46,440 interface to provide the column name 19 00:00:46,440 --> 00:00:48,989 constants. You can find a lot of argument 20 00:00:48,989 --> 00:00:50,430 on-line about whether using an interface 21 00:00:50,430 --> 00:00:53,219 just to provide constant values is a 22 00:00:53,219 --> 00:00:55,530 good thing or not. In this case, I think 23 00:00:55,530 --> 00:00:57,660 using an interface in a Contract class 24 00:00:57,660 --> 00:01:00,210 is perfectly valid. The fact that we're 25 00:01:00,210 --> 00:01:02,399 talking about a Contract here, is an 26 00:01:02,399 --> 00:01:03,989 indication that an interface is a 27 00:01:03,989 --> 00:01:06,840 suitable way to implement this. An 28 00:01:06,840 --> 00:01:09,659 interface is a Contract and guarantees 29 00:01:09,659 --> 00:01:11,340 that it'll always provide the fields 30 00:01:11,340 --> 00:01:14,430 and methods that it publishes. However, 31 00:01:14,430 --> 00:01:17,939 Kotlin provides another way - Objects. So 32 00:01:17,939 --> 00:01:19,380 you can find more information on Object 33 00:01:19,380 --> 00:01:21,180 declarations in the Kotlin documentation. 34 00:01:21,180 --> 00:01:26,210 I'm just going to briefly bring that up - 35 00:01:26,210 --> 00:01:28,530 that's a good resource link to check out. 36 00:01:28,530 --> 00:01:30,960 But one important use is to implement 37 00:01:30,960 --> 00:01:33,570 the singleton pattern. If you only ever 38 00:01:33,570 --> 00:01:35,009 want to have a single instance of 39 00:01:35,009 --> 00:01:37,619 something, then an object in Kotlin may 40 00:01:37,619 --> 00:01:38,850 be a good way to do that. 41 00:01:38,850 --> 00:01:42,180 It does however have one limitation; you 42 00:01:42,180 --> 00:01:44,369 can't pass arguments to its constructor, 43 00:01:44,369 --> 00:01:46,200 but actually we don't need to do that 44 00:01:46,200 --> 00:01:48,720 here. If you put an object inside a class 45 00:01:48,720 --> 00:01:51,630 it becomes a companion object, which 46 00:01:51,630 --> 00:01:53,640 we've looked at earlier. Now that's also 47 00:01:53,640 --> 00:01:55,950 documented on this page, just after this 48 00:01:55,950 --> 00:01:58,530 section - you can see the discussion there 49 00:01:58,530 --> 00:02:00,899 about Companion Objects. Okay so I'm 50 00:02:00,899 --> 00:02:02,729 going to use objects for the Contract 51 00:02:02,729 --> 00:02:04,530 classes for our app. So I'm going to go 52 00:02:04,530 --> 00:02:06,930 back to IntelliJ, and what I'm going to 53 00:02:06,930 --> 00:02:10,530 do is create a new Kotlin class. So I'm 54 00:02:10,530 --> 00:02:12,540 going to go to our package, right click 55 00:02:12,540 --> 00:02:13,860 New 56 00:02:13,860 --> 00:02:15,990 Kotlin File/Class, and we're going to call this 57 00:02:15,990 --> 00:02:22,620 one TasksContract. Now creating an 58 00:02:22,620 --> 00:02:24,720 object is very similar to creating a 59 00:02:24,720 --> 00:02:26,640 class - we just used the word object 60 00:02:26,640 --> 00:02:29,450 instead. So I'm going to type object 61 00:02:29,450 --> 00:02:33,030 TasksContract, then left and right 62 00:02:33,030 --> 00:02:35,400 curly braces. Now we'll start with a 63 00:02:35,400 --> 00:02:38,430 string to store the table name. We'll be 64 00:02:38,430 --> 00:02:39,870 referring to our table in a few 65 00:02:39,870 --> 00:02:42,090 different places, so this is a good place 66 00:02:42,090 --> 00:02:44,640 to define the name of the table. Now 67 00:02:44,640 --> 00:02:47,130 remember that external apps don't have 68 00:02:47,130 --> 00:02:48,660 to know anything about how we're 69 00:02:48,660 --> 00:02:50,550 implementing the underlying storage, 70 00:02:50,550 --> 00:02:53,070 which means the name of our table should 71 00:02:53,070 --> 00:02:55,620 only be visible inside our package. So 72 00:02:55,620 --> 00:02:57,120 I'm going to use the internal access 73 00:02:57,120 --> 00:03:02,670 modifier to achieve that. So we're going to 74 00:03:02,670 --> 00:03:06,690 type internal const Val TABLE 75 00:03:06,690 --> 00:03:09,930 underscore NAME equals Tasks in double quote, 76 00:03:09,930 --> 00:03:13,950 double quotes rather. Now note that we 77 00:03:13,950 --> 00:03:16,050 can use the const keyword inside an 78 00:03:16,050 --> 00:03:17,820 object - something we couldn't do in a 79 00:03:17,820 --> 00:03:20,190 class. Alright so that's the table name. 80 00:03:20,190 --> 00:03:22,140 Next we need to figure out our column 81 00:03:22,140 --> 00:03:24,180 names. Now what we'll do is we'll put 82 00:03:24,180 --> 00:03:26,549 them inside another object, in the Tasks 83 00:03:26,549 --> 00:03:29,250 Contract object. Now this is effectively 84 00:03:29,250 --> 00:03:32,250 the same as a companion object but we 85 00:03:32,250 --> 00:03:33,989 don't need the companion. Now I'm going to 86 00:03:33,989 --> 00:03:36,000 type it anyway, and we'll see what 87 00:03:36,000 --> 00:03:37,530 Android Studio's link checker has to 88 00:03:37,530 --> 00:03:40,769 say about it, and I'll just put a comment 89 00:03:40,769 --> 00:03:47,780 down here, "Tasks fields". If we type companion 90 00:03:47,780 --> 00:03:51,480 object Columns left and right curly 91 00:03:51,480 --> 00:03:53,370 braces, we get an error there, and if we 92 00:03:53,370 --> 00:03:55,799 have a look at the error: "Modifier 93 00:03:55,799 --> 00:03:57,450 companion is not applicable inside 94 00:03:57,450 --> 00:04:00,120 object". And so we can delete the word 95 00:04:00,120 --> 00:04:02,940 companion near the keyword and just 96 00:04:02,940 --> 00:04:05,130 declare an object, and I'll add the 97 00:04:05,130 --> 00:04:07,320 constants for the field names inside 98 00:04:07,320 --> 00:04:13,920 this object. We'll start with const val ID 99 00:04:13,920 --> 00:04:15,989 in uppercase equals double quotes 100 00:04:15,989 --> 00:04:18,959 _id. Then on the next line, 101 00:04:18,959 --> 00:04:21,720 const val, in uppercase TASK underscore 102 00:04:21,720 --> 00:04:25,800 NAME equals name double quotes. Next line 103 00:04:25,800 --> 00:04:27,420 const val 104 00:04:27,420 --> 00:04:28,890 Tech, TASK rather underscore 105 00:04:28,890 --> 00:04:33,000 DESCRIPTION, in uppercase again, equals 106 00:04:33,000 --> 00:04:35,520 and in double quotes Description. And 107 00:04:35,520 --> 00:04:39,570 lastly, const val TASK_SORT 108 00:04:39,570 --> 00:04:42,650 underscore ORDER is equal to SortOrder, 109 00:04:42,650 --> 00:04:45,570 capital S, capital O there, in double 110 00:04:45,570 --> 00:04:47,580 quotes. And obviously you can see, I've 111 00:04:47,580 --> 00:04:50,400 capitalized the Name, Description and 112 00:04:50,400 --> 00:04:52,920 SortOrder on lines 14 through 16. 113 00:04:52,920 --> 00:04:55,890 So our columns object behaves almost 114 00:04:55,890 --> 00:04:58,560 exactly the same as if it was defined in 115 00:04:58,560 --> 00:05:01,290 its own column.kt file. The only 116 00:05:01,290 --> 00:05:03,210 difference is that we can't refer to it as 117 00:05:03,210 --> 00:05:06,600 directly as columns. We have to use tasks 118 00:05:06,600 --> 00:05:09,720 Contract.columns. Now one advantage of 119 00:05:09,720 --> 00:05:11,670 this, is that we can have several columns 120 00:05:11,670 --> 00:05:13,680 objects. Now we're going to need to 121 00:05:13,680 --> 00:05:15,750 define the columns for our Timings table, 122 00:05:15,750 --> 00:05:18,330 and that object would be referred to as 123 00:05:18,330 --> 00:05:22,050 timingsContract.columns. Alright, so 124 00:05:22,050 --> 00:05:24,030 this can, this columns object is 125 00:05:24,030 --> 00:05:25,890 just to provide the constants we'll be 126 00:05:25,890 --> 00:05:28,500 using to refer to the column name in our 127 00:05:28,500 --> 00:05:30,900 Tasks table. So we're going to add more 128 00:05:30,900 --> 00:05:32,940 code to this TasksContract object later, 129 00:05:32,940 --> 00:05:35,070 to provide everything that we'll need 130 00:05:35,070 --> 00:05:37,350 for our Content Provider, but that's 131 00:05:37,350 --> 00:05:39,390 enough for now, except there's one change 132 00:05:39,390 --> 00:05:41,370 that I want to make. And I want to make a 133 00:05:41,370 --> 00:05:43,710 change to our code so that we can have a 134 00:05:43,710 --> 00:05:46,470 look at what Base Columns is. So if we 135 00:05:46,470 --> 00:05:49,620 come down here and change the ID, I'm 136 00:05:49,620 --> 00:05:50,820 actually going to add the word Base 137 00:05:50,820 --> 00:05:56,390 Columns, BaseColumns._ID and 138 00:05:56,390 --> 00:05:58,440 I'm going to delete out the double 139 00:05:58,440 --> 00:05:59,790 quotes _id that we had 140 00:05:59,790 --> 00:06:04,710 there. So if we click on this now we can 141 00:06:04,710 --> 00:06:06,900 check out the source code, and you can 142 00:06:06,900 --> 00:06:08,160 see there that BaseColumns is an 143 00:06:08,160 --> 00:06:10,800 interface that defines two constants: 144 00:06:10,800 --> 00:06:14,280 underscore ID and underscore COUNT. Now I 145 00:06:14,280 --> 00:06:15,900 mentioned that Android makes use of a 146 00:06:15,900 --> 00:06:17,760 primary key called underscore ID, and 147 00:06:17,760 --> 00:06:20,700 this is where that name's defined. Now we 148 00:06:20,700 --> 00:06:22,350 could have left it hard-coded in our 149 00:06:22,350 --> 00:06:24,750 code, but if Google provide a constant to 150 00:06:24,750 --> 00:06:26,610 use, then we really should use that 151 00:06:26,610 --> 00:06:29,310 constant. So that way, if they change 152 00:06:29,310 --> 00:06:31,470 things in future our code will still 153 00:06:31,470 --> 00:06:33,810 work and won't break. Now I can't really 154 00:06:33,810 --> 00:06:36,090 imagine any reason why they might decide 155 00:06:36,090 --> 00:06:38,400 to rename this field - it could break too 156 00:06:38,400 --> 00:06:40,650 much existing code for one thing. So if 157 00:06:40,650 --> 00:06:41,340 you prefer to 158 00:06:41,340 --> 00:06:43,860 used the string literal underscore ID - I'll 159 00:06:43,860 --> 00:06:46,710 just close this down - in your code instead of 160 00:06:46,710 --> 00:06:48,780 BaseColumns._ID that 161 00:06:48,780 --> 00:06:51,150 we've got here, that's probably fine. But 162 00:06:51,150 --> 00:06:52,889 I've done it this way, because you'll see 163 00:06:52,889 --> 00:06:55,350 this BaseColumns interface being used 164 00:06:55,350 --> 00:06:56,790 like this in a lot of code on the 165 00:06:56,790 --> 00:06:57,300 Internet 166 00:06:57,300 --> 00:06:59,699 Alright so that's enough to be getting 167 00:06:59,699 --> 00:07:01,320 on with. In the next video we'll start 168 00:07:01,320 --> 00:07:03,570 looking at creating the database class. 169 00:07:03,570 --> 00:07:07,729 So I'll see you in the next video.