Cleaning Your Code6:28 with Jamie Huson
In this workshop we introduced the concepts of RxJava and we walked through converting a simple Todo app to use it. With this concept you can move forward and start using RxJava in many parts of your apps.
Okay, now that we've converted to using RX Java form, 0:00 most of the core functionality of our app lets just kind and 0:03 clean up this code a bit and then run it and see it in action. 0:06 So, firstly we can do to clean up is we get rid of this completely change listener 0:09 which we converted into an observable in the last section. 0:14 So let's go and delete that. 0:20 And in our main activity what we can do is if you want 0:23 you can convert all these to using butterknife which is much easier. 0:28 So we can say bind here. 0:33 Or to do adapters here so we put all of our views together and 1:03 we have our list of data and our adapter down here. 1:08 It turns out we don't really need to keep our adapter anywhere else. 1:13 The adapter is not being utilized any place else outside of on create. 1:17 So what we're gonna do is just make it a local variable. 1:22 Let's just move it down to here. 1:28 So we're simplifying our main activity. 1:34 Has our views, it has our data set, our list. 1:38 And it has our subscriptions Let's make sure that we 1:41 bind so we can get all those views. 1:49 We no longer need to do any of these look ups by ID here or here. 1:51 So we're gonna create our list. 2:01 And let's go ahead and 2:03 add a few items to our list if it's the first time we're in here. 2:04 So let's just go ahead down here and add a new todo. 2:10 This is just so we can run it later in a minute and see it in action. 2:32 We'll also be able to add our own items as well through the UI. 2:37 To create our adapter, make sure we set up our recycler view 2:43 request focus that's fine. 2:48 Do our subscription here, or toolbar we don't need to do this anymore. 2:51 Let's go ahead and move this up to the top 2:55 We don't need to find our spinner anymore. 3:05 We do need to call setAdapter. 3:11 And actually the last thing that we're gonna do is we're gonna set the adapter. 3:13 And then we also need to set on the spinner the last 3:22 selected position so set selection with the position. 3:27 So we need to keep track of that filter position. 3:32 Let's go ahead and add the ability to do that now. 3:35 So up here, we have our list. 3:38 And what we wanna do is just add the int for the filter position. 3:41 And by default. 3:49 It's going to be, all And then we want 3:51 to save that across rotation too so let's go ahead and put in a value for that. 3:58 This is our key for our bundle. 4:04 And we want to make sure that we get that data as well. 4:10 So here savings in state is not nul. 4:13 We want to go ahead and get our filter positions from our statements and 4:15 stay bundle. 4:20 And we can supply a default, as well. 4:26 And let's make sure we save it in on save instant state. 4:33 And what we want here is, we wanna get the item that was currently selected. 4:50 So it's getSelectedItemPosition. 4:57 Okay so we've just cleaned up some things here and simplified our code a bit. 5:07 Let's go ahead and run it and see this happen in action. 5:14 Here's the old version. 5:24 Let's see it after we clean it up. 5:26 Here it is so we have the sample items and we can add another item as well. 5:32 And we can check some and select a filter, 5:41 and see that our UI dynamically adjusts as we select and check things. 5:47 In this workshop, we introduced the concepts of RX Java and 5:54 we walked through converting a simple to do app to use it. 5:58 With this concept you can move forward and 6:02 start using RX Java in many parts of your own apps. 6:04 To start using RX Java to manage your networking take a look at retrofit 6:08 which supports RX Java out of the box. 6:12 If you haven't used retrofit before I've left a link to the retrofit workshop 6:15 in the teacher's notes. 6:19 Thanks for joining me in this workshop. 6:21 I hope RX Java helps you build better android apps. 6:23 Will see you soon. 6:27
You need to sign up for Treehouse in order to download course files.Sign up