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