Reader small image

You're reading from  Modern Android 13 Development Cookbook

Product typeBook
Published inJul 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781803235578
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Madona S. Wambua
Madona S. Wambua
author image
Madona S. Wambua

Madona S. Wambua is a Google Developer Expert in the Android category, an Android Engineer programming in Kotlin, and the founder and Chief Technology Officer of Jibu Labs. She is also a Women Tech Makers Ambassador and Co-Chair of AnitaB. She has over ten years of experience in the field and has worked on consumer-facing applications and building software development kits for developers. She has also worked on the famous Google Glass during her tenure at a startup and got an opportunity to work on interactive AR videos to transform lives through machine learning and computer vision. Madona is also a Keynote Speaker and the host of Tech Talks with Madona, a podcast geared towards supporting women and allies in tech.
Read more about Madona S. Wambua

Right arrow

Getting Started with Paging

In Android development, the Paging library helps developers load and display data pages from a larger dataset from local storage or over a network. This can be a common case if your application loads considerable amounts of data for people to read. For instance, a good example is Twitter; you might notice the data refreshes due to the many tweets that people send daily.

Hence, in Modern Android Development (MAD), Android developers might want to implement the Paging library in their applications to help them with such instances when loading data. In this chapter, you will learn how to utilize the Paging library in your projects.

In this chapter, we’ll cover the following recipes:

  • Implementing the Jetpack Paging library
  • Managing present and loading states
  • Implementing your custom pagination in Jetpack Compose
  • Loading and displaying paged data
  • Understanding how to transform data streams
  • Migrating to Paging 3 and understanding...

Technical requirements

The complete source code for this chapter can be found at https://github.com/PacktPublishing/Modern-Android-13-Development-Cookbook/tree/main/chapter_eight. You will also need to get an API key for https://newsapi.org/. NewsApi is a worldwide API for news.

Implementing the Jetpack Paging library

The Paging library comes with incredible features for developers. If your codebase is established and extensive, there are other custom ways that developers have created to help them load data efficiently. One notable advantage of Paging is its in-memory caching for your page’s data, which ensures your application uses the system resources efficiently while working with the already paged data.

In addition, it offers support for Kotlin coroutine flows and LiveData and has built-in deduplication, which ensures your application uses network bandwidth and resources efficiently, which can help save battery. Finally, the Paging library offers support for error handling, including when refreshing and retrying your data.

Getting ready

In this recipe, we will need to create a new project; if you need to reference a previous recipe for creating a new project, you can visit Chapter 1, Getting Started with Modern Android Development Skills...

Managing present and loading states

The Paging library offers the loading state information to users through its load state object, which can have different forms based on its current loading state. For example, if you have an active load, then the state will be LoadState.Loading.

If you have an error state, then the state will be a LoadState.Error; and finally, there might be no active load operation, and this state is called the LoadState.NotLoading. In this recipe, we will explore the different states and get to understand them; the example demonstrated here can also be found at the following link: https://developer.android.com/topic/libraries/architecture/paging/load-state. In this example, we assume your project uses legacy code, which utilizes XML for the view system.

Getting ready

To follow along with this recipe, you need to have completed the code in the previous recipe. You can also skip this if it is not required in your project.

How to do it…

We will...

Implementing your custom pagination in Jetpack Compose

The Paging library has incredible features for developers, but sometimes you encounter challenges and are forced to create custom pagination. At the beginning of the chapter, we talked about complex code bases having or creating pagination.

In this recipe, we will look into how we can achieve this with a simple list example and how you can use this example to create custom pagination in your application.

Getting ready

In this recipe, we will need to create a new project and call it CustomPagingExample.

How to do it…

In our example project, we will try to create a student profile card and use custom pagination to load the profiles in Jetpack Compose.

  1. For this recipe, let us go ahead and add the lifecycle-ViewModel dependency since we will need it:
    implementation "Androidx.lifecycle:lifecycle-viewmodel-compose:2.x.x"
  2. Let’s go ahead and create a new package and call it data. In our...

Loading and displaying paged data

There are essential steps to consider when loading and displaying paged data. In addition, the Paging library provides tremendous advantages for loading and displaying large, paged datasets. A few steps you must have in mind is ensuring you first define a data source, your Paging Source set up streams if needed, and more.

In this recipe, we will look at how loading and displaying paged data works.

How to do it…

You need to have completed the Implementing the Jetpack Paging library recipe to be able to follow along with the explanation of this recipe:

  1. You might have noticed in our first recipe that we override load(), a method that we use to indicate how we retrieve the paged data from our corresponding data source:
    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, NewsArticle> {
        return try {
            val page = params.key ?: 1
      ...

Understanding how to transform data streams

When writing any code dealing with Paging, you need to understand how you can transform the data stream as you load it to your users. For instance, you may need to filter a list of items or even convert the items to a different type before you can feed the UI with the data.

Hence, ensuring you apply transformation directly to the stream data lets you keep your repository and UI logic separated cleanly. In this recipe, we will try to understand how we can transform data streams.

Getting ready

To follow along, you must be familiar with the primary usage of the Paging library; hence make sure you have read the previous recipes in this chapter.

How to do it…

In this recipe, we will perform the following steps:

  1. Look into how we can apply the essential transformation.
  2. Convert and filter the data.
  3. Handle separators in the UI and convert the UI model.

The recipe is helpful to you if you are already using...

Migrating to Paging 3 and understanding the benefits

You might be using the old Paging version, in this case, Paging 2 or 1, and you might be required to migrate to utilize the benefits Paging 3 offers. Paging 3 offers enhanced functionality and ensures it addresses the most common challenges people experience using Paging 2.

In this recipe, we will look into how you can migrate to the latest recommended Paging library.

Getting ready

If your application is already using Paging 3, then you can skip this recipe; this step-by-step migration guide is intended for users currently using the older versions of the Paging library.

How to do it…

Migrating from old versions of the Paging library might seem complex due to the fact that each application is unique, and complexities might vary. In our example, however, we will touch on a low-level kind of migration since our example application does not need any migration.

To perform migration from old Paging libraries, follow...

Writing tests for your Paging Source

Writing tests for your implementations is crucial. We will write unit tests for our PagingSource implementation in this recipe to test our logic. Some tests that might be worth writing are checking when news Paging load failure happens.

We can also test the success state and more. You can follow the pattern to write tests for your project or use case.

Getting ready

To follow this recipe step by step, you need to have followed the Implementing the Jetpack Paging library recipe, and you need to use the PagingJetpackExample project.

How to do it…

Open PagingJetpackExample and follow along with this project to add unit tests:

  1. Add the following testing libraries to your build.gradle app:
    testImplementation 'org.assertj:assertj-core:3.x.x'
    testImplementation "org.mockito:mockito-core:3.x.x"
    testImplementation 'Androidx.arch.core:core-testing:2.x.x'
    testImplementation 'org.jetbrains.kotlinx:kotlinx...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Modern Android 13 Development Cookbook
Published in: Jul 2023Publisher: PacktISBN-13: 9781803235578
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 €14.99/month. Cancel anytime

Author (1)

author image
Madona S. Wambua

Madona S. Wambua is a Google Developer Expert in the Android category, an Android Engineer programming in Kotlin, and the founder and Chief Technology Officer of Jibu Labs. She is also a Women Tech Makers Ambassador and Co-Chair of AnitaB. She has over ten years of experience in the field and has worked on consumer-facing applications and building software development kits for developers. She has also worked on the famous Google Glass during her tenure at a startup and got an opportunity to work on interactive AR videos to transform lives through machine learning and computer vision. Madona is also a Keynote Speaker and the host of Tech Talks with Madona, a podcast geared towards supporting women and allies in tech.
Read more about Madona S. Wambua