https://kotlinlang.org logo
Title
m

mattinger

12/14/2021, 5:07 PM
So, i’m trying to use the accompanist permissions. The issue i’m running into is that i’m doing my checking when a particular button is clicked. As such, PermissionsRequired isn’t a great solution for me. Instead, i’ve built a composable that’s turned on and off with a flag. I’m not 100% happy with this, but wondering if there’s a better way. See thread for that function
@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun PermissionScaffold(
    checkingPermission: Boolean,
    onCheckingPermissionChanged: (Boolean) -> Unit,
    permissions: List<String>,
    onPermisionsGranted: () -> Unit,
    onPermissionsDeniedTemporarily: () -> Unit,
    onPermissionsDeniedPermanently: () -> Unit,
    onShowRationale: @Composable (MultiplePermissionsState) -> Unit,
) {
    if (checkingPermission) {
        val permissionState = rememberMultiplePermissionsState(permissions = permissions)

        when {
            permissionState.allPermissionsGranted -> {
                SideEffect {
                    onCheckingPermissionChanged(false)
                    onPermisionsGranted()
                }
            }

            permissionState.shouldShowRationale && !permissionState.permissionRequested -> {
                onShowRationale(permissionState)
            }

            !permissionState.permissionRequested -> {
                SideEffect {
                    permissionState.launchMultiplePermissionRequest()
                }
            }

            permissionState.shouldShowRationale && permissionState.permissionRequested -> {
                SideEffect {
                    onCheckingPermissionChanged(false)
                    onPermissionsDeniedTemporarily()
                }
            }

            !permissionState.shouldShowRationale && permissionState.permissionRequested -> {
                SideEffect {
                    onCheckingPermissionChanged(false)
                    onPermissionsDeniedPermanently()
                }
            }
        }
    }
}
Basically i’m putting the permissionState in here as part of the composition because i need the permissionRequired flag to reset once the initial check is done. This is because if the user denies the request, it’s possible i might want to stay on the screen i’m on.
The PermissionsRequired functoin still makes me have to have check all the permissionState stuff directly in the permissionsNotGrantedContent callback, because otherwise i can’t tell the difference between states.