You are currently viewing Implementing Pagination for fetching Instagram photos

Implementing Pagination for fetching Instagram photos

Before starting this tutorial, I recommend you to read about Pagination Library in Android.

Here, we are going to implement pagination for fetching user photos from Instagram using the MVVM pattern. Let’s start now.

1. Configuration

To configure pagination in our project use

for integrating Instagram please follow these steps.

Go to the next step when you have the required access token for Instagram.

2. Setting up network calls

  • Setting up Retrofit client

  • Creating our media model

3. Create a Repository for fetching data

We will be displaying only photos, so we need:

media_type -> to filter images/album

media_url -> to load images from URL

children -> used only in case of a carousel album. We will define the same fields for children nodes too

4. Creating a Data source

There are 3 types of Data source available. You can use any of the below Data source as per your requirement.

  • ItemKeyedDataSource -> if you need to fetch data from last fetched item id.
  • PositionalDataSource -> if you need to fetch data from a specific position/page.
  • PageKeyedDataSource -> if pages you load embed next/previous keys

We will be using PageKeyedDataSource as we get next/previous pointers in response from Graph API.

Initially, we will pass them the next cursor as null. Then the adapter will load data until the params.key or next is null.

On getting InstagramMediaModel in API response we supply the data along with previous and next keys to the callback object. In case of error, we simply pass an empty list and the next pointer as null.

getFilteredList method filters the response list and provides a single list consisting of all photos from an individual post as well as from an album post.

5. Creating a Data Source Factory

Data-loading systems of an application or library can implement this interface to allow LiveData<PagedList> to be created

6. Creating List Adapter

A simple PagedListAdapter to inflate the photo item into our layout. PagedListAdapter internally extends RecyclerView.Adapter. It takes 2 parameters:

  • data -> the type of list. Here, InstagramMediaModel.Data? is our item type,
  • ViewHolder.

We also provide a DiffUtil class. It is a utility class which is used to calculate the difference between old and new list supplied to the adapter and outputs the update operation to convert old into a new list. It uses an efficient mechanism to communicate with updates to the recycler view.

7. Creating ViewModel

This ViewModel will be used for creating the paged list. That PagedList will ask InstagramPhotosDataSource to load all the photo list and also will hold the photo item. For fetching the data it uses background executors.

To build PagedList we use:

  • DataSourceFactory -> to fetch data form,
  • PagedList configuration -> configuration of pagedList such as initial page size, size of loaded items when the list is first initialized, enable/disable placeholders, etc.

8. Presenting list inside Activity

Initializing adapter inside our activity. If we receive a null list than we can check if there is an error. If the adapter does not hold any list previously, then we can handle our error case on the view (for now, I am showing a snackbar), otherwise we submit the list to our adapter and it appends the new list to the current list.

That’s all! Now you can try this inside your app.

Suggestions are always welcome! Happy Coding!

Implementing Pagination for fetching Instagram photos was originally published in Kt. Academy on Medium, where people are continuing the conversation by highlighting and responding to this story.