Coder Social home page Coder Social logo

double-loading-loaders's Introduction

Double Loading Loaders (1.1.0)

Introduction

Loader to implement the following flow:

  • Load in the background the cache and present the data
  • Load in the background the data via a network request, update the cache and represent the data
  • ForceLoad() on the loader to restart the flow from the network request only
  • Determine on onLoadFinished() in which Load State we are alt text

Usage

In your app module build.gradle add the dependency, published in jCenter:

repositories {
    jcenter()
}
dependencies {
    (...)
    compile 'il.co.galex:double-loading-loaders:1.1.0'
}

Example

Extend the class DoubleLoadingAsyncLoader to implement the double loading flow

public class ChatLoader extends DoubleLoadingAsyncLoader<List<Chat>> {

    private final ChatDao chatDao;
    private String uuid;

    public ChatLoader(Context context, String uuid) {
        super(context);
        this.uuid = uuid;
        chatDao = new ChatDao(getContext());
    }
    @Override
    public List<Chat> loadCacheInBackground() {
        return chatDao.findAll();
    }

    @Override
    public List<Chat> loadNetworkInBackground() {
        chatDao.insertOrUpdate(ApiHelper.getUserChats(uuid)); // API call + Update of Database
        return chatDao.findAll(); // reload from database as the data is merged by the previous line
    }
}

in onLoadFinished(), to determine in what step of the flow we are, use DoubleLoaderUtils.getLoadState(loader)

 @Override
    public void onLoadFinished(Loader<List<Comment>> loader, List<Chat> data) {

        final LoadState loadState = DoubleLoaderUtils.getLoadState(loader);
        android.util.Log.d(TAG, "onLoadFinished() called with: load state = " + loadState);
        if (loadState == LoadState.NETWORK_LOADED || loadState == LoadState.NETWORK_RELOADED) {
            //TODO remove the loading indicator
        }

        // TODO set data in your adapter
    }

Roadmap

  • CursorLoader with the same concept

License

This plugin is available under the Apache License, Version 2.0.

(c) All rights reserved Alexander Gherschon

double-loading-loaders's People

Contributors

galex avatar

Stargazers

 avatar

Forkers

matrixy

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.