1 00:00:00,660 --> 00:00:05,070 We've just got access to our ViewModel, now it's time to start using it. 2 00:00:05,070 --> 00:00:10,060 Let's start by updating the activities ui based on what's in the ViewModel. 3 00:00:10,060 --> 00:00:14,380 To update the name of the pizza, we just need to change the title of the activity. 4 00:00:15,650 --> 00:00:18,898 On the line after our ViewModel, 5 00:00:18,898 --> 00:00:23,310 let's set title=viewModel.pizzaName. 6 00:00:24,630 --> 00:00:28,740 Next, we need to update the pizza based on what's in the view model. 7 00:00:28,740 --> 00:00:33,926 To do this, instead of passing an empty map to our pizzaView, 8 00:00:33,926 --> 00:00:37,631 let's pass in viewModel.switchStates. 9 00:00:37,631 --> 00:00:38,790 Great! 10 00:00:38,790 --> 00:00:41,830 Now that we've updated the ui based on the viewModel, 11 00:00:41,830 --> 00:00:45,160 we just need to update the viewModel whenever the ui changes. 12 00:00:46,180 --> 00:00:50,810 When a user gives a pizza a new name, it triggers this code down here. 13 00:00:53,400 --> 00:00:57,050 Which makes sure there's actually some text and 14 00:00:57,050 --> 00:01:00,380 the edit text, and then updates the activities title. 15 00:01:01,940 --> 00:01:05,968 Let's add a line below that and also update our view model, 16 00:01:05,968 --> 00:01:11,050 viewModel.pizzaName =_text. 17 00:01:11,050 --> 00:01:15,520 Perfect, all that's left is updating the switches based on the viewModel and 18 00:01:15,520 --> 00:01:19,120 updating the viewModel whenever a switch is switched. 19 00:01:19,120 --> 00:01:23,280 To do this since those actions take place in creator adapter, 20 00:01:23,280 --> 00:01:27,420 we'll need to pass along our viewModel to creator adapter. 21 00:01:27,420 --> 00:01:32,397 Let's add our viewModel as the second parameter to creator adapter up inside of 22 00:01:32,397 --> 00:01:33,146 onCreate. 23 00:01:35,588 --> 00:01:40,298 Then let's use Alt+Enter to add this parameter to the constructor, 24 00:01:42,770 --> 00:01:47,148 And choose for it to be a vowel, then we hit Refactor, and 25 00:01:47,148 --> 00:01:50,120 let's open Creator Adaptor. 26 00:01:50,120 --> 00:01:53,231 And now that we've got our view model as a variable, 27 00:01:53,231 --> 00:01:57,133 let's use it in the updateView function to update the switches. 28 00:01:57,133 --> 00:02:00,369 Below where we update that text, 29 00:02:00,369 --> 00:02:05,571 let's add a new line instead of switch.isChecked = 30 00:02:05,571 --> 00:02:11,360 _viewModel.switchStates and then pass in the topping. 31 00:02:13,851 --> 00:02:17,439 Then it's telling us that it's looking for a boolean, but 32 00:02:17,439 --> 00:02:20,870 it's found a boolean that might be null. 33 00:02:20,870 --> 00:02:23,980 So let's use the Elvis operator to give it a fallback value. 34 00:02:25,160 --> 00:02:30,130 At the end of the line, add a space, then a question mark and 35 00:02:30,130 --> 00:02:34,020 a colon, then another space, and type false. 36 00:02:34,020 --> 00:02:39,830 And now if this value is ever null, we'll just get false instead. 37 00:02:39,830 --> 00:02:42,630 Last but not least, when a switch is switched, 38 00:02:42,630 --> 00:02:46,640 we need to update the view model instead of the pizza toppings list. 39 00:02:47,750 --> 00:02:52,120 So instead of pizzaView.toppings, 40 00:02:52,120 --> 00:02:57,200 let's go with viewModel.switchStates. 41 00:02:57,200 --> 00:02:59,250 That should do it for problem number one. 42 00:02:59,250 --> 00:03:01,281 Let's run the app and see if it worked. 43 00:03:12,622 --> 00:03:14,050 Let's create a new pizza. 44 00:03:15,430 --> 00:03:16,782 Add a couple of toppings. 45 00:03:20,390 --> 00:03:21,437 Give it a fun name. 46 00:03:26,712 --> 00:03:28,071 And then rotate the app. 47 00:03:29,931 --> 00:03:32,471 Awesome, it looks like everything worked. 48 00:03:32,471 --> 00:03:35,611 Coming up in the next stage, we'll start diving into room, and 49 00:03:35,611 --> 00:03:38,291 learn how we can store pizza in a database.