0

I need to call a function in my SignScreen's onCreate method but there is not any applicable place for this. I can't call currentUserCheck function from anywhere.

What i tried :

  1. Calling it in init block in viewModel. But the problem was it throwing nullPointerException for NavController here.

  2. Calling it in MainActivity and MyTheme but i faced many weird issues in these scopes.

ViewModel:

@HiltViewModel                                                                                     
class SignViewModel (@ApplicationContext context: Context) : ViewModel() {
    val auth = FirebaseAuth.getInstance()

    init {
        currentUserCheck(NavController(context))
    }

    fun signIn(email: String?, password: String?, context: Context, navController: NavController) {

        if (email != null && email.isNotBlank() && password != null && password.isNotBlank()) {
            auth.signInWithEmailAndPassword(email, password).addOnSuccessListener {
                navController.navigate(ScreenHolder.ProfileScreen.route) {
                    popUpTo(ScreenHolder.SigningScreen.route) {
                        inclusive = true
                    }
                }
            }.addOnFailureListener {
                Toast.makeText(context, it.localizedMessage, Toast.LENGTH_LONG).show()
            }
        } else {

            Toast.makeText(
                context,
                "Lütfen email ve şifre alanlarını boş bırakmayınız.",
                Toast.LENGTH_LONG
            ).show()
        }
    }

    fun signUp(email: String?, password: String?, context: Context, navController: NavController) {
        if (email != null && email.isNotBlank() && password != null && password.isNotBlank()) {
            auth.createUserWithEmailAndPassword(email, password)
                .addOnSuccessListener {
                    navController.navigate(ScreenHolder.ProfileScreen.route) {
                        popUpTo(ScreenHolder.ProfileScreen.route) {
                            inclusive = true
                        }
                    }
                }
        } else {
            Toast.makeText(
                context,
                "Lütfen email ve şifre alanlarını boş bırakmayınız.",
                Toast.LENGTH_LONG
            ).show()
        }
    }

    fun currentUserCheck(navController: NavController) {
        if (auth.currentUser != null) {
            navController.navigate(ScreenHolder.ProfileScreen.route) {
                popUpTo(ScreenHolder.ProfileScreen.route) {
                    inclusive = true
                }
            }
        }
    }
}
Sevban Bayır
  • 196
  • 3
  • 13
  • 1
    You're trying to route the user based on if they're authenticated or not? Instead of passing NavController to your ViewModel you can just return the state of that user to your composable, observe the state, and allow your nav controller to route based on that. – Pztar May 31 '22 at 21:18

1 Answers1

0

Solution

As @Pztar said i declare the authentication controlling operation as a state in viewModel and then i observed it in my Composable SignScreen with LaunchEffect and it is solved.

@HiltViewModel                                                                                     
class SignViewModel (@ApplicationContext context: Context) : ViewModel() {
    val auth = FirebaseAuth.getInstance()

    var currentUser = mutableStateOf(false)

    
//New currenUserCheck method that declare if state is true or not
fun currentUserCheck() {

        if (auth.currentUser != null) {
            currentUser.value = true

        }
    }
}
@Composable
fun SignScreen(viewModel: SignViewModel= hiltViewModel(),navController: NavController,context: Context) {

    //Observing the state from my composable and routing user if state is true.

    LaunchedEffect(key1 = Unit ){
        viewModel.currentUserCheck()
        if (viewModel.currentUser.value){
            navController.navigate(ScreenHolder.ProfileScreen.route) {
                popUpTo(ScreenHolder.SigningScreen.route) {
                    inclusive = true
                }
            }
        }
    }
}
Sevban Bayır
  • 196
  • 3
  • 13
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 02 '22 at 07:04