Hey folks, good to see you back after a long time 👋. I have been working on the Android library called eazypermissions
for past one month which allows you to request runtime permission with coroutines and LiveData support. In this post, we will see why I created this library and a basic introduction to the library.
The motto 🎯
In this year’s Google I/O there were lots of announcements regarding coroutines support for Jetpack libraries. Also since coroutines became stable in Kotlin 1.3.0 there were also lots of buzz about it in community.
The community is slowly adapting coroutines and it’s here to stay for sure. The fact that you can write asynchronous code in a sequential way makes your code clean and concise.
Oftentimes we consume the third party library APIs in our coroutines and those APIs were not designed having coroutines in mind. But things are changing and some popular third-party libraries are started supporting coroutines e.g. Retrofit 2.6.0 now has suspend
support.
Runtime permission is callback based API wherein you request for permission and listen for the result. Wouldn’t it be awesome if you don’t need to write boilerplate callback code for the result and consume it in a sequential way within coroutines? Well, this is exactly why I wrote this library focusing on clean and concise API that you can easily consume within coroutines (No callbacks yay 🎉). We will see how in a bit.
Coming to LiveData support, Architecture Components has been around for quite some time. Observing runtime permission results through LiveData could be one of the interesting use-case. I thought it would be nice to have runtime permission API around LiveData. The library will expose LiveData of result and consumer can observe and decide what to do upon the result. We will see how in a bit.
Introduction to Eazy Permissions
Based on your need (coroutines support or LiveData support) you can include the library in your project as shown below.
//For coroutines
implementation 'com.sagar:coroutinespermission:1.0.0'
//For LiveData
implementation 'com.sagar:livedatapermission:1.0.0'
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.
Here is the signature of requestPermissions
method.
This is how you would request for permission within coroutines and get result sequentially.
You can request permission from coroutines launched using any dispatcher (IO/Default/Main).
Library exposes PermissionResult
as a result of permission request which is nothing but simple sealed class which wraps all possible outcomes.
LiveData support
Just in case of coroutines 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.
Here is the signature of requestPermissions
method.
This is how you would request permissions from your Activity/Fragment.
Observing result
With just one simple step (implementing an interface) you are ready to observe the result of the request. Your Activity/Fragment must implement **setupObserver**
method of PermissionObserver
interface which exposes LiveData<PermissionResult
>. Here is the definition of interface.
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 shown below.
That’s it, that’s all I had to share with you folks about this library. I will be maintaining this library regularly and I am hoping for your suggestions and contributions to improve this library. Head over to the GitHub repo below for more details about library.
Do give it a shot and let me know your feedback in comments below or you can reach out to me on Twitter.
That’s it, for now, folks 🙂 Until next time 👋
Happy coding 😃
Thanks, Nate Ebel and Nemi Shah for proofreading this.
This was originally posted on Medium