Coder Social home page Coder Social logo

rxpagingloading's Introduction

Reactive Paging and Loading

Maven Central Android Arsenal License: MIT

This library implements reactive paging and loading.

It helps to handle the states of loading a simple data (LCE - loading/content/error) or the complex states of lists with pagination (PLCE - paging/loading/content/error).

The solution is based on the usage of Unidirectional Data Flow pattern.

The library depends on RxJava, so you will find familiar interfaces in it's API.

Dependency

Add the dependency to your build.gradle:

dependencies {
    implementation 'ru.mobileup:rxpagingloading:1.0.1'
}

Loading a simple data

Loading interface looks as follows:

interface Loading<T> {

    enum class Action { REFRESH, FORCE_REFRESH }

    val state: Observable<State<T>>

    val actions: Consumer<Action>

    data class State<T>(
        val content: T? = null,
        val loading: Boolean = false,
        val error: Throwable? = null
    )
}

It includes:

  • State class — represents LCE state.
  • Action enum - the possible actions.
  • state: Observable - observes changes of the LCE state.
  • actions: Consumer - receives performed Action.

There are two implementations of this interface:

LoadingOrdinary

This class is for simple case when at first load or after refresh content comes from Single data source, passed into the constructor:

LoadingOrdinary(
    source = Single.just("Content string")
)

LoadingAssembled

This implementation is for case when there is a separate Сompletable to refresh the content and an Observable stream for receiving this content updates:

LoadingAssembled(
    refresh = repository.refreshDataCompletable(),
    updates = repository.dataChangesObservable()
)

Paging

The Paging interface looks a bit more complicated. In addition to the LCE, it has the paging states:

interface Paging<T> {

    enum class Action { REFRESH, FORCE_REFRESH, LOAD_NEXT_PAGE }

    val state: Observable<State<T>>

    val actions: Consumer<Action>

    data class State<T>(
        val content: List<T>? = null,
        val loading: Boolean = false,
        val error: Throwable? = null,
        val pageLoading: Boolean = false,
        val pageError: Throwable? = null,
        val lastPage: Page<T>? = null
    ) {
        val isEndReached: Boolean get() = lastPage?.isEndReached ?: false
    }

    interface Page<T> {
        val items: List<T>
        val lastItem: T? get() = items.lastOrNull()
        val isEndReached: Boolean
    }
}

Note, the State also stores the last loaded page. It is used to download the following page, as well as to determine the end of the list.

Page is an interface made for flexibility. Your data source can map a page data to it's own class. For example, you can store an identifier of the last entity, or a link to the next page, or any data depending on your back-end requirements. The last page will be passed to a lambda pageSource from the constructor of the PagingImpl:

class PageInfo(
    override val items: List<Item>,
    override val isEndReached: Boolean
    lastItemId: Int
) : Paging.Page<Item>

PagingImpl(
    pageSource = { offset, lastPage ->
        repository
            .loadPage(lastItemId = lastPage?.lastItemId)
            .map {
                PageInfo(
                    items = it.list,
                    isEndReached = (offset + it.list.size) == it.totalCount
                    it.lastItemId
                )
            }
    }
)

Display the state

You can just use the resulting PLCE or LCE state to render your screen UI. Or you can use extensions from LoadingExtensions.kt and PagingExtensions.kt to observe individual state parts changes. It's helpful when you don't need all of the states or use with MVVM-like pattern.

In the sample we use the RxPM library and extensions to split resulting state to the Presentation Model states.

License

MIT License

Copyright (c) 2019 MobileUp

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

rxpagingloading's People

Contributors

aartikov avatar dmdevgo avatar jeevuz avatar mochalovv avatar mohaxspb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.