Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Android + Firestore + Flow + Pagination = ❤

 

 
Torkel Velure
Android dev @shortcut.io
Published: March 01, 2021
Tweet
Share
 

 

I’ve been working on this app recently: Fjelltoppjakten

In Fjelltoppjakten we use Firebase/Firestore for everything!

The app is simple; you can view all the mountains in Bergen(Norway), and if you reach the summit, you can check in and gain points!

After gaining hundreds of users and 50+ check-ins daily, it wasn’t feasible anymore to fetch all users and check ins immediately, nor was it a good solution to only fetch/show a few of them.

 

Image for post

 

We had to implement some kind of pagination.

Our previous implementation used a callback flow and a snapshot listener:

 

 

It returns a flow of a SnapshotListener for a query, works great if you just want to listen to few documents!

And to get all the top users we would do something like this this:

 

 

Given that we have thousands of potential documents, we don’t want to read them all at once, that could be costly. We want to paginate the query!

In addition to adding pagination, we don’t want to change the other code too much, our repository should still return a flow, keeping our functional approach!

By reading the firebase documentation we can see that they recommend using query cursors, namely startAt and startAfter.

 

Adding pagination

First we’ll listen to our recycler view and add the last visible item index to a MutableStateFlow.

In our viewModel we add:

 

val lastVisibleItem = MutableStateFlow<Int>(0)

 

And in our fragment we add (feel free to make a binding adapter instead!):

 

 

The above code updates lastVisibleItem whenever the last visible item index in the recycler view is larger than the last seen one. (Simply, it will be incremented until the user has scrolled to the bottom of the recycler view)

Using the lastVisibleItem flow, we could rewrite our flow like this:

 

 

Here we immediately fetch the top 25 users and store them in a mutable list, then we map them to local entities and emit them. Further we listen to lastVisibleItem with a transform, and whenever it is updated we check if it is equal to the size of the list of documents we currently have, and if it is, we fetch the next 25 users by using startAfter with the last document we previously fetched. We then add the newly fetched documents to our list and emit the whole list of documents.

This works perfectly! However, we have many queries, and we don’t want to write that much code every time..

We can write a generic pagination function like this:

 

 

And then our paginated users query will be as simple as:

 

 

And in our viewModel we just write:

 

 

Now leaders is a paginated flow that will update/grow whenever the user scrolls to the bottom of our recycler view. We just bind the leaders flow to our recycler view, and there is nothing more to worry about!

Now we can update all our other queries as well. Just add a lastVisibleItem flow and add .paginate(lastVisibleItem) to our queries instead of .limit(x) 👏

While using this pagination implementation we won’t get real-time updates like we did with the snapshotlistener, but that’s a trade-off we can live with.

 

 

 

Tags: Flow, Android, Firebase, Firestore, Pagination

 

View original article at: 


 

Originally published: February 12, 2021

Android News
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03

By Carlos Mota

It’s Monday, no releases this week, and… there’s a new version of Jetpack Compose — beta 03—available. What a perfect time to just increment 02 to 03 and see what’s new. The API is (almost) final so after updating from alpha to beta there weren’t any big changes to do. However, and remember that’s still in development, there’s always something that I need to update. 

By ProAndroidDev -
Android News
Noisy Code 🗣 With Kotlin Scopes
Noisy Code 🗣 With Kotlin Scopes

By Chetan Gupta

Scopes make your code more readable? think again... You are going to encounter these scope functions namely let, run, apply, also, within every Kotlin codebase, along with all the mischievous ways developers exploit their usage from the way they were intended for. Let see how popular opinion on those ends up just as a code noise.

By ProAndroidDev -
Android News
Improving Android DataBinding with Bindables library
Improving Android DataBinding with Bindables library

By Jaewoong Eum

DataBinding is one of the most important factors for MVVM architecture. The basic concept of DataBinding is to link the view and view model via observer patterns, properties, event callbacks, etc. Linking and automating communication between the view via the bound properties or something in the view model has a lot of benefits in the MVVM architecture concept.

By ProAndroidDev -
Android News
KMM QuickStart Guide
KMM QuickStart Guide

By Mayank Kharbanda

Kotlin Multiplatform (KMP) is a code-sharing technology that allows us to use the same code for different platforms whether it’s JVM, Android, iOS, watchOS, tvOS, Web, Desktop, or WebAssembly. In this article, we will use Kotlin Multiplatform Mobile (KMM) which is a subset of KMP with the focus on providing better tooling and support for sharing code on mobile platforms i.e. Android and iOS.

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One