Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Refactor6:47 with Ben Deitch
In this video we'll refactor IngredientsFragment and DirectionsFragment!
The app looks great. 0:00 We've got all our recipes and we can easily keep track of which 0:01 ingredients we've already added and which directions we've already followed. 0:04 All that's left is to handle the tablet layout. 0:09 But before we get to that, let's take another look at the ingredients and 0:11 directions fragments. 0:15 By this point you've probably realized that the IngredientsFragment class and 0:17 the DirectionsFragment class are pretty similar, 0:22 which makes this a good opportunity to do a refactoring. 0:25 Let's start by figuring out exactly where these two fragments differ. 0:29 If we look through the code, The two 0:34 big differences are the layout and whether we're using Recipes.ingredients or 0:40 Recipes.directions to populate the check boxes. 0:45 But actually the layouts, except for the IDs, are exactly the same. 0:50 So the only real difference between these two fragments is whether 1:00 we're using Recipes.ingredients or Recipes.directions. 1:04 Let's refactor our app to instead just use one class named CheckBoxesFragment. 1:09 Then when we create this fragment, 1:15 we can tell it what to display, ingredients or directions. 1:17 So in addition to passing in the index, we'll also be passing in a boolean. 1:21 If that boolean is true, 1:27 our CheckBoxesFragment will show the ingredients. 1:28 And if that boolean is false, 1:32 our CheckBoxesFragment will show the directions. 1:33 Over in ViewPagerFragment, 1:37 let's start by creating a new key named KEY_IS_INGREDIENTS. 1:40 Psfs, Enter, 1:46 KEY_IS_INGREDIENTS = 1:49 "key_is_ingredients". 1:54 Next, let's create our CheckBoxesFragment class. 2:02 But instead of actually creating a new class, 2:06 let's just rename IngredientsFragment. 2:09 It's already most of the way there. 2:11 Right-click on IngredientsFragment in the project pane on the left. 2:13 Select Refactor > Rename, which has a shortcut of Shift+F6. 2:19 And then let's name it CheckBoxesFragment. 2:25 Then it asks if we want to rename our variable as well. 2:32 But we don't, so let's just hit OK. 2:35 While we're at it, 2:39 let's also rename the ingredients layout to fragment check boxes. 2:40 Shift+F6, fragment_checkboxes. 2:44 And inside this layout, let's rename the id to checkBoxesLayout as well. 2:53 Shift+F6, checkBoxesLayout. 2:59 And make sure you're using the actual rename option. 3:09 This way, these will be updated everywhere and not just in this one file. 3:12 Then, back in our CheckBoxesFragment class, which used to be the ListFragment 3:18 class, the layout name and layout id should already be updated. 3:23 So let's start by retrieving the isIngredients boolean from the arguments. 3:28 Right below where we set the index, 3:33 let's create a new boolean variable named isIngredients. 3:35 And set it equal to getArguments.getBoolean[ViewPagerFragme- 3:43 nt.KEY_IS_INGREDIENTS]. 3:50 And now that we know which type of fragment this is, ingredients or 3:57 directions, we just need to grab the right data from the Recipes class. 4:01 But first, since this class is now a little less specialized, 4:06 let's rename the ingredients array to contents. 4:10 Shift+F6, contents. 4:13 Then on the next line, 4:18 let's create an if statement to help us populate our new contents are read. 4:20 if (isIngredients) is true, 4:26 we'll set the contents array equal to the ingredients for 4:29 this recipe, which we can copy and paste from up here. 4:33 Otherwise we'll set the contents array equal to the directions. 4:44 Else, Copy and paste and change and ingredients to directions. 4:49 And now, since our contents array will always be overwritten, 4:58 we can delete the initial assignment. 5:01 Last, and definitely least, 5:06 let's update the setUpCheckBoxes method to use better variable names. 5:08 Let's refractor ingredients to be contents, and ingredient to be content. 5:14 Now back in our ViewPagerFragment class, 5:24 let's update our two bundles to include the KEY_IS_INGREDIENT argument. 5:27 Right above where we set the arguments for our ingredients fragment, 5:32 let's add bundle.putBoolean(KEY_IS_INGREDIENTS, 5:35 true), because this is the ingredientsFragment. 5:44 Then let's copy this, Paste it down here and 5:49 change true to false, because this isn't the IngredientsFragment. 5:54 And as the very last step, let's make our DirectionsFragment be of type 6:00 CheckBoxesFragment instead of DirectionsFragment. 6:04 Which also means that we don't need our DirectionsFragment class or 6:13 layout anymore. 6:17 So let's delete those. 6:18 Now let's test the app and 6:28 make sure everything still works just the same as it did before. 6:29 Nice, it looks exactly the same and we've got a lot less code. 6:41 Great job! 6:46
You need to sign up for Treehouse in order to download course files.Sign up