Skip to content

Instantly share code, notes, and snippets.

@sagar-viradiya
Created June 10, 2019 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sagar-viradiya/d01c20910e9d78fc2398c44d52772dd5 to your computer and use it in GitHub Desktop.
Save sagar-viradiya/d01c20910e9d78fc2398c44d52772dd5 to your computer and use it in GitHub Desktop.
Eazy permission request permissions using coroutines
.
.
.
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