Git Product home page Git Product logo

remotedatak's Introduction

RemoteData for Kotlin

jcenter Build Status

Algebraic data type (ADT) to represent the state of data that is loading from/to remote sources/destinations

Setup

dependencies {
  repositories {
    jcenter()
  }
}

implementation("com.mercari.remotedata:remotedata:<latest-version>")

About

RemoteData is useful to represent the state of data, when it is loading from/to a remote source/destination.

Using RemoteData is pretty straightforward, it is however meant to be used in a functional style.

RemoteData works nicely with RxJava and RxRedux, however it can be used independently.

This is done through 4 types: Initial, Loading, Success and Failure.

  • Initial represents the initial state of the data before any progress has been made.

  • Loading represents the intermediary loading state of data.

  • Success represents the state where the loading is completed, and holds the resulting value of the data.

  • Failure represents the state where the loading failed, and holds information about the reason of failure with an Exception.

In cases where the data size is known, you may find the properties progress and totalUnits of the Loading type useful. The value of progress is always between 0 and totalUnits (default of 100 for percentages).

Initial and Loading are Incomplete, whereas Success and Failure are Complete.

Usage

A common use case for RemoteData would be mapping it into a UI transition or component state.

For example, when making a network request to fetch data, most UIs have a progress indicator and finally display the result.

  • Declare your data where you need it, where V is the type of data value and E the type of Exception/Error.
var data: RemoteData<V, E> = Initial
  • Once the data starts to load, transition to Loading with the optional setting of a progress and totalUnits.
data = Loading()

If progress is used, simply update it when required.

data.progress = data.progress?.let { it + delta }
  • Once the loading is complete successfully, transition to Success along with the actual data.
data = Success(value)
  • Otherwise, transition to Failure with a proper Exception.
data = Failure(Exception("error"))
  • Behaviour based on the data state can then be mapped neatly, wherever the data is processed.
when (data) {
    is Initial -> initialize()
    is Loading -> progress(data.progress)
    is Success -> success(data.value) 
    is Failure -> failure(data.error)
 }

Or with the other conveniences:

when {
    data.isIncomplete -> showProgress()
    data.isComplete -> hideProgress()
 }

Higher oder functions

A few higher order functions are also provided for convenience such as:

  • map
  • mapError
  • mapBoth
  • getOrElse
  • flatMap
  • flatMapError
  • fanout

For examples, take a look at some of the comprehensive tests.

Contribution

Please read the CLA below carefully before submitting your contribution.

https://www.mercari.com/cla/

License

Copyright 2018-2019 Mercari, Inc.

Licensed under the MIT License.

remotedatak's People

Contributors

beylerian avatar keithyokoma avatar kittinunf avatar tanakaworld 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  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

snijsure

remotedatak's Issues

Design for Parcel support needs improvement

The current implementation of the android version for Parcel support has a lot of overlap with the pure Kotlin one. The design should be improved to share the common code between those implementations.

Republish the library to maven central

As JCenter will close its distribution service and stop the delivery in February 2022, we need to republish this library including the past versions on Maven Central.

Deploy artifact to SNAPSHOT repository

Currently, builds fail for uploading duplicate artifacts on master branch workflow.
It seems to be better to upload them as a SNAPSHOT, and upload the finalized artifact to the release repository when we create a new tag.

Add progress value to Loading?

It could be a good idea to consider adding a value to keep track of the loading progress, such as the percentage, if the total size is known, or just the loaded data size.

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.