Giter Club home page Giter Club logo

cloudbox-android-rx's Introduction

Cloudbox-Android-RX

This library enables you to get your remote files hosted on Cloudbox seamlessly using RXJava which can come in handy when you don't want to waste your server resources on some files that don't change frequesntly. If you prefer the Callbacks fashion you can use this version (https://github.com/duriana/Cloudbox-Android)

Open Source Love License

Setting up.

1-Import the stable version of the module (Gradle/Maven):

2-if you have extended the class Application.

Add this to tag at the top of your manifest file:

xmlns:tools="http://schemas.android.com/tools" 

Add these lines to your tag:

tools:replace="name"
tools:node:"merge"

3-Add the following to the project root build grade:

dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
classpath 'me.tatarka:gradle-retrolambda:2.5.0โ€™
}

#How to use

1-In your Application class or any activity you define the single Object CloudBox like this.

CloudBox cloudBox = CloudBox.getInstance(context, YOUR_CLOUDBOX_DOMAIN)
          .setMetaPrefix(CUSTOM_PREFIX);
<!-- where YOUR_CLOUDBOX_DOMAIN = "https://cloudbox.domain.com"
      CUSTOM_PREFIX = "/GBCloudBoxResourcesMeta/" -->
2- Use that object to sync files on background and get observable with the result of the process.
Observable<CloudBox.RESULT> o = cloudBox.getFileFromServerRX(this, "currenciesRates", ".json");
o.observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<CloudBox.RESULT>() {
            @Override
            public void onCompleted() {
            //COMPLETION ACTION
            }

            @Override
            public void onError(Throwable e) {
            //REPORT EXCEPTION.
            }

            @Override
            public void onNext(CloudBox.RESULT result) {
                //SUCCESS ACTION
                if(result == CloudBox.RESULT.FETCHED)
                    //INDICATES THAT NEW VERSION OF THE FILE HAS BEEN DOWNLOADED.
                else if(result == CloudBox.RESULT.UP_TO_DATE)
                    //INDICATES THAT YOU ALREADY HAVE THE NEWEST
        });

The code above will Sync the file called 'currenciesRates' by calling the meta URL: https://cloudbox.domain.com/GBCloudBoxResourcesMeta/currenciesRates.json then downloads the file if there is a newer version of it.

If you need to block some action until you sync multiple files together or waiting for some other API call of yours you can use .zip operator to join multiple observables together.

Observable<CloudBox.RESULT> o = cloudBox.getFileFromServerRX(this, "currenciesRates", ".json");
Observable<CloudBox.RESULT> o2 =  cloudBox.getFileFromServerRX(this, "privacyPolicy", ".json");

o2.zipWith(o, new Func2<CloudBox.Result, CloudBox.Result, ArrayList<CloudBox.Result>>() {
            @Override
            public ArrayList<CloudBox.Result> call(CloudBox.Result result1, CloudBox.Result result2) {
                ArrayList<CloudBox.Result> res = new ArrayList<>();
                res.add(result1);
                res.add(result2);
                return res;
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ArrayList<CloudBox.Result>>() {
                   ...... 
                });

3-Now if you want to get the content of the file you simply type:

String privacyPolicyString = cloudbox.getFileAsString(MainActivity.this, "privacyPolicy", ".json");

4-Keep a fallback version with same name in the assets to use in case of offline use.

Use CloudBox.setLogEnabled(true) to keep track of the whole process.

Thanks.

cloudbox-android-rx's People

Contributors

zuhair-kj avatar

Watchers

 avatar

Forkers

tonyhaddad91

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.