Giter Site home page Giter Site logo

rxpermissions's Introduction

RxPermissions

Build Status

This library allows the usage of RxJava with the new Android M permission model.

Example (With Retrolambda for brevity, but not required):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Must be done during an initialization phase like onCreate
    RxPermissions.getInstance(this)
        .request(Manifest.permission.CAMERA)
        .subscribe(granted -> {
            if (granted) { // Always true pre-M
               // I can control the camera now
            } else {
               // Oups permission denied
            }
        });
}

If multiple permissions at the same time, the result is combined :

RxPermissions.getInstance(this)
    .request(Manifest.permission.CAMERA,
             Manifest.permission.READ_PHONE_STATE)
    .subscribe(granted -> {
        if (granted) {
           // All requested permissions are granted
        } else {
           // At least one permission is denied
        }
    });

You can also observe a detailed result with requestEach :

RxPermissions.getInstance(this)
.requestEach(Manifest.permission.CAMERA,
             Manifest.permission.READ_PHONE_STATE)
    .subscribe(permission -> { // will emit 2 Permission objects
        if (permission.granted) {
           // `permission.name` is granted !
        }
    });

Look at the sample app for more.

Important read

Because your app may be restarted during the permission request, the request must be done during an initialization phase. This may be Activity.onCreate/onResume, or View.onFinishInflate or others.

If not, and if your app is restarted during the permission request (because of a configuration change for instance), the user's answer will never be emitted to the subscriber.

If you need to trigger the permission request from a specific event and not during initialization phase, you have to pass an extra parameter to the library methods, the trigger. The trigger must be an Observable, you can use JakeWharton/RxBinding to turn your view to an observable (not included in the library).

Example :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Observable<Void> trigger = RxView.clicks(findViewById(R.id.enableCamera));

    RxPermissions.getInstance(this)
        // The trigger is passed as first arg
        .request(trigger, Manifest.permission.CAMERA)
        .subscribe(granted -> {
            // R.id.enableCamera has been clicked
        });
}

Status

This library is at a very early stage of development, so contributions are welcome. It should not be used in production, or at your own risk.

Benefits

  • Avoid worrying about the framework version. If the sdk is pre-M, the observer will automatically receive a granted result.

  • Prevents you to split your code between the permission request and the result handling. Currently without this library you have to request the permission in one place and handle the result in Activity.onRequestPermissionsResult().

  • Handles multiple permission requests out of the box. For instance if during the initialization of your app you request the same permission in 2 different places, only one request will be made to the framework. As a result, only one popup will appear to the user, but his response will be dispatched to all requesters.

  • All what RX provides about transformation, filter, chaining...

Setup

To use this library your minSdkVersion must be >= 9.

In your build.gradle :

repositories {
    maven { url "http://dl.bintray.com/tbruyelle/tbruyelle" }
}

dependencies {
    compile 'com.tbruyelle.rxpermissions:rxpermissions:0.5.1@aar'
}

rxpermissions's People

Contributors

tbruyelle avatar pavelsynek avatar eleventigers avatar egor-n avatar vanniktech avatar tevjef avatar

Stargazers

godchin avatar

Watchers

James Cloos avatar godchin 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.