Handle Check/Uncheck7:27 with Jamie Huson
Similar to the Spinner `RxBinding` contains a helpful Observable here, the `RxCompoundButton`. `CompoundButton` is the base class for Checkbox and some other Android Views that toggle between a check and unchecked state.
All right, we simplified a lot of our architecture using RxJava, 0:00 instead of the interfaces and the listeners. 0:03 So the next piece is we want to handle changes to the Todo items themselves. 0:06 So when the user taps that checkbox, and the item is checked or unchecked. 0:11 So, if we look at our code running right now, we can see that it currently does 0:16 work and that's because we still have the old listener interface code in there. 0:21 So that's this TodoCompletedChangeListener and what we see is that when 0:28 we create our adapter, we're passing in this ChangeListener. 0:32 And when the changes happen, they eventually then manually 0:37 get called back to our list to toggle that specific item. 0:41 So what we're gonna do is we're gonna get rid of sort of this middleman 0:46 right here which is this TodoCompletedChangeListener. 0:50 Because what really what we want is when an item is checked or 0:53 unchecked, we want the list to know about it directly. 0:57 So let's go ahead and do that. 1:02 So let's go to our TodoAdapter, and here where we're 1:03 doing the setOnCheckedChangeListener what we wanna do is we wanna modify this here. 1:07 So the first thing that we can do is we want to make an observable out of this. 1:13 And you'll notice that the first thing we had to was set this null listener, 1:19 then set the data, then set the CheckedChangeListener so 1:23 we can get rid of this really nicely. 1:27 Because remember we have that option to skip the first item. 1:29 So, what we're gonna do is we're resetting the data and 1:33 just make it the first thing that we do. 1:36 Then what we're gonna do is we're gonna get rid of this 1:40 CheckedChangeListener stuff. 1:44 And instead, we're gonna use RxCompoundButton. 1:48 And just like RXview and adapter view we used before, it has a nice subscription 1:53 where it creates an observable out of our compound button check changes so 1:59 that's the one we want. 2:02 We want checkedChanges. 2:03 And what this is is it takes in our compound button. 2:05 So we can put in our checkbox and remember we want to skip the first one. 2:08 And really we don't care that this box is being checked, 2:15 which is what this callback gives us. 2:20 It tells us when the box is being checked or unchecked, 2:22 what we care about is the underlying data. 2:24 So what we're gonna do is we're gonna apply another operator that we used 2:27 before map. 2:31 What we're going to do is we're going to map from the checkedChange happening to 2:32 the actual data. 2:36 So let's create a function to do that. 2:38 So it's gonna have a Boolean. 2:41 And then a Todo is the thing that we're gonna return. 2:45 So, Let's implement the method. 2:47 And what we're gonna do here is, we just want to return that data every time. 2:53 So before when we mapped, we were actually checking whether the data was true or 2:59 false. 3:03 And that doesn't matter in this case. 3:04 That's when we were using filter. 3:05 With map here, the Boolean is the value of the checkbox at the time and 3:08 we're not worrying about what that value is. 3:14 What we want to just do is pass through the actual item, 3:17 the Todo that's being acted upon that data that's being acted upon. 3:19 And we need to figure out okay, 3:25 what is actually going to subscribe to this as well? 3:27 So we're gonna call subscribe and 3:30 we need to pass in some sort of subscriber or observer here. 3:36 So let's pass it in, but we don't have one of these yet. 3:40 If we go up here, we can see that before we were passing in the listener. 3:45 So similar to how we are doing that, let's go ahead and 3:48 pass in a subscriber that's gonna listen to this. 3:51 So, the subscriber is really just gonna be remember an action. 3:54 So it's going to be listening for this. 4:00 And we can pass that in here, as well. 4:07 So now whatever action you pass in here is going to be notified. 4:15 Because it's going to be a thing that's subscribing to these changes. 4:21 And there's one thing that we want to note here and that's that this is a view 4:26 as a part of the adapter, as a part of our RecyclerView. 4:30 So that means these views are going on and off the screen. 4:33 And we need to make sure that we clean up our subscriptions when things get added or 4:37 removed from the screen. 4:41 So by that, I specifically mean that we need to make sure we unsubscribe whatever 4:43 our views are going away. 4:47 So to do that, we can use our holder which is a part of RecyclerView. 4:48 So in our holder, what we can do is we can keep track of the subscription in here. 4:54 And then what we can do is when we set up our subscription, 5:06 we can assign it in our holder. 5:10 And finally, there's a callback that we can use in our adapter 5:15 to make sure that unsubscribe happens correctly. 5:20 So let's go ahead and use that so we can go to Generate. 5:22 And we can go to Override Methods. 5:26 And there's one called onViewDetachedFromWindow. 5:31 So let's go ahead and do that. 5:35 So when this view is getting detached from the window. 5:37 What we want to do is grab that subscription and we want to unsubscribe. 5:40 So now whatever subscriber, whatever action we pass in here is gonna 5:46 get notified when our check changes happen. 5:50 And remember, just like we talked about a few minutes ago, 5:53 what really cares about this is our list. 5:57 So our list wants to know when things are being checked or unchecked. 6:01 So, what we're gonna do do is we're gonna make our TodoList be that action. 6:04 So let's implement Action1<Todo. 6:10 So I'm going to move this up. 6:20 Before we had a method called toggle, we were manually calling to toggle things. 6:29 Let's go ahead and make that private and instead, what we're gonna do is, 6:33 We're gonna call toggle internally here. 6:43 So our todoList is going now to subscribe to the changes in the adapter. 6:45 So that means we need to change how we construct this adapter. 6:51 We no longer need this TodoCompletedChangeListener. 6:54 Instead what we need is to pass in the list which is the actual action here to 6:58 our TodoAdapter. 7:03 So now whenever any items are checked or unchecked, 7:07 the list is gonna get that call back to its call and its action immediately. 7:10 And the list will then emit its latest items which will be combined with 7:15 the filter. 7:20 And the adapter will receive the latest items to display in our list. 7:21
You need to sign up for Treehouse in order to download course files.Sign up