Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in dialog use error #140

Open
Caleb-Rainbow opened this issue Nov 9, 2024 · 0 comments
Open

in dialog use error #140

Caleb-Rainbow opened this issue Nov 9, 2024 · 0 comments

Comments

@Caleb-Rainbow
Copy link

The same permission request code works normally in a normal compose, but an exception occurs in a dialog.
code:

@Composable
fun PermissionsRequest(vararg permission: Permission,onPermissionGranted: (Boolean) -> Unit): PermissionsController {
    val factory: PermissionsControllerFactory = rememberPermissionsControllerFactory()
    val permissionsController: PermissionsController = remember(factory) { factory.createPermissionsController() }
    val coroutineScope: CoroutineScope = rememberCoroutineScope()
    BindEffect(permissionsController)
    LaunchedEffect(Unit){
        var isGranted = true
        coroutineScope.launch {
            permission.forEach {
                val r = permissionsController.isPermissionGranted(it)
                if (!r) isGranted = false
            }
            onPermissionGranted(isGranted)
        }
    }
    return permissionsController
}
@Composable
fun CustomCameraView(modifier: Modifier = Modifier) {
var permissionState by remember {
        mutableStateOf(false)
    }
    val scope = rememberCoroutineScope()
    val permissionsController = PermissionsRequest(Permission.CAMERA){
        permissionState = it
    }
    if (permissionState){
        //todo Any
    }else{
        Column(modifier = modifier.background(black40), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
            Text( text = "please open permission", color = white)
            Button(onClick = {
                scope.launch {
                    try {
                        permissionsController.providePermission(Permission.CAMERA)
                        permissionState = true
                    }catch(deniedAlways: DeniedAlwaysException) {
                        // todo 
                    } catch(denied: DeniedException) {
                        // todo 
                    }
                }
            }){
                Text(text = "申请相机权限")
            }
        }
    }
}

use in dialog:

@Composable
fun ScanAddMaterialDialog(onDismissRequest: () -> Unit) {
    AlertDialog(onDismissRequest = onDismissRequest,
        title = { Text(text = "扫描标签")},
        text = {
            CustomCameraView(modifier = Modifier.fillMaxSize())
        },
        confirmButton = {},
        dismissButton = {
            TextButton(onClick = onDismissRequest) {
                Text(text = "取消")
            }
        }, containerColor = white)
}

error:

java.lang.IllegalStateException: android.view.ContextThemeWrapper@47c2ad9 context is not instance of ComponentActivity
                                         	at dev.icerock.moko.permissions.compose.BindEffect_androidKt$BindEffect$1.invokeSuspend(BindEffect.android.kt:23)
                                         	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                         	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:101)
                                         	at androidx.compose.ui.platform.AndroidUiDispatcher.performTrampolineDispatch(AndroidUiDispatcher.android.kt:81)
                                         	at androidx.compose.ui.platform.AndroidUiDispatcher.access$performTrampolineDispatch(AndroidUiDispatcher.android.kt:41)
                                         	at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.run(AndroidUiDispatcher.android.kt:57)
                                         	at android.os.Handler.handleCallback(Handler.java:942)
                                         	at android.os.Handler.dispatchMessage(Handler.java:99)
                                         	at android.os.Looper.loopOnce(Looper.java:211)
                                         	at android.os.Looper.loop(Looper.java:300)
                                         	at android.app.ActivityThread.main(ActivityThread.java:8321)
                                         	at java.lang.reflect.Method.invoke(Native Method)
                                         	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:581)
                                         	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1028)
                                         	Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [androidx.compose.ui.platform.MotionDurationScaleImpl@c6d4aa, androidx.compose.runtime.BroadcastFrameClock@ef0889b, StandaloneCoroutine{Cancelling}@8eb9438, AndroidUiDispatcher@ec75c11]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant