Created
July 12, 2018 11:03
-
-
Save mahdi-malv/5180927b5a6819c839240ab099ab2557 to your computer and use it in GitHub Desktop.
Ask for permission with TedPermission
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
//Ted Permission is a great permission library to manage Runtime permission added since Android 6.0(Marshmallow) | |
//This example Uses RxJava 2 to get permission. | |
/** | |
* <b>Remember this need one dependency code</b> | |
* Code: implementation 'gun0912.ted:tedpermission-rx2:2.2.0' // Or later | |
* Might also need: implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' //Or later | |
* @param c is the Context | |
* @param deniedTitle is the Title when permission denied and you are showing a message that you need it | |
* @param deniedMessage is the Message | |
* @param doWithPermissionResult is a function that has a Boolean. If true the permission is granted | |
* @param permissions are the Permission list like <code>Manifest.permission.READ_CONTACTS</code> | |
* {@see https://github.com/ParkSangGwon/TedPermission} | |
* <b>Remember it's not a long method it's just one line actually</b> | |
*/ | |
fun askPermission(c: Context, | |
rationalTitle: String, | |
rationalMessage: String, | |
deniedTitle: String, | |
deniedMessage: String, | |
doWithPermissionResult: (Boolean) -> Unit, | |
vararg permissions: String | |
) = | |
TedRx2Permission.with(c) | |
.setRationalTitle(rationalTitle) | |
.setRationalMessage(rational) | |
.setDeniedTitle(deniedTitle) | |
.setDeniedMessage(deniedMessage) | |
.setPermissions(*permissions) | |
.request() | |
.subscribe({ | |
doWithPermissionResult(it.isGranted) | |
}, { | |
System.err.println("Error in permission: ${it.message}") | |
})!! | |
//It also checks for permission... Means if you asked then user allowed next time it will be considered as allowed | |
//implementation 'gun0912.ted:tedpermission-rx2:2.2.0' // Or later | |
//implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' //Or later | |
//If you don't wanna use RxJava 1 or 2 to do this please refer to https://github.com/ParkSangGwon/TedPermission for other types. |
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
private fun getPermission() { | |
askPermission(this, "Need to vibrate", "We need to vibrate your device.","Vibration needed.", | |
"Some cards might vibrate your device, So we need this permission. If you deny app won't function.", | |
this::handlePermissionResult, Manifest.permission.VIBRATE | |
) // Vibrate is an Example ... But it doesn't need runtime asking | |
//Denied and Rational Title and message are optional. If you don't set them they won't be shown to user. | |
} | |
//What is handlePermissionResult? | |
private fun handlePermissionResult(result: Boolean) { | |
if (result) { | |
//GRANTED | |
} else { | |
//DENIED | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment