Skip to content

Instantly share code, notes, and snippets.

@fschuetz04
Last active November 14, 2022 21:39
Show Gist options
  • Save fschuetz04/bde1bc46cfd914bdb25689ea5be40c7f to your computer and use it in GitHub Desktop.
Save fschuetz04/bde1bc46cfd914bdb25689ea5be40c7f to your computer and use it in GitHub Desktop.
package com.example.app
import android.app.Activity
import android.content.pm.PackageManager
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
@Composable
fun PermissionRequired(
permission: String,
state: PermissionRequiredState,
onStateChange: (PermissionRequiredState) -> Unit,
rationaleContent: @Composable (onRequestPermission: () -> Unit) -> Unit,
deniedContent: @Composable () -> Unit,
grantedContent: @Composable () -> Unit,
) {
val context = LocalContext.current
val lifecycle = LocalLifecycleOwner.current.lifecycle
var permissionGranted by rememberSaveable {
val result = ContextCompat.checkSelfPermission(context, permission)
mutableStateOf(result == PackageManager.PERMISSION_GRANTED)
}
DisposableEffect(key1 = Unit) {
val observer = LifecycleEventObserver { _, event ->
if (event != Lifecycle.Event.ON_RESUME) {
return@LifecycleEventObserver
}
val result = ContextCompat.checkSelfPermission(context, permission)
permissionGranted = result == PackageManager.PERMISSION_GRANTED
if (permissionGranted && state != PermissionRequiredState.OneRequestLeft) {
onStateChange(PermissionRequiredState.OneRequestLeft)
}
}
lifecycle.addObserver(observer)
onDispose {
lifecycle.removeObserver(observer)
}
}
val permissionRequest = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestPermission(),
) { granted ->
if (granted) {
// handled via lifecycle observer
return@rememberLauncherForActivityResult
}
val showRationale = ActivityCompat.shouldShowRequestPermissionRationale(
context as Activity,
permission,
)
val newState = when (state to showRationale) {
PermissionRequiredState.TwoRequestsLeft to false -> PermissionRequiredState.TwoRequestsLeft
PermissionRequiredState.TwoRequestsLeft to true -> PermissionRequiredState.OneRequestLeft
PermissionRequiredState.OneRequestLeft to true -> PermissionRequiredState.OneRequestLeft
else -> PermissionRequiredState.NoRequestsLeft
}
if (newState != state) {
onStateChange(newState)
}
}
when {
permissionGranted -> grantedContent()
state in listOf(
PermissionRequiredState.TwoRequestsLeft,
PermissionRequiredState.OneRequestLeft
) -> rationaleContent(onRequestPermission = {
permissionRequest.launch(permission)
})
state == PermissionRequiredState.NoRequestsLeft -> deniedContent()
}
}
enum class PermissionRequiredState {
TwoRequestsLeft,
OneRequestLeft,
NoRequestsLeft;
companion object {
val Initial = TwoRequestsLeft
}
}
@AlaaEddinAlbarghoth
Copy link

Hello, can I know why there is TwoRequestsLeft in the init state? and why do you need PermissionRequiredState since you have the callback onStateChange ?

@fschuetz04
Copy link
Author

@AlaaEddinAlbarghoth The Android model allows an app to request a permission a second time, even if the user already denied it once. This means that initially the app has two requests left.

Having state and onStateChange as the arguments to this composable is a typical pattern in Jetpack Compose to allow the calling composable to manage the state. This composable receives its state via the argument state and communicates a change to this state via onChangeState.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment