1 00:00:00,850 --> 00:00:03,550 Google Play Services encompasses a lot of APIs, 2 00:00:03,550 --> 00:00:08,570 and we needed to handle many edge cases to ensure our app doesn't crash. 3 00:00:09,790 --> 00:00:12,960 By building a helper class we've simplified the interaction with 4 00:00:12,960 --> 00:00:17,420 Google Play Services library, making it easier to reuse in future apps. 5 00:00:18,780 --> 00:00:21,430 Now that our code is connecting to Google Play Services, 6 00:00:21,430 --> 00:00:23,530 we'll be able to start using those APIs. 7 00:00:24,810 --> 00:00:26,710 Let's handle the adapter first. 8 00:00:26,710 --> 00:00:29,330 Remember the adapter is what actually creates our views and 9 00:00:29,330 --> 00:00:34,070 displays them on the screen, and in on bind view holder, we can hide or 10 00:00:34,070 --> 00:00:37,620 show views as we need to, so this is what's really going to 11 00:00:37,620 --> 00:00:41,950 care about whether Google Play Services is available or is not available. 12 00:00:41,950 --> 00:00:43,200 So let's go ahead and 13 00:00:43,200 --> 00:00:47,970 add in a way to store and know whether Google Play Services is available or not. 14 00:00:47,970 --> 00:00:54,705 Let's just store a boolean and we're going to call it, isGooglePlayServicesAvailable. 15 00:00:56,270 --> 00:00:59,500 By default, we're just going to assume it's not available. 16 00:00:59,500 --> 00:01:03,520 So that way, we make sure we handle that case. 17 00:01:03,520 --> 00:01:06,070 So by default we're just gonna say false, and 18 00:01:06,070 --> 00:01:10,310 now this is gonna need to know when Google Play Services becomes available. 19 00:01:10,310 --> 00:01:12,310 So, in order to know that, 20 00:01:12,310 --> 00:01:16,210 we're gonna use the interface that we defined in our helper. 21 00:01:16,210 --> 00:01:19,560 So remember, let's go to our helper, and at the top we defined this interface 22 00:01:19,560 --> 00:01:25,410 called GoogleServicesListener, and it has a method on connected on disconnected and 23 00:01:25,410 --> 00:01:28,950 that's what our adapter wants to know is when it's connected and disconnected. 24 00:01:28,950 --> 00:01:31,140 So let's going ahead and have it implement that interface. 25 00:01:32,620 --> 00:01:35,130 There it is, GoogleServicesListener. 26 00:01:35,130 --> 00:01:38,880 And now we need to ahead and use Alt+Return and 27 00:01:38,880 --> 00:01:42,290 implement those methods on connected and on disconnected. 28 00:01:42,290 --> 00:01:45,188 Add those to our class, we can see it added them down here. 29 00:01:45,188 --> 00:01:50,920 And what's gonna happen when we're connected? 30 00:01:50,920 --> 00:01:55,520 Well, when we're connected, we now know, okay, Google Play Services is available. 31 00:01:55,520 --> 00:01:58,400 So first off, let's go ahead and set that value to be true. 32 00:02:00,180 --> 00:02:03,200 Okay. And then, we need to go ahead and redraw 33 00:02:03,200 --> 00:02:07,120 the items on screen, because by default we assumed that they weren't connected, so 34 00:02:07,120 --> 00:02:09,598 we're probably displaying them in a different way. 35 00:02:09,598 --> 00:02:12,510 But if they're, Google Play Services is now available, 36 00:02:12,510 --> 00:02:16,600 we wanna display the options to plus one and share to Google+. 37 00:02:16,600 --> 00:02:19,460 So, we're gonna call notifyDataSetChanged. 38 00:02:19,460 --> 00:02:23,620 And this will cause this adapter and recycler re, 39 00:02:23,620 --> 00:02:25,260 together to redraw those items. 40 00:02:25,260 --> 00:02:28,690 And the same thing is gonna happen down here at on disconnected. 41 00:02:28,690 --> 00:02:32,840 We're just going to say it's false now it's not available and 42 00:02:32,840 --> 00:02:37,690 we want to make sure that we update the recycler view items to reflect that. 43 00:02:37,690 --> 00:02:41,970 Okay and now that that's happening we can actually handle the cases where it's 44 00:02:41,970 --> 00:02:45,770 available and not available in our on bind view holder method, 45 00:02:45,770 --> 00:02:49,350 so up in our on bind view holder method, here we are. 46 00:02:49,350 --> 00:02:53,310 Right here, we're just gonna add a place where later on, 47 00:02:53,310 --> 00:02:56,710 we can check and display the UI in a different way. 48 00:02:56,710 --> 00:03:01,000 So we're just gonna put this in here and make sure we come back to it later on, but 49 00:03:01,000 --> 00:03:05,020 we'll have an opportunity to say, okay, if Google Play Services is available, we're 50 00:03:05,020 --> 00:03:09,870 gonna be able to display that +1 button once we've added it in to each item, and 51 00:03:09,870 --> 00:03:12,790 initialize it, initialize the share button, do whatever we need to do, 52 00:03:12,790 --> 00:03:16,110 and we're also gonna handle the case where it's not available, and 53 00:03:16,110 --> 00:03:18,120 that's gonna be our else case. 54 00:03:18,120 --> 00:03:22,700 Okay, and because the listening adapter is the thing that actually gets the data, 55 00:03:22,700 --> 00:03:28,750 holds our active listings object, displays the UI, let's go ahead and move the logic 56 00:03:28,750 --> 00:03:33,660 of loading, doing the load to the network, calling Etsy.getActiveListings. 57 00:03:33,660 --> 00:03:36,410 Let's move that into our adapter here. 58 00:03:36,410 --> 00:03:39,930 So in our adapter, what we're gonna do is just check, when we call to 59 00:03:39,930 --> 00:03:43,970 Google Play Services, it's either gonna come back as connected or disconnected. 60 00:03:43,970 --> 00:03:48,656 So one of these cases, that's going to be the case every time we start our activity. 61 00:03:48,656 --> 00:03:52,670 So, what we're gonna do here is just check and if it connects but 62 00:03:52,670 --> 00:03:55,800 we don't have any items yet, we can use getItemCount equals 0. 63 00:03:55,800 --> 00:03:57,382 That means we don't have any items yet. 64 00:03:57,382 --> 00:04:02,140 We're gonna go ahead and now call to getActiveListings and we're gonna pass in 65 00:04:02,140 --> 00:04:07,850 this because remember the adapter itself was the callback for retrofit. 66 00:04:07,850 --> 00:04:11,500 And we're actually gonna, gonna do the same thing in disconnected as well. 67 00:04:11,500 --> 00:04:15,660 So either way, we still wanna do the load to the API. 68 00:04:15,660 --> 00:04:18,900 We wanna call that network and get the response back. 69 00:04:18,900 --> 00:04:23,000 So, in either case, either we're connected or disconnected, but if we don't have any 70 00:04:23,000 --> 00:04:27,440 data to show anyway we need to go ahead and start a network request. 71 00:04:27,440 --> 00:04:28,370 So let's go ahead and do that. 72 00:04:29,980 --> 00:04:33,750 Okay, now that we've moved the network request into the adaptor and 73 00:04:33,750 --> 00:04:37,820 the adaptor handles when Google Play Services is available or not available. 74 00:04:37,820 --> 00:04:41,160 We need to go ahead and modify our activity to account for this. 75 00:04:41,160 --> 00:04:46,460 So, let's go to our activity and when we initialize our activity 76 00:04:46,460 --> 00:04:52,740 we were initializing a calling here into API. 77 00:04:52,740 --> 00:04:54,580 Well we don't wanna do that anymore. 78 00:04:54,580 --> 00:04:59,770 We want to now create a Google Services helper object instead and initialize it. 79 00:04:59,770 --> 00:05:02,470 So let's go ahead and add that in to our class. 80 00:05:04,810 --> 00:05:08,364 So we create our googleServicesHelper object, and 81 00:05:08,364 --> 00:05:11,360 then we're always going to initialize it. 82 00:05:11,360 --> 00:05:14,250 And we need to initialize it after we create our adapter. 83 00:05:14,250 --> 00:05:19,810 Because remember, our adapter is the listener for our googleServicesHelper. 84 00:05:19,810 --> 00:05:27,640 So after this happens we can go ahead and call through to create a new helper here. 85 00:05:29,020 --> 00:05:30,570 And the helper needs the activity. 86 00:05:30,570 --> 00:05:31,630 That's this. 87 00:05:31,630 --> 00:05:35,350 And it needs the listener which is the adapter. 88 00:05:35,350 --> 00:05:39,351 And now we can remove the call to getActiveListings, because remember, 89 00:05:39,351 --> 00:05:42,880 Google Services helper will call back to the adapters on connected or 90 00:05:42,880 --> 00:05:46,270 on disconnected, and that will start the load to the API. 91 00:05:46,270 --> 00:05:49,960 So, in either case, we don't need to do that in the activity anymore. 92 00:05:49,960 --> 00:05:52,860 All we really need to do is just show loading. 93 00:05:52,860 --> 00:05:55,150 So let's go ahead and move that in here. 94 00:05:55,150 --> 00:05:59,550 We're just gonna say, show loading by default always. 95 00:05:59,550 --> 00:06:03,610 And now, we're just gonna move this check to say, okay. 96 00:06:03,610 --> 00:06:09,892 If the savings and state was not available and, we did have some saved data. 97 00:06:09,892 --> 00:06:13,250 We're gonna go ahead and call through to success. 98 00:06:13,250 --> 00:06:14,630 And if we look at success, 99 00:06:14,630 --> 00:06:18,130 we're gonna see it, it, it's actually calling through to show the list already. 100 00:06:18,130 --> 00:06:20,260 So, we don't even, we don't even need that anymore. 101 00:06:20,260 --> 00:06:22,570 Let's go ahead and remove that as well. 102 00:06:22,570 --> 00:06:26,630 Okay, and now that we have our googleServicesHelper object created, 103 00:06:26,630 --> 00:06:31,680 we need to go ahead and connect or disconnect, and to do that, remember from 104 00:06:31,680 --> 00:06:36,770 the documentation, we need to do this in our onStart and onStop methods. 105 00:06:36,770 --> 00:06:41,510 So, let's go ahead and get those methods in here. 106 00:06:41,510 --> 00:06:46,230 There's onStop and we gotta find onStart as well, down here. 107 00:06:46,230 --> 00:06:49,830 Gonna implement those methods in our class and 108 00:06:49,830 --> 00:06:53,230 we're just gonna call through to our helper object. 109 00:06:53,230 --> 00:06:55,170 And onStop you wanna disconnect. 110 00:06:55,170 --> 00:06:58,140 And in onStart we want to connect. 111 00:06:58,140 --> 00:06:59,760 And I'm just gonna go ahead and 112 00:06:59,760 --> 00:07:03,280 arrange these in the order that they would typically be called so 113 00:07:03,280 --> 00:07:06,050 usually you'd connect first and then disconnect later on. 114 00:07:07,370 --> 00:07:12,000 Okay and finally we need to handle those cases where we called through 115 00:07:12,000 --> 00:07:16,420 to resolve an error or check availability with the googleServicesHelper. 116 00:07:16,420 --> 00:07:19,355 And when doing that, remember it would call back to the activities 117 00:07:19,355 --> 00:07:23,170 onActivityResult, so we need to add in that method as well. 118 00:07:24,390 --> 00:07:25,540 So let's add that in here. 119 00:07:28,260 --> 00:07:31,479 So onActivityResult, here, yep, and 120 00:07:31,479 --> 00:07:37,320 we're going to simply pass that through to our helpers method. 121 00:07:37,320 --> 00:07:39,100 Called handleActivityResult. 122 00:07:39,100 --> 00:07:42,960 And we just, remember we were passing in the exact same parameters 123 00:07:42,960 --> 00:07:44,050 that were coming through here. 124 00:07:46,450 --> 00:07:47,100 Perfect. 125 00:07:47,100 --> 00:07:51,220 And now, this will be able to handle those cases where 126 00:07:51,220 --> 00:07:56,220 the result of the intent came back, it was good, handle the result, reconnect, etc. 127 00:07:57,310 --> 00:08:00,247 Let's run the app and make sure everything is working before taking a break. 128 00:08:03,358 --> 00:08:07,300 So we'll run it on my emulator, and we're gonna see when it runs, oh, 129 00:08:07,300 --> 00:08:11,246 it has stopped, and that's because we can see right here that there's 130 00:08:11,246 --> 00:08:14,010 an illegal argument exception that gets thrown, 131 00:08:14,010 --> 00:08:18,218 and in our googleServicesHelper, it's telling us that we must call add API 132 00:08:18,218 --> 00:08:22,080 to add at least one API before connecting to Google Play Services. 133 00:08:22,080 --> 00:08:26,170 So, right now this isn't gonna be working, but in the next stage we're gonna add in 134 00:08:26,170 --> 00:08:29,070 the Google Plus API and we're gonna get this working. 135 00:08:29,070 --> 00:08:31,210 And we're gonna see those listings come up, and 136 00:08:31,210 --> 00:08:33,590 be able to display that plus one and Share button.