Join Albert Lo for an in-depth discussion in this video Manage the model: GooglePhotosManager, part of Android Development: Modern Android UI Design Elements.
- [Commentator] Now we are ready to create a manager to provide access to our Google photos item model. Let's go ahead and create our manager by going to our package and under our package let's select Google photos and right mouse we're going to create a new class and we will call it Google photos manager. Our Google photos manager will have a couple of methods.
The first one will be get item count. And this method is going to return the number of Google photo items. Let's also create our collection. We'll go to the top of the class and we're going to create a private list.
The list will be of type Google photos item. And we will call it Google photos item list. We will also create a constructor. Our constructor will initialize our array list. Next we will also add a get item method. Our get item method will return a Google photos item.
And it will take in a parameter of integer and we'll call it position. And we will basically invoke the get position of Google photos item list. Passing in our position. Okay, now we're going to add a method called set album list.
And set album list will take in a list of album entries. So with this method, so far we have an empty Google photos item list, set album list will populate our Google photos item list, so let's first clear out our Google photos item list because we may be calling this method multiple times. Next we're going to iterate through our album entries.
And for each album entry, from the passed in list we're going to create a Google photos item. And we will pass in our album entry. Now we have a Google photos item, we're going to add it to our list. Now we have our set album list, let's create a similar method called set photos list. And it will take in a collection of photo entries from picasa.
And we'll call the parameter photo entries. Let's go up and create a tag right at the beginning of the class. It's going to be a private static final. I'm going to call the tag Google photos manager. And call the get simple name method. Now let's go back down to set photos list.
Our set photos list method will start out similar to set albums list, so let me copy the method for clearing our collection and we'll past it in here. I'm going to declare a local variable for storing our header timestamp string. And initialize it to an empty string. Now let's add some code to iterate through our collection.
So for each item in our collection let's first add some debug logging for the date. And we'll get our date from picasa API using the get Gphoto timestamp method. Now let's declare another variable for our timestamp string. This will be a temporary variable to store the timestamp from our picasa call.
The picasa call will be entry get updated get body. Now let's add some debug logging to show our temporary timestamp. So if our header timestamp is not the same as what we read from picasa, then we're going to add a header view. So we add the logic in here, if the header's not the same as the timestamp that we just read, we will create a new Google photos item and we will add it to our collection.
And the new Google photos item will be a header type and will pass in timestamp as our constructor parameter. And let's update our header timestamp. So if this is not a header view, then we're going to do something similar to set albums list, which is to create a Google photos entry for the photo itself, so I'm just going to copy the code for that and insert it right here at line 52.
So as you can tell, Google photos manager is managing both the albums and the photos with the same collection. It's possible to have a separate manager, one for the albums and one for the photos, but because they are similar in structure, it's easier and simpler just to combine them into one single manager. You'll also notice that Google photos manager is a small class, which brings up the question as to whether it's worth the overhead of creating a separate class. As we work with recycler view adapters more and more, we will see that implementing these adapters can involve a lot of code fairly quickly.
A lot of what we do with adapters typically involves manipulating our collection. For example, if we want to search for a specific item in the collection, or delete a specific item in the collection, this is where we should be implementing those behaviors in a manager. That's why I would recommend implementing Google photos manager as a separate class like how we have done so here. Now we are finished with Google photos manager.
- Creating a custom layout with XML
- Setting up OnClick handlers
- Making API calls
- Creating custom views
- Implementing ViewHolder objects
- Signing certificates
- Adding a checkbox