简体   繁体   English

如何在存储库 class MVVM 中使用 Firebase 身份验证侦听器?

[英]How to use a Firebase auth listener in a repository class MVVM?

This is my MainActivity class:这是我的MainActivity class:

class MainActivity : AppCompatActivity(), FirebaseAuth.AuthStateListener {
    @Inject lateinit var auth: FirebaseAuth

    override fun onAuthStateChanged(auth: FirebaseAuth) {
        val firebaseUser = auth.currentUser
        if (firebaseUser == null) {
            //Update UI
        }
    }

    override fun onStart() {
        super.onStart()
        auth.addAuthStateListener(this)
    }

    override fun onStop() {
        super.onStop()
        auth.removeAuthStateListener(this)
    }
}

Meaning that when the FirebaseUser becomes null for example (the user is signed out), I update the UI accordingly.这意味着例如当 FirebaseUser 变为 null 时(用户已退出),我会相应地更新 UI。 It works fine but when it comes to MVVM, the activity knows about Firebase, which is not correct.它工作正常,但是当涉及到 MVVM 时,活动知道 Firebase,这是正确的。 I have this structure:我有这个结构:

Activity -> ViewModel - Repository (calls to Firebase)

How to listen for auth changes in the repository class?如何监听存储库 class 中的身份验证更改? Or is there any other solution?或者还有其他解决方案吗?

In MVVM architecture we have an event-driven architecture everything that is happening in view model emits and observers get it in view(activity or fragment) in your case repository handles getting the user, then gives it to view model, then you should emit the result.在 MVVM 架构中,我们有一个事件驱动的架构,视图中发生的所有事情 model 发出,观察者在您的案例存储库处理获取用户的视图(活动或片段)中获取它,然后将其提供给查看 model,然后您应该发出结果。 take a look at this SingleLiveEvent in GitHub by google developers for architecture samples: https://github.com/android/architecture-samples/blob/dev-todo-mvvm-live/todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/SingleLiveEvent.java here you use singleLiveEvent to Observe the changes just once, for things like your case with the state of the user just change after requesting for user state to fire base.看看谷歌开发人员在 GitHub 中的 SingleLiveEvent 架构示例: https://github.com/android/architecture-samples/blob/dev-todo-mvvm-live/todoapp/app/src/main/java/com /example/android/architecture/blueprints/todoapp/SingleLiveEvent.java here you use singleLiveEvent to Observe the changes just once, for things like your case with the state of the user just change after requesting for user state to fire base. create a SingleLiveEvent like this in your View model:在您的视图 model 中创建一个像这样的 SingleLiveEvent:

fun userState() : SingleLiveEvent<Boolean> = SingleLiveEvent()

then:然后:

if(repo.getAuthUser() != null)
    userState().postValue(true)

and now you should observe it in your view like this:现在你应该像这样在你的视图中观察它:

viewModel.userState().observe(this, Observer {
    if (it!!)
        //user is created
    else
        //user is null
})

and for handling onStart and onStop in your viewModel, you can easily use Lifecycle-Aware Component:为了在 viewModel 中处理 onStart 和 onStop,您可以轻松地使用 Lifecycle-Aware Component:

in your view add observer on what will implement the "LifecycleObserver": I am going to put it on my viewModel like this:在您的视图中添加观察者,了解将实现“LifecycleObserver”的内容:我将把它放在我的 viewModel 上,如下所示:

lifecycle.addObserver(viewModel)

then make your viewModel the observer by extending it from "LifecycleObserver" and inside handle the lifecycles like this: class myViewModel: LifecycleObserver{然后通过从“LifecycleObserver”扩展您的 viewModel 使其成为观察者,并在内部处理这样的生命周期: class myViewModel: LifecycleObserver{

@OnLifecycleEvent(Event.ON_STOP)
    fun onStop() {
        //do some work1
    }

@OnLifecycleEvent(Event.ON_START)
    fun onStart() {
        //do some work2
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有 Firebase 身份验证的空指针。 如何将监听器放到另一个类 - Null pointer with Firebase auth. How to put listener to another class 如何在自定义 Dialog 类中设置接口和侦听器以将 Facebook 的登录回调传递给 MainActivity 中的 firebase auth? - How to set up Interface and listener in custom Dialog class to pass Facebook's login callback to firebase auth in MainActivity? 如何使用 MVVM 架构上的 Room 在 android 存储库 class 中查询? - How to query inside android repository class using Room on MVVM architechture? MVVM - 如何跨多个 ViewModel 共享单个存储库 class - MVVM - How to share a single repository class across multiple ViewModels MVVM - 如何将上下文传递到存储库 class? - MVVM - how do I pass context to the repository class? 如何保留 Firebase Auth 并在以后使用它登录? - How to persist Firebase Auth and use it to login later? 如何在非活动类中使用Xamarin.Firebase.Auth并得到通知? - How to use Xamarin.Firebase.Auth in a non-activity class and get notified? 如何在 Android 的 Firebase Auth 中使用多个帐户? - How to use multiple accounts in Firebase Auth in Android? 如何在 Xamarin 中同时使用 Firebase Cloud Messaging 和 Firebase Auth? - How to use both Firebase Cloud Messaging and Firebase Auth in Xamarin? 如何使用Firebase使用firebase-auth:11.0.0与Google登录 - How to use firebase to sign in with google using firebase-auth:11.0.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM