Created
December 1, 2023 04:14
-
-
Save haydenhhyc/fc9a7ff72ba30d54aa52e2135987137d to your computer and use it in GitHub Desktop.
Composable that ask for permission
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Wrapper composable for checking permissions at runtime | |
| */ | |
| @OptIn(ExperimentalPermissionsApi::class) | |
| @Composable | |
| fun PermissionWrapper( | |
| activity: ComponentActivity, | |
| composable: @Composable () -> Unit, | |
| ) { | |
| val permissionStates = rememberMultiplePermissionsState( | |
| activity.requestedPermissions() | |
| ) | |
| SideEffect { | |
| permissionStates.launchMultiplePermissionRequest() | |
| } | |
| if (permissionStates.allPermissionsGranted) { | |
| composable() | |
| } | |
| } | |
| /** | |
| * Util for getting all requested permissions for the current package | |
| */ | |
| @Suppress("DEPRECATION") | |
| fun ComponentActivity.requestedPermissions(): List<String> { | |
| return packageManager.getPackageInfo( | |
| packageName, | |
| PackageManager.GET_PERMISSIONS | |
| ).requestedPermissions.toList() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment