TodoList as Observable9:36 with Jamie Huson
Convert TodoList class into Observable that pushes data to subscribers. That means our data-model which holds the Todo items will emit the updated items anytime the Todo list is updated.
To start converting our app to use RxJava, 0:00 let's look at the core functionality this app has. 0:03 We're gonna convert our to do list class into an observable that pushes data 0:06 to subscribers. 0:11 So I got to look at our TodoList class, and if you recall from before, 0:13 this holds a list of to do's and 0:19 has the ability to add and remove and toggle to do's here. 0:22 And it also has a listener and 0:27 if you look at this TodoListener you can see that any time our list changes. 0:30 Anyone who is registered, any object or 0:36 other part of our app that's registered can listen for changes to this list. 0:39 And so what we're gonna do is we're going to convert 0:44 our TodoList into an observable. 0:47 So when changes happen it pushes and emits those changes out to any subscribers. 0:50 So to do this what we need to do is we need this TodoList 0:57 to both be an observable and also emit items. 1:00 So be able to push items out through that observable and 1:04 there's a specific type RxJava that we can use to do this, 1:06 it's called a subject and theres several different types of subjects. 1:10 For this, we're gonna use one called a replay subject. 1:14 So let's add it in here in the we'll talk about what it does. 1:18 So our replay subject it's typed and we're going to make it the TodoList type and 1:22 that just means that every time we emit an item it's gonnas be a TodoList object. 1:27 So that means whenever anything changes we're gonna update this TodoList object 1:32 and then push the object out emit it as an item and we'll gonna call it a notifier. 1:36 And using RxJava, normally you think, I'm gonna create something using new. 1:42 In RxJava usually there's a static helper method to create objects. 1:47 So in this case we're gonna say ReplaySubject.create. 1:53 So what does this do? 1:57 This ReplaySubject is a special kind of subject and 1:58 what it does is If item are emitted let's say two items are emitted and then a new 2:01 subscriber subscribes to this subject remember, the subject is an observable. 2:07 It's pushing, it's emitting items so 2:12 as subscribers subscribe it's gonna emit all items that have been emitted so far. 2:15 So it's gonna replay those items to every new subscriber. 2:21 That means no subscribers will miss out on any items if they subscribe later 2:24 then when items were first emitted and that's what we wanna do here. 2:29 And now we want to use this notifier instead of our TodoListener. 2:34 So I'm gonna remove the TodoListener, we're not gonna use that anymore. 2:38 And we're gonna go down here and 2:42 every place that we reference our TodoListener we're going to get rid of it. 2:44 So the set listener we just don't need that at all. 2:48 And down here, when we add to you we're gonna update the list. 2:52 And now instead of notifying the listener, what we're gonna do is push out or 2:56 emit an item on our notifier and we do that by just calling the onNext. 3:00 And it expects the updated TodoList which is just this object so 3:05 we're gonna pass in this and we're gonna do the same exact thing for remove. 3:09 So for remove we're gonna go ahead and say notifier.onNext(this). 3:12 So if items are added or 3:18 removed any subscribers will receive the new updated TodoList object. 3:20 And finally, down here, 3:27 if something is toggled as well, we can go ahead and notify as well. 3:29 So we'll say notifier.onNext(this). 3:33 So that's pretty straightforward, we got rid of our interface and 3:39 our listener call back and we made our TodoList into 3:43 an observable using the subject and specifically this ReplaySubject. 3:47 So now whenever this list changes we need something else to subscribe to this and 3:52 the main thing we want to subscribe is our adapter. 3:59 The adapter is listening for changes in the list of items and 4:02 then it updates its own data set, and calls notify data set change. 4:06 So we can see here, right now, it implements TodoListener. 4:12 And whenever TodoListener on TodoList changed gets called, 4:15 it sets the new data and calls notifyDataSetChanged. 4:19 In this is more or less exactly what we wanna do except we're not gonna use this 4:23 interface anymore instead we wanna use our TodoList as an observable. 4:26 So to do that we actually need to expose the to do list as an observable so 4:31 we're gonna add a method to do that. 4:36 So it's gonna return back observable and we're gonna call it asObservable. 4:45 And we're gonna return back our notifier. 4:50 So now when I have my TodoList I can use it as an observable and 4:56 we'll be able to subscribe to it. 5:00 And the thing that's gonna subscribe is our to do adapter. 5:03 So we want to call back in here when that happens and 5:06 we need to set up that subscription. 5:09 So to do that instead of implementing TodoListener, let's go ahead and 5:10 implement the action that we talked about before. 5:14 So remember Action Is when we don't care about on a completed or 5:16 on error, we just care about onNext. 5:21 So any time this TodoList changes, it calls onNext, that's what we care about. 5:24 So we're just gonna change this TodoList as an Action, 5:28 make sure we import it and implement the required method which is just call. 5:33 And then we're simply gonna move this code into our call method. 5:41 So the call method is going to be called, every time onNext. 5:47 Would be called for the subscription and what we're gonna do is just gonna say all 5:51 right data be the new todoList and then notify data set changed. 5:55 So everything else remains the same we still have some listeners and 6:02 things will get to that, but we've now changed our TodoList to an observable and 6:04 on our to do adaptor to be an observer with an action. 6:10 And now we can go to our main activity and 6:14 just make sure that that gets set up correctly. 6:16 So before we are passing in our onTodoCompletedChanged 6:18 listener as part of our adapter we would be calling our onTodoListChanged. 6:23 So we no longer need that because 6:29 the TodoList is gonna push out changes automatically to any subscribers 6:31 all we need to do is make sure that our adapter is subscribed to our list. 6:36 So let's just get our list as an observable and 6:41 let's subscribe and the thing that's going to subscribe is going to be our adapter. 6:46 So we created our list up here we created our adapter. 6:55 Now we subscribe our adaptor to our list. 6:58 So any time our list changes, we no longer need to manually call to update and 7:01 send out those listeners. 7:06 We can just get rid of these all together. 7:08 So list will call toggle. 7:12 It will push out and 7:14 emit a new item of the list to any subscriber, same with down here. 7:15 Now you'll notice that there was a part here for filtering data, and 7:22 we'll get to that. 7:26 But for now we're just gonna delete these, cuz we no longer need that TodoListener. 7:27 And if we look for any references to TodoListener, 7:41 we can see the TodoListFilter also implements the TodoListener. 7:44 So we can see that this TodoListFilter, 7:51 we can also update and change it so it doesn't use our TodoListener anymore. 7:55 So remember the list here, setListener(this). 8:00 What we wanna do is just make this instead into an action. 8:04 The same one we had before. 8:10 And we had here on TodoListChanged, go ahead and 8:22 change that move the code over here. 8:26 We no longer need to set the listener. 8:41 And, finally we just want to subscribe 8:44 our TodoListFilter as well to our list. 8:50 So now any time items are emitted from our TodoList the filter and 9:00 the adapter are both going to get those items emitted to them as observers. 9:05 So as you can see Android depends a lot on listeners and 9:12 we often make interfaces with different types of listeners. 9:15 But the RxJava model of push instead of pull can simplify a lot of this code and 9:18 it can simplify the interactions between different objects in your app. 9:24 So this is just the start we're gonna keep going and 9:27 we'll see that it gets even simpler as we add in new functionality RxJava gives us. 9:30
You need to sign up for Treehouse in order to download course files.Sign up