Opinionated permission handling done right with Kotlin.
Check more details in my blog
Never Ask again
option, we will route the user to the application settings to enable the permission there.No
shouldShowRationale
? Yes, this is such a confusing method depends on its name. In our flow, user always gets the explanation first, which should be a design which you always follow. But underneath, we use it to check theNeverAskAgain
option.
implementation 'xyz.albertgao:permissionk:1.0.0'
startToRequestPermission {
permissions = listOf(SEND_SMS, READ_PHONE_STATE)
requestCode = [email protected]
ifSuccess {
// Your handling logic
}
elseShowDialogToRequestPermission{
message = "Hi, We need these permissions because we want to make the user experiences better, please grant them!"
}
}
override fun onRequestPermissionsResult(
theRequestCode: Int,
thePermissions: Array<String>,
theGrantResults: IntArray
) {
onRequestPermissionResultHandler {
actualRequestCode = theRequestCode
expectRequestCode = [email protected]
permissions = thePermissions
grantResultsParam = theGrantResults
onSuccess {
// Do what you want
}
onFailure {
// You don't get the permission, do something
}
onNeverAskAgain {
message = "We noticed you have disabled some permissions . We will take you to the Application settings, you can re-enable the permission there."
}
}
}