1 00:00:00,510 --> 00:00:03,450 We've got a pizza object and a topping object. 2 00:00:03,450 --> 00:00:06,850 And when the app is created we use the toppings list 3 00:00:06,850 --> 00:00:09,470 to generate our topping bitmaps. 4 00:00:09,470 --> 00:00:12,730 With that out of the way let's move on to the activities. 5 00:00:12,730 --> 00:00:16,440 We've got main activity, which was the list of pizzas. 6 00:00:16,440 --> 00:00:20,870 And creator activity, which let's us customize the pizzas. 7 00:00:20,870 --> 00:00:22,150 Let's start with main activity. 8 00:00:23,550 --> 00:00:27,560 At the top of the file, we've got a pizza ID constant. 9 00:00:27,560 --> 00:00:30,434 Other than that, we just find the recyclerview. 10 00:00:32,690 --> 00:00:35,970 Give it a layout manager, and set it up with an adapter. 11 00:00:37,690 --> 00:00:41,540 If you've not seen something like this before, don't worry. 12 00:00:41,540 --> 00:00:45,640 It's just an anonymous function stored in a variable. 13 00:00:45,640 --> 00:00:51,016 So launch creator activity is a function that takes in an integer 14 00:00:51,016 --> 00:00:57,286 which might be null and when this function runs, it will create an intent, 15 00:00:57,286 --> 00:01:02,578 add the pizza ID to the intent and then start creator activity. 16 00:01:05,086 --> 00:01:09,190 So, let's jump into Main Adapter and start from the top. 17 00:01:09,190 --> 00:01:13,000 You can use Cmd or CTRL+B to jump to a class from its usage. 18 00:01:14,630 --> 00:01:19,300 First, we've got our LaunchCreatorActivity function as the only parameter. 19 00:01:19,300 --> 00:01:25,790 And as we just saw it takes an integer that might be null and returns nothing. 20 00:01:25,790 --> 00:01:29,700 From here, we have a list of pizzas which will contain the pizzas 21 00:01:29,700 --> 00:01:30,930 to display on the screen. 22 00:01:31,930 --> 00:01:35,890 Each row in this RecyclerView will use the pizza row layout. 23 00:01:39,060 --> 00:01:44,440 If we take a look at that layout, under Resources and the Layout directory. 24 00:01:45,710 --> 00:01:49,620 We can see that it contains a text view for the name of the pizza, 25 00:01:49,620 --> 00:01:54,010 a create new pizza button and a divider line at the bottom. 26 00:01:55,120 --> 00:01:58,830 So instead of using multiple views for the pizza rows and 27 00:01:58,830 --> 00:02:04,660 the create new pizza button, we just used this one view and we'll hide the elements 28 00:02:04,660 --> 00:02:09,950 we don't need depending on whether we wanna show the pizza or the button. 29 00:02:09,950 --> 00:02:13,659 Getting back to the adapter, let's move onto the view holder. 30 00:02:13,659 --> 00:02:18,497 Back in the code we can see that the item count is one more than the size of 31 00:02:18,497 --> 00:02:23,183 the list, this is to account for the new pizza button at the bottom. 32 00:02:23,183 --> 00:02:26,087 Moving on to the view holder, we show or 33 00:02:26,087 --> 00:02:30,430 hide the views depending on which type of row it is. 34 00:02:30,430 --> 00:02:35,680 So, if it's not the last item in the list, we'll hide the button and if it is 35 00:02:35,680 --> 00:02:41,020 the last item in the list, we'll make the button visible and hide everything else. 36 00:02:41,020 --> 00:02:43,910 We also set up the on click listeners to call through to our 37 00:02:43,910 --> 00:02:45,460 launch creator activity function. 38 00:02:46,810 --> 00:02:51,520 If it's a pizza row that was clicked, we'll pass in the ID of the pizza. 39 00:02:53,150 --> 00:02:56,315 And if the new pizza button was clicked, we'll just pass in null. 40 00:02:57,980 --> 00:03:02,125 So regardless of which row we click, we'll end up launching CreatorActivity. 41 00:03:03,200 --> 00:03:06,551 Let's hop over to CreatorActivity and see where we go from here. 42 00:03:10,987 --> 00:03:13,240 Let me give us just a little more room here as well. 43 00:03:15,450 --> 00:03:16,895 Let me just hide it instead. 44 00:03:19,493 --> 00:03:24,145 At the top of CreatorActivity we create a pizza ID variable and 45 00:03:24,145 --> 00:03:28,580 lateinit it another variable named pizzaView. 46 00:03:28,580 --> 00:03:32,880 In Cotland, when you create a variable you have to give it a value. 47 00:03:32,880 --> 00:03:35,630 But with lateinit, we can get around that. 48 00:03:35,630 --> 00:03:38,950 We just need to make sure it gets initialized before we use it. 49 00:03:39,960 --> 00:03:44,070 Speaking of PizzaView, let's take a quick detour of the PizzaView class. 50 00:03:45,900 --> 00:03:49,860 Remember, Cmd or Ctrl+B on the class name will jump us into the class. 51 00:03:51,350 --> 00:03:57,510 PizzaView is a custom view, meaning it extends from View that draws a pizza. 52 00:03:57,510 --> 00:04:00,440 It starts by drawing a big circle for the crust. 53 00:04:01,590 --> 00:04:04,130 Then it draws a smaller circle for the body of the pizza. 54 00:04:05,860 --> 00:04:08,420 And finally draws each of the toppings on top. 55 00:04:10,070 --> 00:04:16,140 To tell it which toppings to draw, we pass in a map of toppings to Booleans. 56 00:04:16,140 --> 00:04:19,660 If a topping is paired with the value of true, we draw it. 57 00:04:19,660 --> 00:04:25,320 Otherwise we don't and that's about all we need to know about the PizzaView class. 58 00:04:25,320 --> 00:04:26,950 Though, feel free to pause me and 59 00:04:26,950 --> 00:04:29,060 look through the rest of this code if you'd like. 60 00:04:30,280 --> 00:04:35,010 Getting back to CreatorActivity and the onCreate method, 61 00:04:36,300 --> 00:04:39,820 we retrieve the Pizza ID from the intent and 62 00:04:39,820 --> 00:04:45,630 initialize our PizzaView to an empty pizza, by passing in an empty map. 63 00:04:45,630 --> 00:04:50,610 Later on when we have some data, we'll update this map to not be empty. 64 00:04:50,610 --> 00:04:55,740 From here we add our PizzaView to the frameLayout at the top of the screen and 65 00:04:55,740 --> 00:04:58,156 set up our recyclerView that lives at the bottom. 66 00:04:58,156 --> 00:05:00,750 To finish out CreatorActivity, 67 00:05:00,750 --> 00:05:04,090 we've got a couple methods to set up the menu at the top of the screen. 68 00:05:05,330 --> 00:05:08,440 On create options menu, creates the menu and 69 00:05:08,440 --> 00:05:13,370 on options item selected is where we add functionality to the menu items. 70 00:05:13,370 --> 00:05:16,365 The last class we need to look at is CreatorAdaptor. 71 00:05:21,077 --> 00:05:25,295 This class manages the recyclerView at the bottom of CreatorActivity. 72 00:05:26,440 --> 00:05:29,610 And as a parameter, it takes in a pizza view. 73 00:05:29,610 --> 00:05:34,600 With this pizza view at the bottom, when a switch is checked, 74 00:05:35,990 --> 00:05:38,970 we update the toppings mapping with the state of the switch and 75 00:05:38,970 --> 00:05:42,240 tell the pizza view to update by calling the invalidate method. 76 00:05:43,700 --> 00:05:46,722 Awesome we've now got a good understanding of the app, and 77 00:05:46,722 --> 00:05:50,670 are ready to implement that resistance, I know that was a lot to cover. 78 00:05:50,670 --> 00:05:55,020 But don't worry, from here on out we'll be taking things one step at at time. 79 00:05:55,020 --> 00:05:57,270 And if you want to spend some more time playing around and 80 00:05:57,270 --> 00:05:59,960 getting comfortable with the app, please do. 81 00:05:59,960 --> 00:06:01,640 I'll be here waiting for you when you get back.