Last active
May 26, 2018 10:34
-
-
Save alorma/7b3913ca38e752ba24b3670b367fe466 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import com.karumi.dexter.DexterBuilder | |
| import com.karumi.dexter.PermissionToken | |
| import com.karumi.dexter.listener.PermissionDeniedResponse | |
| import com.karumi.dexter.listener.PermissionGrantedResponse | |
| import com.karumi.dexter.listener.PermissionRequest | |
| import com.karumi.dexter.listener.single.PermissionListener | |
| @DslMarker | |
| annotation class PermissionDsl | |
| @PermissionDsl | |
| class PermissionBuilder( | |
| private val name: String, | |
| private val permissionListener: DexterBuilder.Permission | |
| ) { | |
| private lateinit var permissionGranted: (permissionName: String) -> Unit | |
| private var permissionRationale: ((permissionName: String, | |
| accept: () -> Unit, | |
| cancel: () -> Unit) -> Unit)? = null | |
| private lateinit var permissionDenied: (permissionName: String) -> Unit | |
| private lateinit var dexter: DexterBuilder | |
| fun build(): PermissionBuilder { | |
| dexter = permissionListener.withPermission(name) | |
| .withListener(object : PermissionListener { | |
| override fun onPermissionGranted(response: PermissionGrantedResponse) { | |
| permissionGranted(response.permissionName) | |
| } | |
| override fun onPermissionRationaleShouldBeShown(permission: PermissionRequest, | |
| token: PermissionToken) { | |
| permissionRationale?.invoke(permission.name, { | |
| token.continuePermissionRequest() | |
| }, { | |
| token.cancelPermissionRequest() | |
| }) | |
| } | |
| override fun onPermissionDenied(response: PermissionDeniedResponse) { | |
| permissionDenied(response.permissionName) | |
| } | |
| }) | |
| return this | |
| } | |
| fun onGranted(function: (String) -> Unit) { | |
| this.permissionGranted = function | |
| } | |
| fun onDenied(function: (String) -> Unit) { | |
| this.permissionDenied = function | |
| } | |
| fun rationale(function: (permissionName: String, | |
| accept: () -> Unit, | |
| cancel: () -> Unit) -> Unit) { | |
| this.permissionRationale = function | |
| } | |
| fun check() { | |
| dexter.check() | |
| } | |
| } | |
| @PermissionDsl | |
| fun DexterBuilder.Permission.dsl( | |
| name: String, | |
| function: PermissionBuilder.() -> Unit): | |
| PermissionBuilder = | |
| with(PermissionBuilder(name, this)) { | |
| apply(function) | |
| build() | |
| } |
This file contains hidden or 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
| permissionBuilder = permission.dsl(Manifest.permission.READ_CONTACTS) { | |
| onGranted { onGrant() } | |
| onDenied { onDeny() } | |
| rationale { permissionName, accept, cancel -> | |
| if (rational(permissionName)) { | |
| accept() | |
| } else { | |
| cancel() | |
| } | |
| } | |
| } | |
| permissionBuilder.check() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment