RxPermissionsResult


Source link: https://github.com/VictorAlbertos/RxPermissionsResult

RxPermissionsResult

Following the same approach that RxActivityResult does, RxPermissionsResult is a reactive wrapper around the permission's Android Api, which allows to get the data without breaking the observable chain.

Features:

  • Request permissions at runtime from any class, as long as you supply a valid Activity or Fragment instance.
  • Get the data back encapsulated in an observable and keep chaining.
  • Survive to configuration changes.

SetUp

Add to top level gradle.build file

allprojects {

  repositories {

maven {
 url "https://jitpack.io" 
}

  
}
 
}

Add to app module gradle.build file

dependencies {
  //RxJava1
  compile 'com.github.VictorAlbertos.RxPermissionsResult:rx1:0.0.4'
  compile 'io.reactivex:rxjava:1.2.1'
//RxJava2
  compile 'com.github.VictorAlbertos.RxPermissionsResult:rx2:0.0.4'
  compile 'io.reactivex.rxjava2:rxjava:2.0.0' 
}

Usage

Call RxPermissionsResult.register in your Android Application.

public class SampleApp extends Application {

@Override public void onCreate() {

super.onCreate();

RxPermissionsResult.register(this);

  
}
 
}

You can call RxPermissionsResult.on(this).requestPermissions(permissions) supplying both, an Activity instance or a Fragment instance.

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

String[] permissions = {
Manifest.permission.ACCESS_FINE_LOCATION
}
;  RxPermissionsResult.on(this).requestPermissions(permissions)
.subscribe(result ->

 result.targetUI()

  .showPermissionStatus(result.permissions(), result.grantResults())
);
  void showPermissionStatus(String[] permissions, int[] grantResults) {

  boolean granted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
  if (granted) {

textView.setText(permissions[0] + " Granted")  
}
 else {

textView.setText(permissions[0] + "Not granted")  
}
 
}

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.

For a complete example about RxPermissionsResult, refer to this module if you are using RxJava1, or to this other one if using RxJava2.

Another author's libraries:

  • ReactiveCache: A reactive cache for Android and Java which honors the Observable chain.
  • RxActivityResult: A reactive-tiny-badass-vindictive library to break with the OnActivityResult implementation as it breaks the observables chain.
  • RxSocialConnect: OAuth RxJava extension for Android.

Resources

This open-source library allows you to integrate Uber into your Android app.

Work easy with Android built-in and custom content providers data + Stetho extension + Sample app.

A Material Design themed ShowcaseView for Android.

Android Device Names is a 14 kB library that transforms the device model name into something people can understand. For example, a useless SM-N910W8 becomes Samsung Galaxy Note 4.

Dumpey is an Android Debug Bridge utility tool.

FancyBackground is a tiny Android library designed to animate a set of resource Drawables. It ensures the drawables are subsampled and cached, if necessary, with heavy lifting done in the background.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes