G'day everyone. How did you get on? Hopefully, you managed to make the
database changes and retrieve the timing record from the database, if there is one.
For my solution, I'll start by creating the current
Timing Contract class. All these Contract classes are very similar, and I'll use
the TimingsContract class as a template. Right click TimingsContract.kt
and choose Copy. Then right click the package name and choose Paste.
The new name will be CurrentTimingContract.
We'll need to change the table name and I'll update the comment after it.
Next, we need to fix the column names. I'm going to shorten them in this contract,
and you can decide for yourself, whether the longer names make the code more readable.
The rows in this view don't have their own ID, so the ID constant can be deleted.
We have just four columns. The final change is to delete the two functions.
As I said, the rows in this view don't have their own ID and these two functions won't be used.
These Contract classes are, generally, very similar,
but do think about what you're doing when you copy one. If a table, or a view in this case,
doesn't have a unique ID for each row, then there's no point including support
in your database classes, to access the rows by ID. Now that we've defined the
field names, we can create the view. We do that in AppDatabase.
We have to change the database version first. This will become version 3. I'll call the function
to create the view, addCurrentTiming View and put it after the addTimings
Table function. I'm going to paste the function in. It's quite tedious, replacing
all the table and column names with the appropriate constants, and it's very easy
to make mistakes. You can find the function in resources for this video
in the file add current timing view dot txt.
I suggest that you start with the correct sequel, and I've added that as a
comment in the code. That makes it easier to replace the individual names with
references to the contract constant values. Alright, we need to call the
function from onCreate,
and also in onUpgrade, when upgrading from versions one or two of the database.
Note that we have to call the function twice. The user may be upgrading from
version one, or they may be upgrading from version 2. Okay that's the App
DatabaseClass changed. This video is getting a bit long, so we'll add the
remaining code in the next video. I'll see you there.