Skip to content
This repository was archived by the owner on Nov 21, 2023. It is now read-only.

A simple, idiomatic permissions API with Kotlin Coroutines

License

Notifications You must be signed in to change notification settings

gabrielfeo/SuspendPermission

Folders and files

NameName
Last commit message
Last commit date

Latest commit

a5ad791 · May 31, 2020

History

43 Commits
May 30, 2020
May 30, 2020
May 28, 2020
May 30, 2020
May 31, 2020
May 28, 2020
May 30, 2020
May 31, 2020
May 30, 2020
May 28, 2020
May 28, 2020
May 28, 2020
May 30, 2020

Repository files navigation

SuspendPermission

Release

A simple, idiomatic permissions API with Kotlin Coroutines.

Setup

Declare JitPack as a repository and add a dependency on the library:

repositories {
    maven { url "https://jitpack.io" }
}

dependencies {
    implementation 'com.gabrielfeo:suspendpermission:1.0.0'
}

Gradle Kotlin DSL example

Usage

requestPermissionsAsync can be called from any Activity or Fragment with a set of permissions and a request code, just like the platform's requestPermissions:

suspend fun requestPermissionsForRecording() {
    val camera = Manifest.permission.CAMERA
    val mic = Manifest.permission.RECORD_AUDIO
    try {
        requestPermissionsAsync(arrayOf(camera, mic), 13949)) // suspend
    } catch (exception: PermissionsDeniedException) {
        when {
            camera in exception.permanentlyDeniedPermissions -> // respect the user's decision
            else -> // explain you really need access to the camera
        }
    }
}

Permissions will first be checked and the ones which aren't yet granted will be requested to the user, throwing a PermissionsDeniedException if any is denied. The exception specifies which were permanently denied so that it's easy to decide what to do.

requestPermissionsAsync is available as an extension function on androidx.app.FragmentActivity and androidx.fragment.Fragment.

License

This project is licensed under the terms of the MIT license. See the LICENSE file.