Android Kotlin paged endpoints made easy

Overview

Fountain

Release License: MIT codebeat badge

A smart and simple way to work with paged endpoints. To see an example of how to use it, check out the introducing Fountain posts: part one and part two.

Atention: since all of Fountain functionalities are present in android official Paging3 library, Fountain is deprecated and we recommend using that tool.

Overview

Fountain is an Android Kotlin library conceived to make your life easier when dealing with paged endpoint services, where the paging is based on incremental page numbers (e.g. 1, 2, 3, ...). It uses the Google Android Architecture Components, mainly the Android Paging Library to make it easier to work with paged services.

The main goal of the library is to easily provide a Listing component from a common service specification. Listing provides essentially five elements to take control of the paged list:

data class Listing<T>(
    val pagedList: LiveData<PagedList<T>>,
    val networkState: LiveData<NetworkState>,
    val refreshState: LiveData<NetworkState>,
    val refresh: () -> Unit,
    val retry: () -> Unit
)
  1. pagedList: A changing data stream of type T represented as a LiveData of a PagedList.
  2. networkState: A stream that notifies network state changes, such as when a new page started loading (so you can show a spinner in the UI).
  3. refresh: A refresh function, to refresh all data.
  4. refreshState: A stream that notifies the status of the refresh request.
  5. retry: A retry function to execute if something failed.

Basically, you could manage all data streams with a Listing component, which is awesome! It's really flexible and useful to display the paged list entities and reflect the network status changes in the UI.

This library was designed to work with paged endpoints. However, it also supports working with not paged endpoints. That means that you can use all Listing features in services that return a list that's not paged.

Fountain provides two ways to generate a Listing component from paged services:

  1. Network support: Provides a Listing based on a common Retrofit service implementation. Note entities won't be saved in memory nor disk.
  2. Cache + Network support: Provides a Listing with cache support using a common Retrofit service implementation, and a DataSource for caching the data. We recommend you use Room to provide the DataSource, because it will be easier. However, you could use any other DataSource.

Fountain supports 2 types of Retrofit service adapters:

It also supports not using any of them, as you could work with a simple Retrofit call.

Download

Add library to project dependencies with JitPack.

repositories {
    maven { url "https://jitpack.io" }
}

dependencies {
    // This dependency is required only if you want to use a Retrofit service without a special adapter. 
    implementation 'com.github.xmartlabs.fountain:fountain-retrofit:0.5.0'

    // This dependency is required only if you want to use a Coroutine retrofit adapter.
    implementation 'com.github.xmartlabs.fountain:fountain-coroutines:0.5.0'

    // This dependency is required only if you want to use a RxJava2 retrofit adapter.
    implementation 'com.github.xmartlabs.fountain:fountain-rx2:0.5.0'
}

Fountain is using Kotlin 1.3, if you are using Kotlin 1.2.X, please use Fountain 0.4.0.

Despite Fountain is in experimental state, we believe the API won't receive major changes.

Usage

You can read the full documentation.

Factory constructors

There's one static factory object class for each each dependency.

  • FountainCoroutines: Used to get a Listing from a Retrofit service which uses a Coroutine adapter.
  • FountainRetrofit: Used to get a Listing from a Retrofit service without using a special adapter.
  • FountainRx: Used to get a Listing from a Retrofit service which uses a RxJava2 adapter.

Each static factory has the same constructors with different params:

Network support

Provides a Listing based on a common Retrofit service implementation. In this mode the fetched entities are volatile, they won't be saved in memory nor disk.

Network support for paged endpoints

The Listing with network support for paged endpoints can be obtained invoking createNetworkListing from the static factory class.

It requires only one argument, a NetworkDataSourceAdapter, which provides all operations that the library will use to handle the paging.

The NetworkDataSourceAdapter contains two main functions: a method to check if a page can be fetched and a property to fetch it.

interface NetworkDataSourceAdapter<PageFetcher> {
  val pageFetcher: PageFetcher

  @CheckResult
  fun canFetch(page: Int, pageSize: Int): Boolean
}

PageFetcher is a structure that provides a way to fetch a page from a service call. There is one page fetcher per library adapter, we will refer to any of them as PageFetcher throughout the documentation.

interface RetrofitPageFetcher<T : ListResponse<*>> {
  fun fetchPage(page: Int, pageSize: Int): Call<T>
}

interface CoroutinePageFetcher<T : ListResponse<*>> {
  fun fetchPage(page: Int, pageSize: Int): Deferred<T>
}

interface RxPageFetcher<T : ListResponse<*>> {
  fun fetchPage(page: Int, pageSize: Int): Single<T>
}

Network support for not paged endpoints

The Listing with network support for not paged endpoints can be obtained invoking createNetworkListing from the static factory class.

It requires only one argument, a NotPagedPageFetcher, which provides a method to fetch the data from a service source.

There is one NotPagedPageFetcher per library adapter, we will refer to any of them as NotPagedPageFetcher throughout the documentation.

interface NotPagedRetrifitPageFetcher<T : ListResponse<*>> {
  fun fetchData(): Call<T>
}

interface NotPagedCoroutinePageFetcher<T : ListResponse<*>> {
  fun fetchData(): Deferred<T>
}

interface NotPagedRxPageFetcher<T : ListResponse<*>> {
  fun fetchData(): Single<T>
}
List Responses

The library defines a common service response type which is used to fetch the pages.

interface ListResponse<T> {
  fun getElements(): List<T>
}

Additionally, there are other response types that can be used when the service provides more information in the response.

ListResponseWithEntityCount: Used when the service provides the amount of entities.

interface ListResponseWithEntityCount<T> : ListResponse<T> {
  fun getEntityCount() : Long
}

ListResponseWithPageCount: Used when the service provides the amount of pages.

interface ListResponseWithPageCount<T> : ListResponse<T> {
  fun getPageCount(): Long
}

If you use either ListResponseWithPageCount or ListResponseWithEntityCount you can convert a PageFetcher to a NetworkDataSourceAdapter. That means that if the response has the number of pages or entities you can get a NetworkDataSourceAdapter without implementing the canFetch method.

To do that Fountain provides some extensions:

fun <ServiceResponse : ListResponseWithEntityCount<*>>
    PageFetcher<ServiceResponse>.toTotalEntityCountNetworkDataSourceAdapter(firstPage: Int)
fun <ServiceResponse : ListResponseWithPageCount<*>>
    PageFetcher<ServiceResponse>.toTotalPageCountNetworkDataSourceAdapter(firstPage: Int)

Cache + Network support

Provides a Listing with cache support using a common Retrofit service implementation, and a DataSource for caching the data.

Cache + Network support for paged endpoints

The Listing with network and cache support for paged endpoints can be obtained invoking createNetworkWithCacheSupportListing from the static factory class.

It has two required components:

  1. A NetworkDataSourceAdapter<out ListResponse<NetworkValue>> to fetch all pages.
  2. A CachedDataSourceAdapter<NetworkValue, DataSourceValue> to update the DataSource. It's the interface that the library will use to take control of the DataSource.

Cache + Network support for not paged endpoints

The Listing with network and cache support for not paged endpoints can be obtained invoking createNotPagedNetworkWithCacheSupportListing from the static factory class.

It has two required components:

  1. A NotPagedPageFetcher<out ListResponse<NetworkValue>> to fetch the data.
  2. A CachedDataSourceAdapter<NetworkValue, DataSourceValue> to update the DataSource. It's the interface that the library will use to take control of the DataSource.

CachedDataSourceAdapter

interface CachedDataSourceAdapter<NetworkValue, DataSourceValue> {
  fun getDataSourceFactory(): DataSource.Factory<*, DataSourceValue>

  @WorkerThread
  fun saveEntities(response: List<NetworkValue>)

  @WorkerThread
  fun dropEntities()

  @WorkerThread
  fun runInTransaction(transaction: () -> Unit)
}

It has only four methods that you should implement:

  • getDataSourceFactory will be used to list the cached elements. The returned value is used to create the LivePagedListBuilder.
  • runInTransaction will be used to apply multiple DataSource operations in a single transaction. That means that if something fails, all operations will fail.
  • saveEntities will be invoked to save all entities into the DataSource. This will be executed in a transaction.
  • dropEntities will be used to delete all cached entities from the DataSource. This will be executed in a transaction.

Caching strategy

The pagination strategy that Fountain is using can be seen in the following image:

It starts with an initial service data request. By default the initial data requested is three pages, but this value can be changed, in the PagedList.Config, using the setInitialLoadSizeHint method. This parameter can be set in the factory constructor method. When the service data comes from the service, all data is refreshed in the DataSource using the CachedDataSourceAdapter. Note that the Listing component will notify that data changed.

After that, the Android Paging Library will require pages when the local data is running low. When a new page is required, the paging library will invoke a new service call, and will use the CachedDataSourceAdapter to save the returned data into the DataSource.

Architecture recommendations

It's strongly recommended to integrate this component in a MVVM architecture combined with the Repository Pattern. The Listing component should be provided by the repository. The ViewModel, can use the different Listing elements, provided by the repository, to show the data and the network changes in the UI.

You can take a look at the example project to see how everything comes together.

Getting involved

  • If you want to contribute please feel free to submit pull requests.
  • If you have a feature request please open an issue.
  • If you found a bug check older issues before submitting a new one.
  • If you need help or would like to ask a general question, use StackOverflow. (Tag fountain).

Before contributing, please check the CONTRIBUTING file.

Changelog

The changelog for this project can be found in the CHANGELOG file.

About

Made with ❤️ by XMARTLABS

You might also like...
Filmesflix - Project made during the NTT DATA Android Developer bootcamp. Developing knowledge in MVVM and Clear Architecture
Filmesflix - Project made during the NTT DATA Android Developer bootcamp. Developing knowledge in MVVM and Clear Architecture

FilmesFlix Projeto criado para o módulo de MVVM e Clean Architecture no Bootcamp

:octocat: Navigation toolbar is a slide-modeled UI navigation controller made by @Ramotion
:octocat: Navigation toolbar is a slide-modeled UI navigation controller made by @Ramotion

NAVIGATION TOOLBAR Navigation toolbar is a Kotlin slide-modeled UI navigation controller. We specialize in the designing and coding of custom UI for M

 Utility for developers and QAs what helps minimize time wasting on writing the same data for testing over and over again. Made by Stfalcon
Utility for developers and QAs what helps minimize time wasting on writing the same data for testing over and over again. Made by Stfalcon

Stfalcon Fixturer A Utility for developers and QAs which helps minimize time wasting on writing the same data for testing over and over again. You can

HowsTheWeather - 🌄 Your typical weather app, made really simple.

How's The Weather This is a simple Android app made with the purpose of learning how to access external APIs. It displays the data fetched from this A

An advanced Minecraft plugin template made in Gradle

//DONT_APPLY_TEMPLATE_CHANGE Gradle Plugin Template Kotlin DSL Version ℹ️ This template was planned to support only kotlin, but it also supports Java!

A personal project made using Jetpack Compose, Clean-MVVM architecture and other jetpack libraries

A basic CRUD for recording your personal credit and debit transactions. Made using Jetpack Compose, Clean-MVVM and other Jetpack libraries, incorporated with Unit Tests.

Educational App made with Retrofit, Coroutines, Navigation Component, Room, Dagger Hilt, Flow & Material Motion Animations.
Educational App made with Retrofit, Coroutines, Navigation Component, Room, Dagger Hilt, Flow & Material Motion Animations.

TechHub TechHub is a sample educational app that provides courses for people who want to learn new skills in mostly tech-related areas. The goal of th

Easy Android logging with Kotlin and Timber

Kotlin logging extensions for Timber Jake Wharton's Timber library is great. It's a Java library with an API that works well for Java, but that isn't

Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties
Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties

Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties Idea Delegated properties in Kotlin allow you to execute a

Comments
  • Support ItemKeyedDataSource style paging

    Support ItemKeyedDataSource style paging

    This might be an issue with my understanding of the library but I'm struggling to see how I can use it to page an endpoint that doesn't use page numbers. Take Reddit for example, if I want a Listing I have to provide the id of the last element in the previous Listing.

    Currently I only have access to fun fetchPage(page: Int, pageSize: Int): Deferred<T> which doesn't provide enough information for me to request the next page. Could you add support for a PageFetcher that provides the item given in the onItemAtEndLoaded callback?

    feature request 
    opened by ScottCooper92 3
  • fetchPage() skip 2nd page and 3rd page (5.0)

    fetchPage() skip 2nd page and 3rd page (5.0)

    Hi, I think I just found out that your library has a bug, here is what I've found, as you can see the page number start at 1 and then it skips ONLY the second page and the third page. it happened to my project and your example app. Additionally information, I've tried to use ListResponseWithPageCount and ListResponseWithEntityCount as a response type but none of them are worked. exampleapp myproject Thanks

    opened by AldiSatrioAji 1
  • Upgrade Coroutines to stable version

    Upgrade Coroutines to stable version

    First of all, great library, it's gonna come in useful for my app so keep up the good work.

    Now that Coroutines are in a stable state in Kotlin 1.3.0 it's probably worth updating from the experimental version. I'm assuming it's more than just updating the version number which is why I haven't attempted a PR myself.

    opened by ScottCooper92 1
  • Fixed detekt configuration to work in windows

    Fixed detekt configuration to work in windows

    Fixed detekt configuration to work in windows:

    Related issue:

    https://stackoverflow.com/questions/48632019/gradle-build-error-on-windows-failed-to-create-md5-hash-for-file

    opened by f7deleon 0
Releases(0.5.0)
  • 0.5.0(Nov 16, 2018)

  • 0.4.0(Oct 23, 2018)

    Big Breaking changes:

    Fountain modules

    The library now has 3 different modules:

    • Coroutine module: Uses a Coroutine Retrofit adapter to fetch the pages.
    • Retrofit module: Uses a simple Retrofit call to fetch the pages.
    • RxJava2 module: Uses a RxJava2 Retrofit adapter to fetch the pages.

    These modules are independent and none of them are strictly required. The new dependencies are:

    repositories {
        maven { url "https://jitpack.io" }
    }
    
    dependencies {
        // This dependency is required only if you want to use a Retrofit service without a special adapter. 
        implementation 'com.github.xmartlabs.fountain:fountain-retrofit:0.4.0'
    
        // This dependency is required only if you want to use a Coroutine retrofit adapter.
        implementation 'com.github.xmartlabs.fountain:fountain-coroutines:0.4.0'
    
        // This dependency is required only if you want to use a RxJava2 retrofit adapter.
        implementation 'com.github.xmartlabs.fountain:fountain-rx2:0.4.0'
    }
    

    Not paged endpoint support

    Although Fountain was designed to work with paged endpoints, it also supports working with not paged endpoints. That means that you can use all [Listing] features in services that return a list that's not paged.

    Factory constructors

    There's one static factory object class for each each dependency.

    • FountainCoroutines: Used to get a [Listing] from a Retrofit service which uses a Coroutine adapter.
    • FountainRetrofit: Used to get a [Listing] from a Retrofit service without using a special adapter.
    • FountainRx: Used to get a [Listing] from a Retrofit service which uses a RxJava2 adapter.

    Each static factory has the same constructors with different params:

    • createNetworkListing: A constructor to get a Listing component from a common paged Retrofit service implementation.
    • createNotPagedNetworkListing: A constructor to get a Listing component from a common not paged Retrofit service implementation.
    • createNetworkWithCacheSupportListing: A constructor to get a Listing component with cache support from a common paged Retrofit service implementation.
    • createNotPagedNetworkWithCacheSupportListing: A constructor to get a Listing component with cache support from a common not paged Retrofit service implementation.

    NetworkDataSourceAdapter creators

    If you use either ListResponseWithPageCount or ListResponseWithEntityCount you can convert a PageFetcher to a NetworkDataSourceAdapter using these extensions:

    fun <ServiceResponse : ListResponseWithEntityCount<*>>
        PageFetcher<ServiceResponse>.toTotalEntityCountNetworkDataSourceAdapter(firstPage: Int)
    fun <ServiceResponse : ListResponseWithPageCount<*>>
        PageFetcher<ServiceResponse>.toTotalPageCountNetworkDataSourceAdapter(firstPage: Int)
    

    To more about these changes you can read the full documentation here.

    Source code(tar.gz)
    Source code(zip)
  • 0.3.0(Sep 12, 2018)

    Improve NetworkState structure #28. Now that structure has the requested page number, pageSize, isFirstPage, isLastPage.

    Breaking changes: The NetworkState data class is replaced by a sealed class and the Status enum was removed.

    Source code(tar.gz)
    Source code(zip)
  • 0.2.0(Jul 25, 2018)

    Breaking changes:

    • Added the possibility of using different models on Network and DataSource sources. Fountain.createNetworkWithCacheSupportListing now requires two type declarations #28.

    Fixes:

    • Fix issue when the initialization process throws an Exception #29.
    Source code(tar.gz)
    Source code(zip)
  • 0.1.0(Jul 9, 2018)

Open source Crypto Currency Tracker Android App made fully in Kotlin

CoinBit CoinBit is a beautiful CryptoCurrency app, completely open sourced and 100% in kotlin. It supports following features Track prices of over 300

Pranay Airan 50 Dec 5, 2022
Native android app made with Kotlin & Compose with example usage of Ktor, SqlDelight.

Delight-Playground ?? Native Android application built with Kotlin and Jetpack Compose. This app also illustrates the usage of advance libraries such

Kasem SM 41 Nov 6, 2022
A curated collection of splendid gradients made in Kotlin

Gradients A curated collection of splendid gradients made in Kotlin (port of https://webgradients.com for Android). Only linear gradients included for

null 51 Oct 3, 2022
SharedPreference usage made fun in Kotlin

PreferenceHolder Kotlin Android Library, that makes preference usage simple and fun. To stay up-to-date with news about library This library is younge

Marcin Moskała 155 Dec 15, 2022
🚟 Lightweight, and simple scheduling library made for Kotlin (JVM)

Haru ?? Lightweight, and simple scheduling library made for Kotlin (JVM) Why did you build this? I built this library as a personal usage library to h

Noel 13 Dec 16, 2022
Server Sent Events (SSE) client multiplatform library made with Kotlin and backed by coroutines

OkSSE OkSSE is an client for Server Sent events protocol written in Kotlin Multiplatform. The implementation is written according to W3C Recommendatio

BioWink GmbH 39 Nov 4, 2022
⏰ A powerful and simple-to-use guilded wrapper made in Kotlin.

⏰ guilded-kt [WIP] A powerful yet simple-to-use guilded wrapper made entirely in Kotlin with supporting multiplatform. Take a look at an example of th

Gabriel 13 Jul 30, 2022
🎲 A powerful and simple-to-use guilded wrapper made in Kotlin.

?? deck [WIP] Deck is a powerful yet simple-to-use guilded wrapper made entirely in Kotlin with support to multiplatform. Implementating In case you'r

Gabriel 13 Jul 30, 2022
A discord bot made in Kotlin powered by JDA and Realms.

A discord bot made in Kotlin powered by JDA and Realms.

null 1 Jun 30, 2022
An android application that made as an exercise, that does 4 different conversions.

Following android studio basic course, this is my second (and bit more complicate this time) "practice on your own" project. In few words, it is an an

null 1 Dec 1, 2021