1

i have a alert dialog that show and close by boolean from the state, the problem is when i click close button and change the state to false, the dialog didnt want to hide. i've logged the state when i close button, and the result from logged state has been false. can you help me ?

this my DialogViewModel :

class DialogViewModel : ViewModel() {
    companion object {
        val myPrefs by mutableStateOf( DialogPref() )
    }
}

class DialogPref {
    var openDialog: Boolean = false
    var title: String = ""
    var subtitle: String = ""
}

this what things that triggered the dialog to showing up :

loginsViewModel.getLiveDataObserver().observe(this) {
                    if (it != null) {
                        if(it.code.toString() == "1")
                        {
                            Handler(Looper.getMainLooper()).postDelayed({
                                loadingViewModel.onDialogDismiss()
                                val intent = Intent(this, SigninotpActivity::class.java)
                                intent.putExtra("method", it.value.otpVia.method)
                                intent.putExtra("via", it.value.otpVia.via)
                                intent.putExtra("phone", phoneSignin)
                                intent.putExtra("password", passwordSignin)
                                intent.putExtra("token", it.value.token)
                                intent.putExtra("biometric", "none")
                                startActivity(intent)
                            }, 2000)
                        }
                        else{
                            Handler(Looper.getMainLooper()).postDelayed({
                                loadingViewModel.onDialogDismiss()
                                DialogViewModel.myPrefs.openDialog = true
                                DialogViewModel.myPrefs.title = "Your mobile number or password is incorrect"
                                DialogViewModel.myPrefs.subtitle = "Please ensure you enter a valid mobile number and password before trying again."
                            }, 2000)
                        }
                    } else {
                        Handler(Looper.getMainLooper()).postDelayed({
                            loadingViewModel.onDialogDismiss()
                            Toast.makeText(applicationContext, "Something Error", Toast.LENGTH_LONG).show()
                        }, 2000)
                    }
                }

this my code :

if (DialogViewModel.myPrefs.openDialog) {
                        AlertDialog(
                            onDismissRequest = {
                                DialogViewModel.myPrefs.openDialog = false
                            },
                            title = {
                                Text(
                                    text = DialogViewModel.myPrefs.title,
                                    fontSize = 24.sp,
                                    fontFamily = roboto,
                                    fontWeight = FontWeight.Medium
                                )
                            },
                            text = {
                                Text(
                                    text = DialogViewModel.myPrefs.subtitle,
                                    fontSize = 19.sp,
                                    fontFamily = roboto,
                                    fontWeight = FontWeight.Normal,
                                    lineHeight = 27.sp,
                                    color = Color(android.graphics.Color.parseColor("#000000")).copy(alpha = 0.5f)
                                )
                            },
                            buttons = {
                                Row(
                                    modifier = Modifier.padding(top = 30.dp),
                                    horizontalArrangement = Arrangement.End
                                ) {
                                    Spacer(modifier = Modifier.weight(1f))
                                    ClickableText(
                                        text = buildAnnotatedString { append("OK") },
                                        onClick = {
                                            DialogViewModel.myPrefs.openDialog = false
                                                  notifyAll()},
                                        style = TextStyle(
                                            fontSize = 14.sp,
                                            lineHeight = 17.sp,
                                            fontFamily = roboto,
                                            fontWeight = FontWeight.Medium,
                                            color = Color(android.graphics.Color.parseColor("#673AB7"))
                                        ),
                                        modifier = Modifier.padding(end = 50.dp, bottom = 20.dp)

                                    )
                                }
                            }
                        )
                    }

i tried to use this code in another activity, the dialog closed. i didnt know what happened

zake zake
  • 11
  • 2

0 Answers0