Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Integrating GoogleServicesHelper8:34 with Jamie Huson
Integrate the Helper class with the UI code using Activity and Adapter
Google Play Services encompasses a lot of APIs, 0:00 and we needed to handle many edge cases to ensure our app doesn't crash. 0:03 By building a helper class we've simplified the interaction with 0:09 Google Play Services library, making it easier to reuse in future apps. 0:12 Now that our code is connecting to Google Play Services, 0:18 we'll be able to start using those APIs. 0:21 Let's handle the adapter first. 0:24 Remember the adapter is what actually creates our views and 0:26 displays them on the screen, and in on bind view holder, we can hide or 0:29 show views as we need to, so this is what's really going to 0:34 care about whether Google Play Services is available or is not available. 0:37 So let's go ahead and 0:41 add in a way to store and know whether Google Play Services is available or not. 0:43 Let's just store a boolean and we're going to call it, isGooglePlayServicesAvailable. 0:47 By default, we're just going to assume it's not available. 0:56 So that way, we make sure we handle that case. 0:59 So by default we're just gonna say false, and 1:03 now this is gonna need to know when Google Play Services becomes available. 1:06 So, in order to know that, 1:10 we're gonna use the interface that we defined in our helper. 1:12 So remember, let's go to our helper, and at the top we defined this interface 1:16 called GoogleServicesListener, and it has a method on connected on disconnected and 1:19 that's what our adapter wants to know is when it's connected and disconnected. 1:25 So let's going ahead and have it implement that interface. 1:28 There it is, GoogleServicesListener. 1:32 And now we need to ahead and use Alt+Return and 1:35 implement those methods on connected and on disconnected. 1:38 Add those to our class, we can see it added them down here. 1:42 And what's gonna happen when we're connected? 1:45 Well, when we're connected, we now know, okay, Google Play Services is available. 1:50 So first off, let's go ahead and set that value to be true. 1:55 Okay. And then, we need to go ahead and redraw 2:00 the items on screen, because by default we assumed that they weren't connected, so 2:03 we're probably displaying them in a different way. 2:07 But if they're, Google Play Services is now available, 2:09 we wanna display the options to plus one and share to Google+. 2:12 So, we're gonna call notifyDataSetChanged. 2:16 And this will cause this adapter and recycler re, 2:19 together to redraw those items. 2:23 And the same thing is gonna happen down here at on disconnected. 2:25 We're just going to say it's false now it's not available and 2:28 we want to make sure that we update the recycler view items to reflect that. 2:32 Okay and now that that's happening we can actually handle the cases where it's 2:37 available and not available in our on bind view holder method, 2:41 so up in our on bind view holder method, here we are. 2:45 Right here, we're just gonna add a place where later on, 2:49 we can check and display the UI in a different way. 2:53 So we're just gonna put this in here and make sure we come back to it later on, but 2:56 we'll have an opportunity to say, okay, if Google Play Services is available, we're 3:01 gonna be able to display that +1 button once we've added it in to each item, and 3:05 initialize it, initialize the share button, do whatever we need to do, 3:09 and we're also gonna handle the case where it's not available, and 3:12 that's gonna be our else case. 3:16 Okay, and because the listening adapter is the thing that actually gets the data, 3:18 holds our active listings object, displays the UI, let's go ahead and move the logic 3:22 of loading, doing the load to the network, calling Etsy.getActiveListings. 3:28 Let's move that into our adapter here. 3:33 So in our adapter, what we're gonna do is just check, when we call to 3:36 Google Play Services, it's either gonna come back as connected or disconnected. 3:39 So one of these cases, that's going to be the case every time we start our activity. 3:43 So, what we're gonna do here is just check and if it connects but 3:48 we don't have any items yet, we can use getItemCount equals 0. 3:52 That means we don't have any items yet. 3:55 We're gonna go ahead and now call to getActiveListings and we're gonna pass in 3:57 this because remember the adapter itself was the callback for retrofit. 4:02 And we're actually gonna, gonna do the same thing in disconnected as well. 4:07 So either way, we still wanna do the load to the API. 4:11 We wanna call that network and get the response back. 4:15 So, in either case, either we're connected or disconnected, but if we don't have any 4:18 data to show anyway we need to go ahead and start a network request. 4:23 So let's go ahead and do that. 4:27 Okay, now that we've moved the network request into the adaptor and 4:29 the adaptor handles when Google Play Services is available or not available. 4:33 We need to go ahead and modify our activity to account for this. 4:37 So, let's go to our activity and when we initialize our activity 4:41 we were initializing a calling here into API. 4:46 Well we don't wanna do that anymore. 4:52 We want to now create a Google Services helper object instead and initialize it. 4:54 So let's go ahead and add that in to our class. 4:59 So we create our googleServicesHelper object, and 5:04 then we're always going to initialize it. 5:08 And we need to initialize it after we create our adapter. 5:11 Because remember, our adapter is the listener for our googleServicesHelper. 5:14 So after this happens we can go ahead and call through to create a new helper here. 5:19 And the helper needs the activity. 5:29 That's this. 5:30 And it needs the listener which is the adapter. 5:31 And now we can remove the call to getActiveListings, because remember, 5:35 Google Services helper will call back to the adapters on connected or 5:39 on disconnected, and that will start the load to the API. 5:42 So, in either case, we don't need to do that in the activity anymore. 5:46 All we really need to do is just show loading. 5:49 So let's go ahead and move that in here. 5:52 We're just gonna say, show loading by default always. 5:55 And now, we're just gonna move this check to say, okay. 5:59 If the savings and state was not available and, we did have some saved data. 6:03 We're gonna go ahead and call through to success. 6:09 And if we look at success, 6:13 we're gonna see it, it, it's actually calling through to show the list already. 6:14 So, we don't even, we don't even need that anymore. 6:18 Let's go ahead and remove that as well. 6:20 Okay, and now that we have our googleServicesHelper object created, 6:22 we need to go ahead and connect or disconnect, and to do that, remember from 6:26 the documentation, we need to do this in our onStart and onStop methods. 6:31 So, let's go ahead and get those methods in here. 6:36 There's onStop and we gotta find onStart as well, down here. 6:41 Gonna implement those methods in our class and 6:46 we're just gonna call through to our helper object. 6:49 And onStop you wanna disconnect. 6:53 And in onStart we want to connect. 6:55 And I'm just gonna go ahead and 6:58 arrange these in the order that they would typically be called so 6:59 usually you'd connect first and then disconnect later on. 7:03 Okay and finally we need to handle those cases where we called through 7:07 to resolve an error or check availability with the googleServicesHelper. 7:12 And when doing that, remember it would call back to the activities 7:16 onActivityResult, so we need to add in that method as well. 7:19 So let's add that in here. 7:24 So onActivityResult, here, yep, and 7:28 we're going to simply pass that through to our helpers method. 7:31 Called handleActivityResult. 7:37 And we just, remember we were passing in the exact same parameters 7:39 that were coming through here. 7:42 Perfect. 7:46 And now, this will be able to handle those cases where 7:47 the result of the intent came back, it was good, handle the result, reconnect, etc. 7:51 Let's run the app and make sure everything is working before taking a break. 7:57 So we'll run it on my emulator, and we're gonna see when it runs, oh, 8:03 it has stopped, and that's because we can see right here that there's 8:07 an illegal argument exception that gets thrown, 8:11 and in our googleServicesHelper, it's telling us that we must call add API 8:14 to add at least one API before connecting to Google Play Services. 8:18 So, right now this isn't gonna be working, but in the next stage we're gonna add in 8:22 the Google Plus API and we're gonna get this working. 8:26 And we're gonna see those listings come up, and 8:29 be able to display that plus one and Share button. 8:31
You need to sign up for Treehouse in order to download course files.Sign up