OK, I understood it.
You need to implement this composable and use it when you need to use a feature that needs the permissions: you pass in an array of permissions
, a requestCode
(any Int
), and two lambdas with composables: onGranted
, which is used when the permissions were granted, and onDenied
composable in the other case.
@Composable
fun PermissionsRequest(
permissions: Array<out String>,
requestCode: Int,
onGranted: @Composable () -> Unit,
onDenied: @Composable () -> Unit,
onDeniedPermanently: (@Composable () -> Unit)? = null,
rational: (@Composable () -> Unit)? = null,
awaitResult: (@Composable () -> Unit)? = null,
) {
val permissionHandler = AmbientPermissionHandler.current
val (permissionResult, setPermissionResult) = remember(permissions) {
mutableStateOf<PermissionResult?>(null)
}
LaunchedEffect(Unit) {
setPermissionResult(permissionHandler.requestPermissions(requestCode, permissions))
}
when (permissionResult) {
is PermissionResult.PermissionGranted -> onGranted()
is PermissionResult.PermissionDenied -> onDenied()
is PermissionResult.PermissionDeniedPermanently -> onDeniedPermanently?.invoke()
is PermissionResult.ShowRational -> rational?.invoke()
null -> awaitResult?.invoke()
}
}
Also you need to implement an ambient. As I understand it, it is used to pass the value down the composables children. In our case AmbientPermissionHandler
is going to be passed with its value — PermissionHandler
— to the PermissionsRequest
from Providers
composable.
val AmbientPermissionHandler = ambientOf<PermissionHandler>()
The PermissionHandler
implementation that would be passed to PermissionRequest
composable as an AmbientPermissionHandler
using Providers
.
class PermissionHandler(private val context: AppCompatActivity) {
suspend fun requestPermissions(
requestCode: Int,
permissions: Array<out String>
): PermissionResult {
return PermissionManager.requestPermissions(context, requestCode, *permissions)
}
}
And then you use it like this:
class MainActivity : AppCompatActivity() {
private val permissionHandler = PermissionHandler(this)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Providers(
AmbientPermissionHandler provides permissionHandler
) {
PermissionsRequest(
permissions = arrayOf(Manifest.permission.READ_SMS),
requestCode = PERMISSION_REQUEST_CODE,
onGranted = { /* Here goes the composables when the permission is granted */ },
onDenied = { /* Is used when the permission is denied */ }
)
}
}
}
}
Initialize PermissionHandler
in MainActivity
and then provide it in Providers
inside the setContent
.
To use PermissionManager
and LaunchedEffect
you need those dependencies:
implementation 'com.sagar:coroutinespermission:2.0.3'
implementation 'androidx.compose.runtime:runtime:1.0.0-alpha11'
And thanks to 2jan222 for the sample code.