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