Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Saving and Deleting6:53 with Ben Deitch
In this video we'll update CreatorActivity to have working 'Save' and 'Delete' Buttons!
Getting back to updating the save button, 0:00 let's head over to the CreatorActivity class. 0:02 And look for the save option in the onOptionsItemSelected method. 0:09 And here, instead of showing a Toast, 0:16 we mean to save the user's pizza to the database. 0:19 Let's delete the toast one and start by wrapping everything in a new thread. 0:24 Then let's create a new val named pizza to represent the current pizza. 0:31 And let's set it equal to a new Pizza object, which we might need to import. 0:36 But hold on, what id should we use for this pizza? 0:42 Ideally, we'd figure out what the next id is and just use that. 0:46 However, there's actually a better way. 0:51 Let's head over to our Pizza class And 0:53 add parenthesis after the PrimaryKey annotation. 0:57 Here, we can specify a bullion called autoGenerate. 1:01 If this is set to true, we can pass in a null id, and 1:06 behind the scenes, Room will figure out what the next id is and use that one. 1:10 So let's set autoGenerate equal to true. 1:15 And since this means we'll be passing in nulls for the id, we'll need to add 1:19 a question mark to the end of int to show that nulls are allowed for this parameter. 1:24 All right, getting back to the CreatorActivity, let's pass a null for 1:29 the id, _viewModel.pizzaName for 1:33 the pizza name, and a new Date object for the creation date. 1:40 And Alt+Enter to import the Date class. 1:49 Now that we've got our pizza, let's save it using our pizzaDao. 1:51 db.pizzaDao .insert(pizza). 1:54 Next up, we need to save which toppings belong on this pizza. 2:03 Let's add a couple of lines and then loop through our view model switch states. 2:07 viewModel.switchstates.foreach. 2:13 And for each switch, if it's turned on, 2:20 we'll create a new pizza topping object and insert it into the database. 2:22 Remember that switchStates is a map of toppings to booleans. 2:28 Inside the brackets, to check if a Topping is mapped to a value of true, 2:34 let's type if (it.value == true), and add the brackets. 2:40 And since it.value is already a boolean, 2:46 we can actually get rid of the == true part. 2:50 You can use Alt+Enter to simplify it, great. 2:55 Now we need to create a pizzaTopping variable and 3:00 then store it in the database. 3:03 Let's create a new val named pizzaTopping and 3:05 set it equal to a new PizzaTopping object. 3:11 Then, for the pizzaId, we run into a problem. 3:16 Since we passed in an id of null, we don't know what id we ended up getting. 3:20 Luckily, there's an easy solution to this problem. 3:26 Let's open up our pizzaDao class And 3:29 make the insert function return the long. 3:35 Since using auto increment is so common, Room makes it easy for 3:40 us to get the id of an inserted row by just adding a return type. 3:45 Back in CreatorActivity, 3:51 let's store our insert result into a new val named newPizzaId. 3:53 Then let's pass that pizzaId into our PizzaTopping constructor, 4:02 Along with the topping ID of the current topping. 4:08 So it.key to access the topping, and .id to get the ID. 4:12 Also, since we're looking for an Int and we're now returning a long, 4:19 let's add .toInt up her to turn new pizzaId into an integer. 4:23 Finally, we just need to save that pizza topping to the database. 4:30 db.pizzatoppingdao.insert, and pass in our pizzaTopping object. 4:35 Awesome, we're almost done with the save function. 4:44 There's just one thing left to fix. 4:47 As it's written, each time we hit the save button, we'll be saving a new pizza. 4:50 But what would happen if we were editing an existing pizza? 4:57 We'll still save the new pizza and end up with two pizzas in our database. 5:00 To fix this, if we're editing an existing pizza in addition to saving a new pizza, 5:05 we'll go ahead and delete the old one. 5:11 Let's add a line after we start our thread. 5:14 And then remember that a pizzaId of negative one corresponds to a new pizza. 5:18 Which just means that we never ended up updating the pizzaId variable. 5:26 So if pizzaId is anything other than -1, We should delete that pizza. 5:36 Let's first delete it from the pizzaTopping table, 5:47 db.pizzaToppingDao.deletePizzaById, and pass in pizzaId. 5:51 And then let's delete it from the pizzaTable. 5:57 db.pizzaDao().deletePizzaById, and pass in the pizzaId. 6:01 That finishes up saving the pizza. 6:08 Let's see if we can't quickly hook up the delete button as well. 6:10 Let's delete the Toast Inside the delete code and replace it with a new thread. 6:14 Then, inside the thread, let's just copy and paste the if statement we just wrote. 6:25 Since we can only delete pizzas that already exist, 6:32 this is just the code we're looking for. 6:36 Great work hooking up those save and delete sections. 6:39 However, before we'll be able to see any changes, 6:42 we'll need to update MainActivity to display which pizzas are in the database. 6:45 We'll do that in the next video. 6:50
You need to sign up for Treehouse in order to download course files.Sign up