firebase 如何添加snackbar当用户已经存在于jetpack compose android

3j86kqsm  于 2023-11-21  发布在  Android
关注(0)|答案(1)|浏览(174)

我想在firebase中已经存在用户时显示用户的snackbar。当.addOnFailureListener或用户已经存在时,我如何添加snackbar通知?我有概念上的知识差距,所以简要解释一下。

package com.example.bettehomes.data.signup
    
    import android.util.Log
    import androidx.compose.runtime.mutableStateOf
    import androidx.lifecycle.ViewModel
    import com.google.firebase.auth.FirebaseAuth
    import com.google.firebase.auth.FirebaseAuth.AuthStateListener
    import com.example.bettehomes.data.RegistrationUIState
    import com.example.bettehomes.data.rules.Validator
    import com.example.bettehomes.navigation.BetteHomesAppRouter
    import com.example.bettehomes.navigation.Screens
    
    
    class SignupViewModel : ViewModel() {
    
        private val TAG = SignupViewModel::class.simpleName
    
    
        var registrationUIState = mutableStateOf(RegistrationUIState())
    
        var allValidationsPassed = mutableStateOf(false)
    
        var signUpInProgress = mutableStateOf(false)
    
        fun onEvent(event: SignupUIEvent) {
            when (event) {
                is SignupUIEvent.FirstNameChanged -> {
                    registrationUIState.value = registrationUIState.value.copy(
                        firstName = event.firstName
                    )
                    printState()
                }
    
                is SignupUIEvent.LastNameChanged -> {
                    registrationUIState.value = registrationUIState.value.copy(
                        lastName = event.lastName
                    )
                    printState()
                }
    
                is SignupUIEvent.EmailChanged -> {
                    registrationUIState.value = registrationUIState.value.copy(
                        email = event.email
                    )
                    printState()
    
                }
    
    
                is SignupUIEvent.PasswordChanged -> {
                    registrationUIState.value = registrationUIState.value.copy(
                        password = event.password
                    )
                    printState()
    
                }
    
                is SignupUIEvent.RegisterButtonClicked -> {
                    signUp()
                }
    
                is SignupUIEvent.PrivacyPolicyCheckBoxClicked -> {
                    registrationUIState.value = registrationUIState.value.copy(
                        privacyPolicyAccepted = event.status
                    )
                }
            }
            validateDataWithRules()
        }
    
    
        private fun signUp() {
            Log.d(TAG, "Inside_signUp")
            printState()
            createUserInFirebase(
                email = registrationUIState.value.email,
                password = registrationUIState.value.password
            )
        }
    
        private fun validateDataWithRules() {
            val fNameResult = Validator.validateFirstName(
                fName = registrationUIState.value.firstName
            )
    
            val lNameResult = Validator.validateLastName(
                lName = registrationUIState.value.lastName
            )
    
            val emailResult = Validator.validateEmail(
                email = registrationUIState.value.email
            )
    
    
            val passwordResult = Validator.validatePassword(
                password = registrationUIState.value.password
            )
    
            val privacyPolicyResult = Validator.validatePrivacyPolicyAcceptance(
                statusValue = registrationUIState.value.privacyPolicyAccepted
            )
    
    
            Log.d(TAG, "Inside_validateDataWithRules")
            Log.d(TAG, "fNameResult= $fNameResult")
            Log.d(TAG, "lNameResult= $lNameResult")
            Log.d(TAG, "emailResult= $emailResult")
            Log.d(TAG, "passwordResult= $passwordResult")
            Log.d(TAG, "privacyPolicyResult= $privacyPolicyResult")
    
            registrationUIState.value = registrationUIState.value.copy(
                firstNameError = fNameResult.status,
                lastNameError = lNameResult.status,
                emailError = emailResult.status,
                passwordError = passwordResult.status,
                privacyPolicyError = privacyPolicyResult.status
            )
    
    
            allValidationsPassed.value = fNameResult.status && lNameResult.status &&
                    emailResult.status && passwordResult.status && privacyPolicyResult.status
    
        }
    
    
        private fun printState() {
            Log.d(TAG, "Inside_printState")
            Log.d(TAG, registrationUIState.value.toString())
        }
    
    
        private fun createUserInFirebase(email: String, password: String) {
    
            signUpInProgress.value = true
    
            FirebaseAuth
                .getInstance()
                .createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener {
                    Log.d(TAG, "Inside_OnCompleteListener")
                    Log.d(TAG, " isSuccessful = ${it.isSuccessful}")
    
    
                    signUpInProgress.value = false
                    if (it.isSuccessful) {
                        BetteHomesAppRouter.navigateTo(Screens.HomeScreen)
                    }
                }
                .addOnFailureListener {
                    Log.d(TAG, "Inside_OnFailureListener")
                    Log.d(TAG, "Exception= ${it.message}")
                    Log.d(TAG, "Exception= ${it.localizedMessage}")
                    scope.launch {
                        snackbarHostState.showSnackbar("Snackbar")
                    }
                }
        }
    
    
    }

字符串

kiz8lqtg

kiz8lqtg1#

这就是我如何添加的零食吧。
1.创建一个Scaffold来包含内容。

  1. add瓦尔scope = rememberCoroutineScope()和瓦尔snackbarHostState = remember { snackbarHostState()}
    1.在SignupViewModels.kt中创建一个var userExists = MutableStateOf("false")并将其更改为in .addOnFailureListener userlogists.value = true。
    1.然后在SignUpScreen.kt的ButtonComponent中
    ButtonComponent(value = stringResource(id = R.string.register),onButtonClicked = { signupViewModel.onEvent(SignupUIEvent.RegisterButtonClicked)if(signupViewModel. userChecksts.value){ scope.launch { snackbarHostState.showSnackbar(“电子邮件地址已被使用!”)},isEnabled = signupViewModel.allValidationsPassed.value)

相关问题