G'day everyone, welcome back. The title of this video is Storing the current Timing
record, but I'm going to start by asking Do we need to store the current timing
record? To phrase that in a different way, Do we already have the current timing
record stored somewhere? and the answer is yes, it's stored in the database.
If everything is working correctly, there should only be one row in the Timings
table that has a duration of zero. It's worth considering things like this,
before jumping in and storing the currentTiming object in our ViewModel.
If we were accessing a remote database over the internet, then I would probably
store the timing in a SharedPreference. If you want to do that, we'll be looking
at SharedPreferences again, soon. In this case, we can retrieve all the information
we need from the database, with a query. I'll do that in the terminal pane.
It's quite a long query, because we have to join the Timings and the Tasks table,
in order to get the Task name. I'll paste the command into the sqlite3
command line. First, I need to run sqlite3 with our TaskTimer database.
Make sure you've run the adb shell and change to the right directory first.
We get two rows returned, and the first one is the Timing record that we want.
Normally, there would be, at most, one row returned, but I messed up the database by
stopping the app in the previous video. That's why I've sorted the rows in
descending order, on startTime. It shouldn't be necessary, but if something goes wrong,
then we can recover to some extent. Before we have a challenge,
I'll copy this version to database, back to my computer, using the Device
File Explorer. The database is in \data\data\learnprogramming.academy. TaskTimer\databases
and I'll put this one somewhere that I'll remember it. Remember to right-click
the databases directory, to make sure you copy all the files. Keep a populated copy
of each database version, before you write the code to upgrade the database.
That allows you to copy the database back to your emulator, to test upgrades for
more previous versions. So here's a challenge: alright, you've got everything
you need to find out if a task was being timed when the app exited. This is a
great opportunity to practice all we've covered about ContentProviders, the
Contract classes and database upgrades. You'll need another view to the database,
using the query I've just typed. I'm gonna call the view vwCurrentTiming.
Remember to update the database version, and call your new function in onUpgrade,
as well as onCreate. You'll also need a Contract class for the new view,
and the ContentProvider's query function will have to be changed to handle a new view.
The classes you'll need to change are:
AppDatabase, AppProvider and a small change to TaskTimerViewModel.
You'll also need to create a new contract class - I'll be calling mine CurrentTimingContract.
I'll go over my solution in the next video. Good luck and see you then.