1 00:00:00,300 --> 00:00:01,780 The app looks great. 2 00:00:01,780 --> 00:00:04,990 We've got all our recipes and we can easily keep track of which 3 00:00:04,990 --> 00:00:09,020 ingredients we've already added and which directions we've already followed. 4 00:00:09,020 --> 00:00:11,670 All that's left is to handle the tablet layout. 5 00:00:11,670 --> 00:00:15,070 But before we get to that, let's take another look at the ingredients and 6 00:00:15,070 --> 00:00:16,020 directions fragments. 7 00:00:17,370 --> 00:00:22,083 By this point you've probably realized that the IngredientsFragment class and 8 00:00:22,083 --> 00:00:25,132 the DirectionsFragment class are pretty similar, 9 00:00:25,132 --> 00:00:29,160 which makes this a good opportunity to do a refactoring. 10 00:00:29,160 --> 00:00:32,930 Let's start by figuring out exactly where these two fragments differ. 11 00:00:34,050 --> 00:00:40,012 If we look through the code, The two 12 00:00:40,012 --> 00:00:45,783 big differences are the layout and whether we're using Recipes.ingredients or 13 00:00:45,783 --> 00:00:49,470 Recipes.directions to populate the check boxes. 14 00:00:50,980 --> 00:00:57,046 But actually the layouts, except for the IDs, are exactly the same. 15 00:01:00,226 --> 00:01:04,762 So the only real difference between these two fragments is whether 16 00:01:04,762 --> 00:01:09,150 we're using Recipes.ingredients or Recipes.directions. 17 00:01:09,150 --> 00:01:15,040 Let's refactor our app to instead just use one class named CheckBoxesFragment. 18 00:01:15,040 --> 00:01:17,230 Then when we create this fragment, 19 00:01:17,230 --> 00:01:21,730 we can tell it what to display, ingredients or directions. 20 00:01:21,730 --> 00:01:27,050 So in addition to passing in the index, we'll also be passing in a boolean. 21 00:01:27,050 --> 00:01:28,392 If that boolean is true, 22 00:01:28,392 --> 00:01:32,530 our CheckBoxesFragment will show the ingredients. 23 00:01:32,530 --> 00:01:33,922 And if that boolean is false, 24 00:01:33,922 --> 00:01:37,580 our CheckBoxesFragment will show the directions. 25 00:01:37,580 --> 00:01:40,308 Over in ViewPagerFragment, 26 00:01:40,308 --> 00:01:46,317 let's start by creating a new key named KEY_IS_INGREDIENTS. 27 00:01:46,317 --> 00:01:49,218 Psfs, Enter, 28 00:01:49,218 --> 00:01:54,129 KEY_IS_INGREDIENTS = 29 00:01:54,129 --> 00:01:59,936 "key_is_ingredients". 30 00:02:02,150 --> 00:02:06,200 Next, let's create our CheckBoxesFragment class. 31 00:02:06,200 --> 00:02:09,060 But instead of actually creating a new class, 32 00:02:09,060 --> 00:02:11,500 let's just rename IngredientsFragment. 33 00:02:11,500 --> 00:02:13,808 It's already most of the way there. 34 00:02:13,808 --> 00:02:19,368 Right-click on IngredientsFragment in the project pane on the left. 35 00:02:19,368 --> 00:02:25,548 Select Refactor > Rename, which has a shortcut of Shift+F6. 36 00:02:25,548 --> 00:02:29,528 And then let's name it CheckBoxesFragment. 37 00:02:32,009 --> 00:02:35,089 Then it asks if we want to rename our variable as well. 38 00:02:35,089 --> 00:02:37,128 But we don't, so let's just hit OK. 39 00:02:39,528 --> 00:02:40,710 While we're at it, 40 00:02:40,710 --> 00:02:44,762 let's also rename the ingredients layout to fragment check boxes. 41 00:02:44,762 --> 00:02:53,930 Shift+F6, fragment_checkboxes. 42 00:02:53,930 --> 00:02:59,614 And inside this layout, let's rename the id to checkBoxesLayout as well. 43 00:02:59,614 --> 00:03:09,442 Shift+F6, checkBoxesLayout. 44 00:03:09,442 --> 00:03:12,730 And make sure you're using the actual rename option. 45 00:03:12,730 --> 00:03:16,950 This way, these will be updated everywhere and not just in this one file. 46 00:03:18,560 --> 00:03:23,725 Then, back in our CheckBoxesFragment class, which used to be the ListFragment 47 00:03:23,725 --> 00:03:28,420 class, the layout name and layout id should already be updated. 48 00:03:28,420 --> 00:03:32,280 So let's start by retrieving the isIngredients boolean from the arguments. 49 00:03:33,320 --> 00:03:35,672 Right below where we set the index, 50 00:03:35,672 --> 00:03:39,681 let's create a new boolean variable named isIngredients. 51 00:03:43,101 --> 00:03:50,259 And set it equal to getArguments[].getBoolean[ViewPagerFragme- 52 00:03:50,259 --> 00:03:53,302 nt.KEY_IS_INGREDIENTS]. 53 00:03:57,182 --> 00:04:01,032 And now that we know which type of fragment this is, ingredients or 54 00:04:01,032 --> 00:04:05,250 directions, we just need to grab the right data from the Recipes class. 55 00:04:06,920 --> 00:04:10,740 But first, since this class is now a little less specialized, 56 00:04:10,740 --> 00:04:13,371 let's rename the ingredients array to contents. 57 00:04:13,371 --> 00:04:18,660 Shift+F6, contents. 58 00:04:18,660 --> 00:04:20,465 Then on the next line, 59 00:04:20,465 --> 00:04:26,553 let's create an if statement to help us populate our new contents are read. 60 00:04:26,553 --> 00:04:29,112 if (isIngredients) is true, 61 00:04:29,112 --> 00:04:33,757 we'll set the contents array equal to the ingredients for 62 00:04:33,757 --> 00:04:38,132 this recipe, which we can copy and paste from up here. 63 00:04:44,233 --> 00:04:49,233 Otherwise we'll set the contents array equal to the directions. 64 00:04:49,233 --> 00:04:56,950 Else, Copy and paste and change and ingredients to directions. 65 00:04:58,110 --> 00:05:01,689 And now, since our contents array will always be overwritten, 66 00:05:01,689 --> 00:05:03,885 we can delete the initial assignment. 67 00:05:06,745 --> 00:05:08,646 Last, and definitely least, 68 00:05:08,646 --> 00:05:13,189 let's update the setUpCheckBoxes method to use better variable names. 69 00:05:14,390 --> 00:05:22,520 Let's refractor ingredients to be contents, and ingredient to be content. 70 00:05:24,070 --> 00:05:27,168 Now back in our ViewPagerFragment class, 71 00:05:27,168 --> 00:05:32,870 let's update our two bundles to include the KEY_IS_INGREDIENT argument. 72 00:05:32,870 --> 00:05:35,776 Right above where we set the arguments for our ingredients fragment, 73 00:05:35,776 --> 00:05:44,505 let's add bundle.putBoolean(KEY_IS_INGREDIENTS, 74 00:05:44,505 --> 00:05:49,090 true), because this is the ingredientsFragment. 75 00:05:49,090 --> 00:05:54,746 Then let's copy this, Paste it down here and 76 00:05:54,746 --> 00:06:00,020 change true to false, because this isn't the IngredientsFragment. 77 00:06:00,020 --> 00:06:04,048 And as the very last step, let's make our DirectionsFragment be of type 78 00:06:04,048 --> 00:06:07,165 CheckBoxesFragment instead of DirectionsFragment. 79 00:06:13,505 --> 00:06:17,166 Which also means that we don't need our DirectionsFragment class or 80 00:06:17,166 --> 00:06:18,920 layout anymore. 81 00:06:18,920 --> 00:06:20,060 So let's delete those. 82 00:06:28,100 --> 00:06:29,625 Now let's test the app and 83 00:06:29,625 --> 00:06:33,381 make sure everything still works just the same as it did before. 84 00:06:41,530 --> 00:06:46,760 Nice, it looks exactly the same and we've got a lot less code. 85 00:06:46,760 --> 00:06:47,260 Great job!