Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork24
Know about real-time state of a Android app Permissions with Kotlin Flow APIs.
License
PatilShreyas/permission-flow-android
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Know about real-time state of a Android app Permissions with Kotlin Flow APIs.Made with ❤️ forAndroid Developers.
In big projects, app is generally divided in several modules and in such cases, if any individualmodule is just a data module (not having UI) and need to know state of a permission, it's notthat easy. This library provides a way to know state of a permission throughout the app andfrom any layer of the application safely.
For example, you can listen for state of contacts permission in class where you'll instantly showlist of contacts when permission is granted.
It's a simple and easy to use library. Just Plug and Play.
You can check/app directory which includes example application for demonstration.
Inbuild.gradle of app module, include this dependency
dependencies { implementation"dev.shreyaspatil.permission-flow:permission-flow-android:$version"// For using in Jetpack Compose implementation"dev.shreyaspatil.permission-flow:permission-flow-compose:$version"}
You can find latest version and changelogs in thereleases.
A permission state can be subscribed by retrievingStateFlow<PermissionState> orStateFlow<MultiplePermissionState> as follows:
val permissionFlow=PermissionFlow.getInstance()// Observe state of single permissionsuspendfunobservePermission() { permissionFlow.getPermissionState(Manifest.permission.READ_CONTACTS).collect { state->if (state.isGranted) {// Permission granted, access contacts. }elseif (state.isRationaleRequired==true) {// Permission denied, but can be requested again }else {// Permission denied, and can't be requested again } }}// Observe state of multiple permissionssuspendfunobserveMultiplePermissions() { permissionFlow.getMultiplePermissionState(Manifest.permission.READ_CONTACTS,Manifest.permission.READ_SMS ).collect { state->// All permission statesval allPermissions= state.permissions// Check whether all permissions are grantedval allGranted= state.allGranted// List of granted permissionsval grantedPermissions= state.grantedPermissions// List of denied permissionsval deniedPermissions= state.deniedPermissions// List of permissions requiring rationaleval permissionsRequiringRationale= state.permissionsRequiringRationale }}
State of a permission and state of multiple permissions can also be observed in Jetpack Compose application as follows:
@ComposablefunExampleSinglePermission() {val state by rememberPermissionState(Manifest.permission.CAMERA)if (state.isGranted) {// Permission granted }elseif (state.isRationaleRequired==true) {// Permission denied, but can be requested again }else {// Permission denied, and can't be requested again }}@ComposablefunExampleMultiplePermission() {val state by rememberMultiplePermissionState(Manifest.permission.CAMERA,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.READ_CONTACTS )if (state.allGranted) {// Render something }val grantedPermissions= state.grantedPermissions// Do something with `grantedPermissions`val deniedPermissions= state.deniedPermissions// Do something with `deniedPermissions`val permissionsRequiringRationale= state.permissionsRequiringRationale// Do something with `permissionsRequiringRationale`}
It's necessary to use utilities provided by this library to request permissions so that whenever permission statechanges, this library takes care of notifying respective flows.
UseregisterForPermissionFlowRequestsResult() method to getActivityResultLauncherand uselaunch() method to request for permission.
classContactsActivity :AppCompatActivity() {privateval permissionLauncher= registerForPermissionFlowRequestsResult()privatefunaskContactsPermission() { permissionLauncher.launch(Manifest.permission.READ_CONTACTS,...) }}
UserememberPermissionFlowRequestLauncher() method to getManagedActivityResultLauncherand uselaunch() method to request for permission.
@ComposablefunExample() {val permissionLauncher= rememberPermissionFlowRequestLauncher()Button(onClick= { permissionLauncher.launch(android.Manifest.permission.CAMERA,...) }) {Text("Request Permissions") }}
If you're not usingActivityResultLauncher APIs provided by this library thenyou willnot receive permission state change updates. But there's a provision by whichyou can help this library to know about permission state changes.
UsePermissionFlow#notifyPermissionsChanged() to notify the permission state changesfrom your manual implementations.
For example:
classMyActivity:AppCompatActivity() {privateval permissionFlow=PermissionFlow.getInstance()privateval permissionLauncher= registerForActivityResult(RequestPermission()) { isGranted-> permissionFlow.notifyPermissionsChanged(android.Manifest.permission.READ_CONTACTS) }}
This library starts processing things lazily whenevergetPermissionState() orgetMultiplePermissionState() is calledfor the first time. But this can be controlled with these methods:
fundoSomething() {// Stops listening to the state changes of permissions throughout the application.// This means the state of permission retrieved with [getMultiplePermissionState] method will not// be updated after stopping listening. permissionFlow.stopListening()// Starts listening the changes of state of permissions after stopping listening permissionFlow.startListening()}
This library automatically gets initialized with the App Startup library.If you want to provide own coroutine dispatcher
classMyApplication:Application() {overridefunonCreate() {super.onCreate()val permissionDispatcher=Executors.newFixedThreadPool(3).asCoroutineDispatcher()PermissionFlow.init(this, permissionDispatcher) }}
Disable auto initialization of library with default configuration using this:
<providerandroid:name="androidx.startup.InitializationProvider"android:authorities="${applicationId}.androidx-startup"android:exported="false"tools:node="merge"> <meta-dataandroid:name="dev.shreyaspatil.permissionFlow.initializer.PermissionFlowInitializer"android:value="androidx.startup"tools:node="remove" /></provider>
Visit the API documentation of this library to get more information in detail. This documentation is generated usingDokka.
Check the Test Coverage Report of this library. This is generated usingKover.
Readcontribution guidelines for more information regarding contribution.
Have any questions, doubts or want to present your opinions, views? You're always welcome. You canstart discussions.
Copyright 2022 Shreyas PatilLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.About
Know about real-time state of a Android app Permissions with Kotlin Flow APIs.
Topics
Resources
License
Code of conduct
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.