Created
June 10, 2019 18:21
-
-
Save sagar-viradiya/d01c20910e9d78fc2398c44d52772dd5 to your computer and use it in GitHub Desktop.
Eazy permission request permissions using coroutines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. | |
. | |
. | |
launch { | |
//CoroutineScope | |
val permissionResult = PermissionManager.requestPermissions( //Suspends the coroutine | |
this@Fragment, | |
REQUEST_ID, | |
Manifest.permission.ACCESS_FINE_LOCATION, | |
Manifest.permission.READ_CONTACTS, | |
Manifest.permission.CAMERA | |
) | |
//Resume coroutine once result is ready | |
when(permissionResult) { | |
is PermissionResult.PermissionGranted -> { | |
//Add your logic here after user grants permission(s) | |
} | |
is PermissionResult.PermissionDenied -> { | |
//Add your logic to handle permission denial | |
} | |
is PermissionResult.PermissionDeniedPermanently -> { | |
//Add your logic here if user denied permission(s) permanently. | |
//Ideally you should ask user to manually go to settings and enable permission(s) | |
} | |
is PermissionResult.ShowRational -> { | |
//If user denied permission frequently then she/he is not clear about why you are asking this permission. | |
//This is your chance to explain them why you need permission. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment