VeceluXa
VeceluXa

Reputation: 67

Firebase authentication onCompleteListener not working when passing fragment activity using requireActivity()

In my current project I need to use MVP pattern. I moved Firebase authentication with onCompleteListener from fragment's OnCreateView to separate file UserModel. This listener requires activity to work. I passed activity using fragment's requireActivity() to LoginPresenter. Presenter then passes activity to UserModel, where sign in is performed.

Sign in works but without working listener I have to restart app in order to close login form.

I tried to change Activity in doLogIn to FragmentActivity but that throws "no interface method doLogIn" error.


OnCreateView Fragment:

binding.buttonLogin.setOnClickListener {
    loginPresenter.doLogin(requireActivity(), email, password)
}

LoginPresenter:

override fun doLogin(activity: Activity, email: String, password: String): Boolean {

    user.email = email
    user.password = password

    return user.doLogIn(activity)
}

UserModel:

override fun doLogIn(activity: Activity): Boolean {
    var isLoggedIn = false

    auth.signInWithEmailAndPassword(email, password)
        .addOnCompleteListener(activity) { task ->            // this line is skipped when debugging
            if (task.isSuccessful) {
                // Sign in success, update UI with the signed-in user's information
                Log.d(TAG, "signInWithEmail:success")
                isLoggedIn = true
            } else {
                // If sign in fails, display a message to the user.
                Log.w(TAG, "signInWithEmail:failure", task.exception)
            }
        }
    return isLoggedIn
}

Upvotes: 0

Views: 83

Answers (0)

Related Questions