Fix calling navigateToAuth twice and even more

Calling that method several times in a row is not supported,
Navigation library throws an Exception saying it doesn't know how
to navigate there
This commit is contained in:
Kirill Kamakin
2021-11-20 22:07:43 +03:00
parent 3cc3d1f174
commit a6e948ca6b
3 changed files with 39 additions and 28 deletions

View File

@@ -7,6 +7,7 @@ import android.view.ViewGroup
import android.widget.EditText
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.google.android.material.textfield.TextInputLayout
@@ -21,18 +22,20 @@ class AuthenticationFragment : Fragment() {
get() = checkNotNull(_binding) { "Binding requested when fragment is off screen" }
private val viewModel by viewModels<AuthenticationViewModel>()
private val authStatuses by lazy { viewModel.authenticationStatuses() }
private val authStatusObserver = Observer<Boolean> { onAuthStatusChange(it) }
private fun onAuthStatusChange(isAuthenticated: Boolean) {
Timber.v("onAuthStatusChange() called with: isAuthenticated = $isAuthenticated")
if (isAuthenticated) {
authStatuses.removeObserver(authStatusObserver)
navigateToRecipes()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Timber.v("onCreate() called with: savedInstanceState = $savedInstanceState")
listenToAuthenticationStatuses()
}
private fun listenToAuthenticationStatuses() {
Timber.d("listenToAuthenticationStatuses() called")
viewModel.authenticationStatuses().observe(this) {
Timber.d("listenToAuthenticationStatuses: new status = $it")
if (it) navigateToRecipes()
}
authStatuses.observe(this, authStatusObserver)
}
override fun onCreateView(