1 00:00:00,510 --> 00:00:04,070 We've just added functionality to our save and delete buttons. 2 00:00:04,070 --> 00:00:08,534 But that doesn't do us much good if main activity won't show the pizzas. 3 00:00:08,534 --> 00:00:12,216 Just like how we created a view model for our creator activity, 4 00:00:12,216 --> 00:00:15,213 we should also have a viewModel for main activity. 5 00:00:15,213 --> 00:00:19,798 This viewModel will store which pizzas should be displayed in main activity, 6 00:00:19,798 --> 00:00:24,323 let's create a new class named MainViewModel, inside of our main package. 7 00:00:32,680 --> 00:00:38,644 Then let's make it extend from viewModel, And 8 00:00:38,644 --> 00:00:42,172 use Alt + Enter to add the call to the constructor. 9 00:00:42,172 --> 00:00:46,225 Inside this class we'll want to maintain a list of pizzas, and 10 00:00:46,225 --> 00:00:50,513 we'll want that list of pizzas to be based on data in our database. 11 00:00:50,513 --> 00:00:52,898 By using something called live data, 12 00:00:52,898 --> 00:00:56,560 room gives us a way to tie together our UI and our database. 13 00:00:57,560 --> 00:01:02,306 So if something changes in our database that change will be automatically 14 00:01:02,306 --> 00:01:05,760 reflected in the UI without us having to do anything. 15 00:01:05,760 --> 00:01:09,541 Inside the class, let's copy and paste in the code from the teacher's notes and 16 00:01:09,541 --> 00:01:10,604 then walk through it. 17 00:01:13,643 --> 00:01:18,000 Use Alt + Enter to import the pizza class and the liveData class. 18 00:01:18,000 --> 00:01:22,200 We start by creating a private variable named pizzas, 19 00:01:22,200 --> 00:01:28,760 which is a liveData containing a list of pizzas, and we set it equal to null. 20 00:01:28,760 --> 00:01:33,530 When we call the getPizzas function, if our pizzas object is null, 21 00:01:34,800 --> 00:01:37,990 then we populate it with pizzas from the data base. 22 00:01:39,270 --> 00:01:43,613 Otherwise, we'll just return our pizzas object. 23 00:01:43,613 --> 00:01:48,908 This way, we're only loading from the data base when we need to, we should 24 00:01:48,908 --> 00:01:55,070 also fix this error by making pizzaDao get all function return alive data. 25 00:01:55,070 --> 00:01:59,882 Let's use Cmd or Ctrl + B to jump to the declaration, and 26 00:01:59,882 --> 00:02:03,702 then add live data around the list of pizzas. 27 00:02:11,957 --> 00:02:17,330 Perfect, and that's really all you have to do to use a live data. 28 00:02:17,330 --> 00:02:21,987 Okay, now to make a use of our live data object, let's head over to MainActivity, 29 00:02:24,810 --> 00:02:27,111 And add some space at the bottom of onCreate. 30 00:02:29,620 --> 00:02:34,341 Then to retrieve our live data, we'll first need to retrieve our view model, 31 00:02:34,341 --> 00:02:36,138 then call the getAll method. 32 00:02:36,138 --> 00:02:41,011 To retrieve the view model, let's do the same thing we did in creator activity. 33 00:02:41,011 --> 00:02:45,191 Start by calling viewModelProviders with a capital V, 34 00:02:48,084 --> 00:02:51,875 .of and passing in this for our activity. 35 00:02:53,410 --> 00:02:57,956 Then on the next line, let's add .get and 36 00:02:57,956 --> 00:03:02,250 pass in the Java class of our ViewModel, 37 00:03:02,250 --> 00:03:06,055 MainViewModel::class.java. 38 00:03:06,055 --> 00:03:11,639 Now that we've got the viewModel on the next line, let's add a call to getPizzas, 39 00:03:14,370 --> 00:03:18,917 To retrieve our live data containing a list of pizzas. 40 00:03:18,917 --> 00:03:20,652 Once we've got the live data, 41 00:03:20,652 --> 00:03:25,270 we can get notified when anything changes by adding an observer. 42 00:03:25,270 --> 00:03:32,590 On the next line, let's call the .observe function on our live data. 43 00:03:32,590 --> 00:03:37,288 Then we need to pass in a lifecycle owner and an observer object. 44 00:03:37,288 --> 00:03:41,897 Luckily, our activity is a lifecycle owner, so for the first parameter, 45 00:03:41,897 --> 00:03:44,210 let's just pass in this. 46 00:03:44,210 --> 00:03:48,210 Then for the observer parameter, let's pass in a new observer 47 00:03:49,410 --> 00:03:53,430 and use Enter to select the option with the brackets. 48 00:03:54,810 --> 00:03:58,938 Now whenever the underline pizza data changes, 49 00:03:58,938 --> 00:04:02,865 it'll call the code between these brackets, 50 00:04:02,865 --> 00:04:07,306 inside it will be a list of pizzas that might be null. 51 00:04:07,306 --> 00:04:11,746 So first let's make sure that the list of pizzas is not null. 52 00:04:11,746 --> 00:04:17,380 So if it is != null, and so long as it isn't null, 53 00:04:17,380 --> 00:04:23,950 we'll want to update our adapter to use the new pizza list. 54 00:04:25,130 --> 00:04:29,540 An easy way to do this is to just clear the adaptor and then repopulate it. 55 00:04:30,580 --> 00:04:35,058 Inside the if statement let's start by clearing our adaptors list. 56 00:04:35,058 --> 00:04:39,787 So my adaptor.list.clear. 57 00:04:42,032 --> 00:04:46,479 Then let's add our new pizza list to the adaptor's list. 58 00:04:46,479 --> 00:04:54,990 MyAdapter.list.addAll, And we'll pass an it. 59 00:04:56,720 --> 00:05:01,693 And finally let's notify the adapter that the data has been changed. 60 00:05:01,693 --> 00:05:06,770 MyAdapter.norifydatasetchanged, great. 61 00:05:06,770 --> 00:05:10,861 Now if we run the app we should be able to save and delete pizzas, and 62 00:05:10,861 --> 00:05:13,728 those pizzas should show up in main activity. 63 00:05:17,090 --> 00:05:18,376 Or we'll get an error. 64 00:05:22,860 --> 00:05:26,851 It looks like Room can't verify the data integrity. 65 00:05:26,851 --> 00:05:31,306 It looks like we've changed schema but forgot to update the version number. 66 00:05:33,920 --> 00:05:38,491 And it suggests we can fix this by increasing the version number. 67 00:05:38,491 --> 00:05:41,663 Okay, let's head in to our PizzaDatabase class, 68 00:05:47,830 --> 00:05:51,222 And change the version from 1 to 2. 69 00:05:53,596 --> 00:06:00,540 Then let's run the app again, And we got another error. 70 00:06:01,780 --> 00:06:07,880 This time it says a migration from 1 to 2 was required but not found. 71 00:06:07,880 --> 00:06:11,702 Please provide the necessary migration path. 72 00:06:11,702 --> 00:06:15,945 In Room whenever you change something about your database, 73 00:06:15,945 --> 00:06:21,361 you need to tell Android how to handle that change by specifying a migration. 74 00:06:21,361 --> 00:06:23,530 If you'd like to learn more about migrations, 75 00:06:23,530 --> 00:06:26,260 check out the teacher's notes below. 76 00:06:26,260 --> 00:06:30,894 But for us rather than handle a migration, when we make a change, 77 00:06:30,894 --> 00:06:34,618 we'd like the database to just start from scratch. 78 00:06:34,618 --> 00:06:38,569 To do this, In the app class, 79 00:06:41,220 --> 00:06:44,202 Where we declare our database, 80 00:06:44,202 --> 00:06:49,393 let's move the call to .build down a couple of lines and 81 00:06:49,393 --> 00:06:55,479 in the middle add a call to .fallbackToDestructiveMigration. 82 00:06:55,479 --> 00:07:00,898 Then if we run the app one more time, we should be good to go. 83 00:07:05,330 --> 00:07:08,422 So let's try creating a pizza. 84 00:07:08,422 --> 00:07:13,737 Let's add some toppings, Give it a name, 85 00:07:19,060 --> 00:07:22,461 Make sure it stays across rotation, all right. 86 00:07:24,720 --> 00:07:32,002 And let's save it, and there it is our pizza is being shown in main activity. 87 00:07:32,002 --> 00:07:37,566 And if we click on it, well it looks like we haven't handled that part quite yet. 88 00:07:37,566 --> 00:07:41,316 In the next video, we'll see what's going on when we try to select a pizza.