Eazy Runtime Permission
A lightweight Android library which wraps boilerplate code of runtime permission and allows you to request permissions from coroutines (No callbacks yay) or request and observe permissions through LiveData.
Including in your project
Eazy permissions is available in the Jcenter and divided into two modules so that based on your need you can include either coroutines or livedata in your project
Coroutines support
Requesting permission is just a simple function call to suspending function requestPermissions
of PermissionManager
from your coroutines or other suspending function which will return PermissionResult
. It takes 3 parameters.
- An instance of AppCompactActivity or Fragment depending on from where you are requesting permission.
- Request id.
- varargs of permission you want to request.
This is how you would request for permission within coroutines and get result sequentially.
You can request permission from coroutine launched using any dispatcher(IO/Default/Main).
Library exposes PermissionResult
as result of permission request which is nothing but simple sealed class which wraps all possible outcomes.
Notice PermissionDenied
and PermissionDeniedPermanently
are also exposing list of denied permissions and permanently denied permissions respectively so that you can decide your flow based on denied permissions if you want to.
LiveData support
Just in case of coroutine we saw above requesting permission is just a simple method call to PermissionManager
from your Activity/Fragment. It takes 3 parameters.
- An instance of AppCompactActivity or Fragment depending from where you are requesting permission.
- Request id.
- varargs of permission you want to request.
This is how you request permissions from your Activity/Fragment.
Observing permission request result
With just one simple step(implementing an interface) you are ready to observe the result of request.
Your Activity/Fragment must implement setupObserver
method of PermissionObserver
interface which expose LiveData<PermissionResult
>. Here is the definition of PermissionObserver
The library will only call setupObserver
method when you are requesting permission for the first time. All the successive call to requestPermissions
method will use the same observer.
Just as you would observe other LiveData you can observe LiveData<PermissionResult
> as follow
It is mandatory to implement
PermissionObserver
from where you are requesting permission(either Activity or Fragment).
If you don't then library will throwIllegalArgumentException
stating that you have to implementPermissionObserver
Library will take care of Activity/Fragment recreation so even if user rotates screen or due to some other reason if your Activity/Fragment gets recreated it will call setupObserver
method to register new observer of LiveData.