Skip to content

Commit

Permalink
Add single permission request with boolean return for suspend function
Browse files Browse the repository at this point in the history
  • Loading branch information
AraujoJordan committed May 21, 2020
1 parent 2337274 commit 92e54cd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ This implementation uses [suspend functions](https://kotlinlang.org/docs/referen

```kotlin
suspend fun cameraUsage() {
val res = ExcuseMe.couldYouGive(this).permissionFor(android.Manifest.permission.CAMERA)
if(res.granted.contains(android.Manifest.permission.CAMERA)) {
if(ExcuseMe.couldYouGive(this).permissionFor(android.Manifest.permission.CAMERA) {
//Do your camera stuffs
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ class ExampleActivity : AppCompatActivity() {
//and using the permission answer to update screen.
calendarPermissionButton.setOnClickListener {
CoroutineScope(Dispatchers.Main.immediate).launch {

val res =
ExcuseMe.couldYouGive(this@ExampleActivity)
.permissionFor(permission.WRITE_CALENDAR)

calendarPermissionsFeedback?.apply {
if (res.granted.contains(permission.WRITE_CALENDAR)) {
if (res) {
text = granted
setTextColor(green.defaultColor)
} else {
Expand Down
10 changes: 10 additions & 0 deletions excuseme/src/main/java/com/araujo/jordan/excuseme/ExcuseMe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ class ExcuseMe private constructor() {
*/
suspend fun permissionFor(vararg permission: String) = runPermissionRequest(*permission)

/**
* Ask permission for one or multiple permissions and start the permission dialog
* This method use async return from Kotlin Coroutines and can be used without callbacks
*
* @param permission one or multiple permissions from android.Manifest.permission.* strings
* @return boolean that holds the result with the granted/refused permission
*/
suspend fun permissionFor(permission: String) =
runPermissionRequest(permission).granted.contains(permission)

/**
* Calls the InvisibleActivity that makes the Permission request. The channel will
* listen for the completePermission()
Expand Down
1 change: 0 additions & 1 deletion excuseme/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="ExcuseMe.Theme.Transparent" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
Expand Down

0 comments on commit 92e54cd

Please sign in to comment.