jetpackcompose碎片发行问题

发布于 2025-02-03 04:56:09 字数 2722 浏览 4 评论 0原文

我需要在我的SignScreen的Oncreate方法中调用功能,但没有任何适用的位置。我无法从任何地方调用CurrentUserCheck函数。

我尝试过的内容:

  1. init中调用它在ViewModel中。但是问题在于它扔了nullpoInterException navcontroller此处。

  2. MainActivityMytheme中调用它,但我在这些范围中遇到了许多奇怪的问题。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除→记忆 2025-02-10 04:56:09

解决方案

正如@pztar所说,我在ViewModel中声明了控制操作为状态的身份验证,然后我在我的Composobable signscreen中使用 启动效果及已解决。

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

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
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文