Reader small image

You're reading from  Kickstart Modern Android Development with Jetpack and Kotlin

Product typeBook
Published inMay 2022
Reading LevelBeginner
PublisherPackt
ISBN-139781801811071
Edition1st Edition
Languages
Right arrow
Author (1)
Catalin Ghita
Catalin Ghita
author image
Catalin Ghita

Catalin Ghita is a Udemy Instructor and an Android Engineer proficient in Native Android Development, while also being active in Cross-platform development with React-Native and Flutter. He successfully built, deployed, and maintained huge scalable apps with millions of downloads and active users for industry giants like Burger King, Carrefour or Bankinter. He is designated to architect huge applications into scalable, maintainable and testable form and shapes. As the owner of the Coding Troops blog and Udemy instructor, he wrote articles and taught courses reaching tens of thousands of students, thereby exposing and clarifying concepts and subtleties on hot topics in Android.
Read more about Catalin Ghita

Right arrow

Chapter 11: Creating Infinite Lists with Jetpack Paging and Kotlin Flow

In the previous chapters, we built the great Restaurants App that displayed content from our own backend. However, the number of restaurants displayed in the Restaurants App was fixed, and the user was only able to browse through the few restaurants that we added to our Firebase database.

In this chapter, we will understand how pagination can help us display large datasets of items without putting pressure on our backend and without huge network bandwidth consumption. We will create the impression of an infinite list of items inside a new app that we will be working on called the Repositories App, and we will achieve that with the help of yet another Jetpack library called Paging.

In the first section, Why do we need pagination?, we will explore what data pagination is and how it can help us break large datasets into pages of data, thereby optimizing the communication between our app and the backend server...

Technical requirements

Building Compose-based Android projects for this chapter usually requires your day-to-day tools. However, to follow along smoothly, make sure you have the following:

  • The Arctic Fox 2020.3.1 version of Android Studio. You can also use a newer Android Studio version or even Canary builds but note that IDE interface and other generated code files might differ from the ones used throughout this book.
  • Kotlin 1.6.10 or newer plugin installed in Android Studio
  • The existing Repositories App from the GitHub repository of the book

The starting point for this chapter is represented by the Repositories App that you can find by navigating to the Chapter_11 directory of the GitHub repository of the book, and then by importing the repositories_app_starting_point_ch11 directory from within Android Studio. Don't worry, as we will do this together later in this chapter.

To access the solution code for this chapter, navigate to the Chapter_11 directory...

Why do we need pagination?

Let's say we have an Android application that allows you to explore GitHub repositories by displaying a list of projects. It does that by querying the GitHub REpresentational State Transfer (REST) application programming interface (API) with Retrofit and obtaining a fixed number of repositories inside the app. While the REST API serves the application with detailed information for each repository, the app only uses and displays the title and description of the repository.

Note

Don't confuse the Repository classes in our project architecture that abstract data logic with the GitHub repositories that are displayed in our Repositories App.

Now, let's imagine that this application retrieves and displays 20 repository elements. Because of this, the user will be able to scroll the content until the 20th element, and therefore will be able to visualize no more than 20 elements.

But what if we wanted to allow the user to explore more repositories...

Importing and exploring the Repositories App

The Repositories App project is a simple application that displays a list of repositories obtained from the GitHub Search API. This project is a simplified version of a Compose-based application that incorporates only a few concepts from the previous chapters as it tries to be a good candidate for implementing pagination with the Jetpack Paging library rather than being a fully-fledged sample app that applies all the concepts taught in the book.

Nevertheless, we will see how the Repositories App follows a Model-View-ViewModel (MVVM) presentation pattern, uses Retrofit to obtain data, a ViewModel class to hold state and present data, coroutines for the asynchronous (async) operation of obtaining data from the server, and Compose for the UI layer.

Let's start off by importing this project into Android Studio, as follows:

  1. Navigate to the GitHub repository page of the book, located at https://github.com/PacktPublishing/Kickstart...

Using Kotlin Flow to handle streams of data

If we want our app to support pagination in the form of an infinite list, it's clear that our existing approach of having a single one-shot request to the backend that results in one UI update will not suffice.

Let's first have a look in the following code snippet at how our RepositoriesViewModel class requests data:

class RepositoriesViewModel(
    private val restInterface: RepositoriesApiService =  [...]
) : ViewModel() {
    val repositories = mutableStateOf(emptyList<Repository>())
    init {
        viewModelScope.launch {
            repositories.value =
                restInterface.getRepositories().repos
        }
 &...

Exploring pagination with Jetpack Paging

To implement an infinite list of repositories in our Repositories App, we must find a way to request more repositories as the user scrolls through the existing list and reaches its bottom, thereby adding new elements on the fly. Instead of manually deciding when the user is approaching the bottom of the current list of repositories and then triggering a network request to get new items, we can use the Jetpack Paging library, which hides all this complexity from us.

Jetpack Paging is a library that helps us load and display pages of data from a large set of data, either through network requests or from our local data storage, thereby allowing us to save network bandwidth and optimize the usage of system resources.

In this chapter, for simplicity, we will use the Paging library to display an infinite list of repositories obtained from a network source (that is, the GitHub Search API), without involving the local cache.

Note

The Jetpack...

Summary

In this chapter, we first understood what pagination is and how pagination can be used to expose large datasets of items to users in a more efficient manner.

Then, we got to meet the Repositories App, a simple Android project where a fixed amount of GitHub repositories was displayed. At that point, we took the decision that users should be able to browse through a huge number of repositories that the GitHub Search API is exposing, so the only solution for that was to integrate paging within our app.

However, we then realized that we needed to first understand the concept of data streams in the context of pagination, so we learned a few things about Kotlin Flow and how it can be a simple solution to consume paginated content.

Then, we explored how the Jetpack Paging library is an elegant solution to adding pagination to our apps, culminating with the practical task of integrating paging in our Repositories App with the help of this library. Finally, we transformed our...

Further reading

In this chapter, we briefly covered how you can integrate Jetpack Paging into an Android application. However, in the context of pagination and Jetpack Paging, there are a couple of more advanced topics that you might end up wondering about, as outlined here:

  • Having both a local and remote source for paginated content—For such a case, you will need a component that manages communication between the two data sources. For this task, you could use the built-in RemoteMediator API of the Paging library. You can learn more about it from its official documentation at https://developer.android.com/topic/libraries/architecture/paging/v3-network-db#implement-remotemediator.
  • Adding support for content refresh or invalidation—If you're looking to support pull-to-refresh functionality, or you're interested in making sure that the user is returned to the appropriate page upon various system events that could restart the paginated content, you need...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Kickstart Modern Android Development with Jetpack and Kotlin
Published in: May 2022Publisher: PacktISBN-13: 9781801811071
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Catalin Ghita

Catalin Ghita is a Udemy Instructor and an Android Engineer proficient in Native Android Development, while also being active in Cross-platform development with React-Native and Flutter. He successfully built, deployed, and maintained huge scalable apps with millions of downloads and active users for industry giants like Burger King, Carrefour or Bankinter. He is designated to architect huge applications into scalable, maintainable and testable form and shapes. As the owner of the Coding Troops blog and Udemy instructor, he wrote articles and taught courses reaching tens of thousands of students, thereby exposing and clarifying concepts and subtleties on hot topics in Android.
Read more about Catalin Ghita