1 00:00:03,730 --> 00:00:09,670 G'day everyone, welcome back. The title of this video is Storing the current Timing 2 00:00:09,670 --> 00:00:14,620 record, but I'm going to start by asking Do we need to store the current timing 3 00:00:14,620 --> 00:00:19,750 record? To phrase that in a different way, Do we already have the current timing 4 00:00:19,750 --> 00:00:25,340 record stored somewhere? and the answer is yes, it's stored in the database. 5 00:00:25,340 --> 00:00:30,660 If everything is working correctly, there should only be one row in the Timings 6 00:00:30,670 --> 00:00:36,250 table that has a duration of zero. It's worth considering things like this, 7 00:00:36,250 --> 00:00:42,100 before jumping in and storing the currentTiming object in our ViewModel. 8 00:00:42,100 --> 00:00:46,500 If we were accessing a remote database over the internet, then I would probably 9 00:00:46,510 --> 00:00:51,430 store the timing in a SharedPreference. If you want to do that, we'll be looking 10 00:00:51,430 --> 00:00:56,950 at SharedPreferences again, soon. In this case, we can retrieve all the information 11 00:00:56,950 --> 00:01:03,480 we need from the database, with a query. I'll do that in the terminal pane. 12 00:01:03,480 --> 00:01:09,120 It's quite a long query, because we have to join the Timings and the Tasks table, 13 00:01:09,120 --> 00:01:13,600 in order to get the Task name. I'll paste the command into the sqlite3 14 00:01:13,600 --> 00:01:19,100 command line. First, I need to run sqlite3 with our TaskTimer database. 15 00:01:19,100 --> 00:01:40,800 Make sure you've run the adb shell and change to the right directory first. 16 00:01:40,800 --> 00:01:46,980 We get two rows returned, and the first one is the Timing record that we want. 17 00:01:46,980 --> 00:01:52,100 Normally, there would be, at most, one row returned, but I messed up the database by 18 00:01:52,100 --> 00:01:56,080 stopping the app in the previous video. That's why I've sorted the rows in 19 00:01:56,080 --> 00:02:01,580 descending order, on startTime. It shouldn't be necessary, but if something goes wrong, 20 00:02:01,580 --> 00:02:05,960 then we can recover to some extent. Before we have a challenge, 21 00:02:05,960 --> 00:02:10,860 I'll copy this version to database, back to my computer, using the Device 22 00:02:10,860 --> 00:02:34,960 File Explorer. The database is in \data\data\learnprogramming.academy. TaskTimer\databases 23 00:02:34,960 --> 00:02:39,280 and I'll put this one somewhere that I'll remember it. Remember to right-click 24 00:02:39,280 --> 00:02:45,610 the databases directory, to make sure you copy all the files. Keep a populated copy 25 00:02:45,610 --> 00:02:50,860 of each database version, before you write the code to upgrade the database. 26 00:02:50,860 --> 00:02:55,260 That allows you to copy the database back to your emulator, to test upgrades for 27 00:02:55,269 --> 00:03:00,849 more previous versions. So here's a challenge: alright, you've got everything 28 00:03:00,849 --> 00:03:05,950 you need to find out if a task was being timed when the app exited. This is a 29 00:03:05,950 --> 00:03:10,720 great opportunity to practice all we've covered about ContentProviders, the 30 00:03:10,720 --> 00:03:16,239 Contract classes and database upgrades. You'll need another view to the database, 31 00:03:16,240 --> 00:03:23,340 using the query I've just typed. I'm gonna call the view vwCurrentTiming. 32 00:03:23,340 --> 00:03:29,500 Remember to update the database version, and call your new function in onUpgrade, 33 00:03:29,500 --> 00:03:34,860 as well as onCreate. You'll also need a Contract class for the new view, 34 00:03:34,860 --> 00:03:41,540 and the ContentProvider's query function will have to be changed to handle a new view. 35 00:03:41,540 --> 00:03:43,980 The classes you'll need to change are: 36 00:03:43,980 --> 00:03:49,960 AppDatabase, AppProvider and a small change to TaskTimerViewModel. 37 00:03:49,960 --> 00:03:57,800 You'll also need to create a new contract class - I'll be calling mine CurrentTimingContract. 38 00:03:57,800 --> 00:04:05,120 I'll go over my solution in the next video. Good luck and see you then.