How we can compare textfield values are same value in jetpack compose?

580 Views Asked by At

I have register screen in android jetpack compose, and I have ScreenA and ScreenB, in ScreenA I have email and in ScreenB I have again mail and confirm mail, so I want to control those three email values is same value. In ScreenA when I put any mail, in ScreenB both mail must be same mail with ScreenA, any solution?

ScreenA:

@Composable
fun ScreenA(
    navController: NavController,
    model: MyViewModel
) {

    val email = remember { mutableStateOf(TextFieldValue()) }
  
    Column(
        Modifier
            .fillMaxSize()
        ,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        val context = LocalContext.current

  OutlinedTextField(
            value = emailState.value,
            colors = TextFieldDefaults.textFieldColors(
                backgroundColor = white,
                focusedIndicatorColor = Grey,
                unfocusedIndicatorColor = Grey,
                focusedLabelColor = Grey,
                unfocusedLabelColor = Grey,
                cursorColor = custom,
                textColor = custom,

                ),
            onValueChange = { emailState.value = it },
            label = { Text(text = "Email") },
            placeholder = { Text(text = "Email") },
            singleLine = true,
            modifier = Modifier.fillMaxWidth(0.8f)

        )
}

ScreenB:

@Composable
fun ScreenB(

    navController: NavController,
    model: MyViewModel

) {

    val emailState = remember { mutableStateOf(TextFieldValue()) }
    val confirmEmailState = remember { mutableStateOf(TextFieldValue()) }

   Column(
        Modifier.fillMaxSize()  ,
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {


     OutlinedTextField(
            value = emailState.value,
            colors = TextFieldDefaults.textFieldColors(
                backgroundColor = white,
                focusedIndicatorColor = Grey,
                unfocusedIndicatorColor = Grey,
                focusedLabelColor = Grey,
                unfocusedLabelColor = Grey,
                cursorColor = custom,
                textColor = custom,

                ),
            onValueChange = { emailState.value = it },
            label = { Text(text = "E-mail") },
          
            singleLine = true,
            modifier = Modifier.fillMaxWidth(0.8f)

        )

        OutlinedTextField(
            value = confirmEmailState.value,
            colors = TextFieldDefaults.textFieldColors(
                backgroundColor = white,
                focusedIndicatorColor = Grey,
                unfocusedIndicatorColor = Grey,
                focusedLabelColor = Grey,
                unfocusedLabelColor = Grey,
                cursorColor = custom,
                textColor = custom,

                ),
            onValueChange = { confirmEmailState.value = it },
            label = { Text(text = "Confirm E-mail") },
            placeholder = { Text(text = "Confirm E-mail") },
            singleLine = true,
            modifier = Modifier.fillMaxWidth(0.8f)

        )
}

viewmodel:

@HiltViewModel
class MyViewModel @Inject constructor(
    val db: FirebaseFirestore,
    val auth: FirebaseAuth,


) : ViewModel() {

    var singIn = mutableStateOf(false)
    var isInProgress = mutableStateOf(false)
    var popNotification = mutableStateOf<Event<String>?>(null)
    var userData = mutableStateOf<User?>(null)


    init {

        val currentUser = auth.currentUser
        singIn.value = currentUser != null
        currentUser?.uid?.let { uid ->
            getUserData(uid)
        }
    }

    fun onSignOut() {
        auth.signOut()
    }

    fun onSignUp(
        email: String,
        password: String


        ) {
        if (
            email.isEmpty()

        ) {
            handledException(customMessage = "Please fill in all fields")
            return
        }

        isInProgress.value = true

        auth.createUserWithEmailAndPassword(email, password)

            .addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    singIn.value = true

                } else {
                    handledException(customMessage = "signed failed")
                }
                isInProgress.value = false
            }
            .addOnFailureListener {

            }
    }

    fun onSignUpEmail(
      
        emailState: String,
        confirmEmailState: String,

        ) {
        if (
      
            emailState.isEmpty() or
            confirmEmailState.isEmpty()


        ) {
            handledException(customMessage = "Please fill in all fields")
            return
        }

        isInProgress.value = true

        db.collection(USERS).whereEqualTo("email", email.replace(" ", "")).get()
            .addOnSuccessListener { documents ->
                if (documents.size() > 0) {
                    handledException(customMessage = "mail already exist")
                    isInProgress.value = false
                } else {

                    createOrUpdateProfile(
                 
                        emailState = emailState,
                        confirmEmailState = confirmEmailState,

                        )

                    isInProgress.value = false

                }
            }
            .addOnFailureListener { }
    }



    private fun createOrUpdateProfile(

        emailState: String? = null,
        confirmEmailState: String? = null,
      
        ) {
        val uid = auth.currentUser?.uid
        val userData = User(
        
            emailState = emailState ?: userData.value?.emailState,
            confirmEmailState = confirmEmailState ?: userData.value?.confirmEmailState,
  
        )
        uid?.let {
            isInProgress.value = true
            db.collection(USERS).document(uid).get()
                .addOnSuccessListener {
                    if (it.exists()) {
                        it.reference.update(userData.toMap())
                            .addOnSuccessListener {
                                this.userData.value = userData
                                isInProgress.value = false
                            }
                            .addOnFailureListener {
                                handledException(customMessage = "Cannot Update user")
                                isInProgress.value = false

                            }
                    } else {
                        db.collection(USERS).document(uid).set(userData)
                        getUserData(uid)
                        isInProgress.value = false
                    }
                }
                .addOnFailureListener { exception ->
                    handledException(exception, "Cannot create user")
                    isInProgress.value = false
                }
        }
    }

    private fun getUserData(uid: String) {
        isInProgress.value = true
        db.collection(USERS).document(uid).get()
            .addOnSuccessListener {
                val user = it.toObject<User>()
                userData.value = user
                isInProgress.value = false

            }
            .addOnFailureListener { exception ->
                handledException(exception, "Cannot retrieve user data")
                isInProgress.value = false
            }
    }

    fun onLogin(email: String, pass: String) {
        if (email.isEmpty() or pass.isEmpty()) {
            handledException(customMessage = "Please fill in all fields")
            return
        }
        isInProgress.value = true
        auth.signInWithEmailAndPassword(email, pass)
            .addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    singIn.value = true
                    isInProgress.value = false
                    auth.currentUser?.uid?.let { uid ->
//                        handledException(customMessage = "Login success")
                        getUserData(uid)
                    }
                } else {
                    handledException(task.exception, "Login failed")
                    isInProgress.value = false
                }
            }
            .addOnFailureListener { exc ->
                handledException(exc, "Login failed")
                isInProgress.value = false
            }
    }

    private fun handledException(exception: Exception? = null, customMessage: String = "") {
        exception?.printStackTrace()
        val errorMsg = exception?.message ?: ""
        val message = if (customMessage.isEmpty()) {
            errorMsg
        } else {
            "$customMessage: $errorMsg"
        }
        popNotification.value = Event(message)
    }
1

There are 1 best solutions below

10
Roman Shtykalo On

Move emailState to your viewModel. You can also convert it to Flow, but it is not obligatory.

It looks like that in your viewmodel:

val emailStateA = MutableStateFlow(TextFieldValue())
val emailStateB = MutableStateFlow(TextFieldValue()) 
val areEmailsEqual = MutableStateFlow(true) 
init {
    viewModelScope.launch {
       combine(emailStateA, emailStateB) { a, b -> 
           areEmailsEqual.emit(a == b) }.collect() 
    }       
}