1 00:00:00,660 --> 00:00:03,670 All right, we simplified a lot of our architecture using RxJava, 2 00:00:03,670 --> 00:00:06,730 instead of the interfaces and the listeners. 3 00:00:06,730 --> 00:00:11,500 So the next piece is we want to handle changes to the Todo items themselves. 4 00:00:11,500 --> 00:00:15,680 So when the user taps that checkbox, and the item is checked or unchecked. 5 00:00:16,740 --> 00:00:21,820 So, if we look at our code running right now, we can see that it currently does 6 00:00:21,820 --> 00:00:27,040 work and that's because we still have the old listener interface code in there. 7 00:00:28,060 --> 00:00:32,917 So that's this TodoCompletedChangeListener and what we see is that when 8 00:00:32,917 --> 00:00:37,560 we create our adapter, we're passing in this ChangeListener. 9 00:00:37,560 --> 00:00:41,680 And when the changes happen, they eventually then manually 10 00:00:41,680 --> 00:00:45,440 get called back to our list to toggle that specific item. 11 00:00:46,630 --> 00:00:50,600 So what we're gonna do is we're gonna get rid of sort of this middleman 12 00:00:50,600 --> 00:00:53,820 right here which is this TodoCompletedChangeListener. 13 00:00:53,820 --> 00:00:57,520 Because what really what we want is when an item is checked or 14 00:00:57,520 --> 00:01:01,240 unchecked, we want the list to know about it directly. 15 00:01:02,660 --> 00:01:03,630 So let's go ahead and do that. 16 00:01:03,630 --> 00:01:07,640 So let's go to our TodoAdapter, and here where we're 17 00:01:07,640 --> 00:01:12,260 doing the setOnCheckedChangeListener what we wanna do is we wanna modify this here. 18 00:01:13,280 --> 00:01:19,560 So the first thing that we can do is we want to make an observable out of this. 19 00:01:19,560 --> 00:01:23,820 And you'll notice that the first thing we had to was set this null listener, 20 00:01:23,820 --> 00:01:27,870 then set the data, then set the CheckedChangeListener so 21 00:01:27,870 --> 00:01:29,750 we can get rid of this really nicely. 22 00:01:29,750 --> 00:01:33,780 Because remember we have that option to skip the first item. 23 00:01:33,780 --> 00:01:36,960 So, what we're gonna do is we're resetting the data and 24 00:01:36,960 --> 00:01:38,970 just make it the first thing that we do. 25 00:01:40,110 --> 00:01:44,277 Then what we're gonna do is we're gonna get rid of this 26 00:01:44,277 --> 00:01:46,820 CheckedChangeListener stuff. 27 00:01:48,400 --> 00:01:52,365 And instead, we're gonna use RxCompoundButton. 28 00:01:53,730 --> 00:01:59,060 And just like RXview and adapter view we used before, it has a nice subscription 29 00:01:59,060 --> 00:02:02,750 where it creates an observable out of our compound button check changes so 30 00:02:02,750 --> 00:02:03,420 that's the one we want. 31 00:02:03,420 --> 00:02:04,519 We want checkedChanges. 32 00:02:05,770 --> 00:02:08,690 And what this is is it takes in our compound button. 33 00:02:08,690 --> 00:02:14,330 So we can put in our checkbox and remember we want to skip the first one. 34 00:02:15,540 --> 00:02:20,280 And really we don't care that this box is being checked, 35 00:02:20,280 --> 00:02:22,040 which is what this callback gives us. 36 00:02:22,040 --> 00:02:24,868 It tells us when the box is being checked or unchecked, 37 00:02:24,868 --> 00:02:27,091 what we care about is the underlying data. 38 00:02:27,091 --> 00:02:31,348 So what we're gonna do is we're gonna apply another operator that we used 39 00:02:31,348 --> 00:02:32,111 before map. 40 00:02:32,111 --> 00:02:36,610 What we're going to do is we're going to map from the checkedChange happening to 41 00:02:36,610 --> 00:02:38,180 the actual data. 42 00:02:38,180 --> 00:02:41,130 So let's create a function to do that. 43 00:02:41,130 --> 00:02:45,040 So it's gonna have a Boolean. 44 00:02:45,040 --> 00:02:47,990 And then a Todo is the thing that we're gonna return. 45 00:02:47,990 --> 00:02:53,517 So, Let's implement the method. 46 00:02:53,517 --> 00:02:59,190 And what we're gonna do here is, we just want to return that data every time. 47 00:02:59,190 --> 00:03:03,480 So before when we mapped, we were actually checking whether the data was true or 48 00:03:03,480 --> 00:03:04,160 false. 49 00:03:04,160 --> 00:03:05,700 And that doesn't matter in this case. 50 00:03:05,700 --> 00:03:08,240 That's when we were using filter. 51 00:03:08,240 --> 00:03:14,130 With map here, the Boolean is the value of the checkbox at the time and 52 00:03:14,130 --> 00:03:17,110 we're not worrying about what that value is. 53 00:03:17,110 --> 00:03:19,872 What we want to just do is pass through the actual item, 54 00:03:19,872 --> 00:03:23,430 the Todo that's being acted upon that data that's being acted upon. 55 00:03:25,400 --> 00:03:27,700 And we need to figure out okay, 56 00:03:27,700 --> 00:03:30,310 what is actually going to subscribe to this as well? 57 00:03:30,310 --> 00:03:34,170 So we're gonna call subscribe and 58 00:03:36,280 --> 00:03:40,310 we need to pass in some sort of subscriber or observer here. 59 00:03:40,310 --> 00:03:44,410 So let's pass it in, but we don't have one of these yet. 60 00:03:45,460 --> 00:03:48,847 If we go up here, we can see that before we were passing in the listener. 61 00:03:48,847 --> 00:03:51,731 So similar to how we are doing that, let's go ahead and 62 00:03:51,731 --> 00:03:54,441 pass in a subscriber that's gonna listen to this. 63 00:03:54,441 --> 00:03:59,210 So, the subscriber is really just gonna be remember an action. 64 00:04:00,380 --> 00:04:02,562 So it's going to be listening for this. 65 00:04:07,445 --> 00:04:09,803 And we can pass that in here, as well. 66 00:04:15,624 --> 00:04:21,620 So now whatever action you pass in here is going to be notified. 67 00:04:21,620 --> 00:04:25,229 Because it's going to be a thing that's subscribing to these changes. 68 00:04:26,860 --> 00:04:30,800 And there's one thing that we want to note here and that's that this is a view 69 00:04:30,800 --> 00:04:33,680 as a part of the adapter, as a part of our RecyclerView. 70 00:04:33,680 --> 00:04:37,010 So that means these views are going on and off the screen. 71 00:04:37,010 --> 00:04:41,610 And we need to make sure that we clean up our subscriptions when things get added or 72 00:04:41,610 --> 00:04:43,240 removed from the screen. 73 00:04:43,240 --> 00:04:47,240 So by that, I specifically mean that we need to make sure we unsubscribe whatever 74 00:04:47,240 --> 00:04:48,980 our views are going away. 75 00:04:48,980 --> 00:04:54,708 So to do that, we can use our holder which is a part of RecyclerView. 76 00:04:54,708 --> 00:04:59,785 So in our holder, what we can do is we can keep track of the subscription in here. 77 00:05:06,409 --> 00:05:10,379 And then what we can do is when we set up our subscription, 78 00:05:10,379 --> 00:05:12,550 we can assign it in our holder. 79 00:05:15,270 --> 00:05:20,060 And finally, there's a callback that we can use in our adapter 80 00:05:20,060 --> 00:05:22,180 to make sure that unsubscribe happens correctly. 81 00:05:22,180 --> 00:05:26,910 So let's go ahead and use that so we can go to Generate. 82 00:05:26,910 --> 00:05:31,010 And we can go to Override Methods. 83 00:05:31,010 --> 00:05:35,550 And there's one called onViewDetachedFromWindow. 84 00:05:35,550 --> 00:05:37,030 So let's go ahead and do that. 85 00:05:37,030 --> 00:05:40,010 So when this view is getting detached from the window. 86 00:05:40,010 --> 00:05:44,570 What we want to do is grab that subscription and we want to unsubscribe. 87 00:05:46,590 --> 00:05:50,430 So now whatever subscriber, whatever action we pass in here is gonna 88 00:05:50,430 --> 00:05:53,790 get notified when our check changes happen. 89 00:05:53,790 --> 00:05:57,170 And remember, just like we talked about a few minutes ago, 90 00:05:57,170 --> 00:06:01,070 what really cares about this is our list. 91 00:06:01,070 --> 00:06:04,930 So our list wants to know when things are being checked or unchecked. 92 00:06:04,930 --> 00:06:10,600 So, what we're gonna do do is we're gonna make our TodoList be that action. 93 00:06:10,600 --> 00:06:14,246 So let's implement Action1 00:06:25,451 So I'm going to move this up. 95 00:06:29,198 --> 00:06:33,670 Before we had a method called toggle, we were manually calling to toggle things. 96 00:06:33,670 --> 00:06:39,037 Let's go ahead and make that private and instead, what we're gonna do is, 97 00:06:43,007 --> 00:06:45,200 We're gonna call toggle internally here. 98 00:06:45,200 --> 00:06:51,190 So our todoList is going now to subscribe to the changes in the adapter. 99 00:06:51,190 --> 00:06:54,700 So that means we need to change how we construct this adapter. 100 00:06:54,700 --> 00:06:58,740 We no longer need this TodoCompletedChangeListener. 101 00:06:58,740 --> 00:07:03,455 Instead what we need is to pass in the list which is the actual action here to 102 00:07:03,455 --> 00:07:05,070 our TodoAdapter. 103 00:07:07,020 --> 00:07:10,700 So now whenever any items are checked or unchecked, 104 00:07:10,700 --> 00:07:15,700 the list is gonna get that call back to its call and its action immediately. 105 00:07:15,700 --> 00:07:20,060 And the list will then emit its latest items which will be combined with 106 00:07:20,060 --> 00:07:21,260 the filter. 107 00:07:21,260 --> 00:07:25,309 And the adapter will receive the latest items to display in our list.