Home > Net >  JetpackCompose Fragment onCreate issue
JetpackCompose Fragment onCreate issue

Time:06-03

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
                }
            }
        }
    }
}

CodePudding user response:

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
                }
            }
        }
    }
}
  • Related