GithubHelp home page GithubHelp logo

froysource's Introduction

Froysource


Easier to manage resource from Network or Local for Android

How to install

build.gradle (project)

repositories{
    maven { url 'https://jitpack.io' }
}
build.gradle (module)

dependencies{
    implementation "com.github.rezaramadhanirianto:froysource:1.1.2"
}

How to use

Only from Network (flow and retrofit)

Declare your model

    // model for handle error if API status code > 400
    class YourErrorResponse: ErrorResponse() {
        @SerializedName("code")
        override var code: Int = 0

        @SerializedName("message")
        override var message: String = ""
    }

    data class YourModel(val id: String)

First wrap data with class Resource, and check the status

    // retrofit function
    // Responses is model from retrofit
    fun loadNetwork: Responses<YourModel> 

    fun loadFromNetwork(){
        return BaseCall.call(YourErrorResponse::class.java){ loadFromNetwork() }
    }

    val res = loadFromNetwork()

    when(res){
        is Resource.Default -> {} // State first default
        is Resource.Loading -> {} // State loading if needed
        is Resource.Success -> {} // state if success
        is Resource.Error -> {} // state if error
    }

From network and local

// first generic is input data and second generic is output data
return object : NetworkBoundResource<YourModel, YourModel>(){
    // data from API
    override suspend fun createCall(): Flow<Resource<YourModel>> {
        return loadFromNetwork()
    }

    // mapping data from network model to view model
    override fun mapToResultType(data: YourModel): YourModel? {
        return data
    }

    // is no need local storage
    override fun noNeedLocalSource(): Boolean {
        return false
    }

    // should fetch from API
    override fun shouldFetch(data: YourModel): Boolean {
        return true
    }

    // should load from DB
    override fun loadFromDB(): Flow<YourModel>? {
        return localDatabase.movieDao().getPopular(page)
    }

    // save to local db (room)
    override suspend fun saveCallResult(data: YourModel) {
        localDatabase.yourDao().insertAllReplace(data)
    }

    // deprecated
    // on failed get from API
    override fun onFetchFailed() {
        println("do something")
    }

}.asFlow()

froysource's People

Contributors

rezaramadhanirianto avatar

Watchers

 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.