Getting confusion in using mutableState with Jetpa...
# compose
l
Getting confusion in using mutableState with Jetpack Compose
Copy code
@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun PermissionScreen(
    modifier: Modifier = Modifier,
    permissionsManager: PermissionsManager,
    onPermissionGranted: () -> Unit
) {
    val context = LocalContext.current
    // To check whether the permission is requested at least once by user so that we can
    // navigation to setting page on permission denied permanently.
    var permissionRequested by remember { mutableStateOf(false) }

    var onResultState: (Map<String, Boolean>) -> Unit by remember { mutableStateOf({}) }

    // Called when storage R/W permission is granted or denied.
    // Also called when permission is denied permanently and we try to ask permission.
    val storageRWPermissionsState = rememberMultiplePermissionsState(
        permissionsManager.permissions
    ) { map ->
        val allGranted = map.values.reduce { acc, next -> acc && next }
        Toast.makeText(context, "2", Toast.LENGTH_SHORT).show()
        onResultState(map)
        Toast.makeText(context, "3", Toast.LENGTH_SHORT).show()
    }
    
    // Ask the permission to the user on app start.
    val lifecycleOwner = LocalLifecycleOwner.current
    DisposableEffect(key1 = lifecycleOwner) {
        val observer = LifecycleEventObserver { _, event ->
            if (event == Lifecycle.Event.ON_START) {
                try {
                    apply(
                        context,
                        produceIntentLauncher = {
                            onResultState = it
                            Toast.makeText(context, "1", Toast.LENGTH_SHORT).show()
                            storageRWPermissionsState
                        }
                    )
                } catch (e: Exception) {
                    Logger.debug(message = e.message, cause = e.cause)
                }
            }
        }
        lifecycleOwner.lifecycle.addObserver(observer)

        onDispose {
            lifecycleOwner.lifecycle.removeObserver(observer)
        }
    }
}

@OptIn(ExperimentalPermissionsApi::class)
fun apply(
    context: Context,
    produceIntentLauncher: (onResult: (Map<String, Boolean>) -> Unit) -> MultiplePermissionsState
) {
   produceIntentLauncher {
       Toast.makeText(context, "4", Toast.LENGTH_SHORT).show()
   }.launchMultiplePermissionRequest()
}
I am using this apply function to get the result in produceIntentLauncher function, But it is not getting called if I do not use the below line above
Copy code
var onResultState: (Map<String, Boolean>) -> Unit by remember { mutableStateOf({}) }
call it like i have used in above my code is working fine but I do not know how it is woking. If I do not use the onResultState I am getting Toast pattern like Toast -> 1, 2, 3, And If I use onResultState I am getting Toast pattern like Toast -> 1, 2, 4, 3 This is what i want but do not know how it is working