Giter Site home page Giter Site logo

flower's Introduction

Flower

Super cool library for Android to manage database caching and networking with ease. It helps you to handle all scenario of API request(success/error/loading) in one place along with database caching. It's inspired from Goolge's Github Browser Sample. It's built on top of Retrofit and use powerful and elegant kotlin flow api.

You can find companion medium article here

Installation

Add JitPack to your project level build.gradle file

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Add Gradle dependency as

dependencies {
    implementation 'com.github.hadiyarajesh:flower:1.0.0'
}

Usage

Prerequisite

  • Your Dao method must return Flow<YourModelClass>
  • Your api method must return Flow<ApiResponse<YourModelClass>>
  • Add FlowCallAdapterFactory as CallAdapterFactory in your retrofit builder

1. In repository class

return the networkBoundResource() function. This function takes following functions as parameter

  • fetchFromLocal - It fecth data from local database
  • shouldFetchFromRemote - It decide whether network request should be made or use local persistent data if available
  • fetchFromRemote - It perform network request operation
  • processRemoteResponse - It process result of network response (if requires)
  • saveRemoteData - It saves result of network request to local persistent database
  • onFetchFailed - It handle network request failure scenario (Non HTTP 200..300 response, exceptions etc)

Sample call to networkBoundResource() should look like this

repository

2. In view model class

Collect or transform flow to get 3 different state of on-going request, LOADING, SUCESS or ERROR

viewmodel

Now you can observe it in your Activity/Fragment.

Sample

Sample app is provided in this repository. You can find it here. It fetch random quote from remote api and save it to local persistent database in order to display it on UI.

License

MIT License

flower's People

Contributors

hadiyarajesh 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.