Coder Social home page Coder Social logo

esunilkumare / rxactivityresult Goto Github PK

View Code? Open in Web Editor NEW

This project forked from victoralbertos/rxactivityresult

0.0 1.0 0.0 186 KB

A reactive-tiny-badass-vindictive library to break with the OnActivityResult implementation as it breaks the observable chain.

License: Apache License 2.0

Java 100.00%

rxactivityresult's Introduction

Android Arsenal

Join the chat at https://gitter.im/VictorAlbertos/RxActivityResult

RxActivityResult

The api which Android SDK exposes to retrieve the data from a 'returning system call' (camera, gallery, email...) just does not give a shit about Don't break the chain leitmotiv. Indeed, the OnActivityResult approach will break entirely your observable chaining.

I did this library to not have to deal with this OnActivityResult pattern. Never. Ever.

RxActivityResult features:

  • Launch the intent from any class, as long as you supply a valid Activity or Fragment instance.
  • Get the Intent back with the data encapsulated in an observable and keep going crazy chaining operators.

Setup

Add the JitPack repository in your build.gradle (top level module):

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

And add next dependencies in the build.gradle of the module:

dependencies {
    compile 'com.github.VictorAlbertos:RxActivityResult:0.4.1-2.x'
    compile 'io.reactivex.rxjava2:rxjava:2.0.5'
}

Usage

Call RxActivityResult.register in your Android Application class, supplying as parameter the current instance.

public class SampleApp extends Application {

    @Override public void onCreate() {
        super.onCreate();
        RxActivityResult.register(this);
    }
}

You can call RxActivityResult.on(this).startIntent(intent) supplying both, an Activity instance or a Fragment instance. Observe the emitted Result item to know the resultCode and retrieve the associated data if appropriate.

Limitation:: Your fragments need to extend from android.support.v4.app.Fragment instead of android.app.Fragment, otherwise they won't be notified.

Intent takePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

RxActivityResult.on(this).startIntent(takePhoto)
        .subscribe(result -> {
            Intent data = result.data();
            int resultCode = result.resultCode();

            if (resultCode == RESULT_OK) {
                result.targetUI().showImage(data);
            } else {
                result.targetUI().printUserCanceled();
            }
        });

Please pay attention to the targetUI() method in the Result object emitted.

This method returns a safety instance of the current Activity/Fragment. Because the original one may be recreated (due to configuration changes or some other system events) it would be unsafe calling it.

Instead, you must call any method/variable of your Activity/Fragment from this instance encapsulated in the Result object.

StartIntentSenderForResult

RxActivityResult supports startIntentSenderForResult too, by calling RxActivityResult.on(this).startIntentSender and supplying the proper arguments. As follows:

RxActivityResult.on(this).startIntentSender(pendingIntent.getIntentSender(), new Intent(), 0, 0, 0)
        .subscribe(result -> {
            
        });

Examples

There is an example of RxActivityResult using both activity and fragment in the app module

Author

Víctor Albertos

Another author's libraries using RxJava:

  • RxCache: Reactive caching library for Android and Java.
  • RxPaparazzo: RxJava extension for Android to take images using camera and gallery.
  • RxFcm: RxJava extension for Android Firebase Cloud Messaging (aka fcm).

rxactivityresult's People

Contributors

victoralbertos avatar riwnodennyk avatar gitter-badger avatar miguelbcr 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.