viewModel.getFactToDisplay is called before the viewModel.authenticationState.observe call intends to return either an androidFact or a californiaFact. Unfortunately, the FirebaseUserLiveData().map function assigning LoginViewModel#authenticationState is only executed when the LoginViewModel is observed. For this reason, authenticationState.value returns null and viewModel.getFactToDisplay returns a california fact instead of the intended android fact by default.
A possible solution is to move the initialization of factToDisplay inside the observe statement
private fun observeAuthenticationState() {
// val factToDisplay = viewModel.getFactToDisplay(requireContext())
viewModel.authenticationState.observe(viewLifecycleOwner, Observer { authenticationState ->
// move inside observer statement so the the FirebaseUserLiveData().map function in LoginViewModel can be executed.
val factToDisplay = viewModel.getFactToDisplay(requireContext())
when (authenticationState) {
LoginViewModel.AuthenticationState.AUTHENTICATED -> {
binding.welcomeText.text = getFactWithPersonalization(factToDisplay)
binding.authButton.text = getString(R.string.logout_button_text)
binding.authButton.setOnClickListener {
AuthUI.getInstance().signOut(requireContext())
}
}
else -> {
binding.welcomeText.text = factToDisplay
binding.authButton.text = getString(R.string.login_button_text)
binding.authButton.setOnClickListener {
findNavController().navigate(R.id.loginFragment)
}
}
}
})
}
How to reproduce?
What are the exact steps to reproduce the problem?
Problem 1. Compile the solution project from Master branch, a compiler error will be shown.
Problem 2. Run the compiled solution project from Master branch, a california fact is displayed by default instead of an android fact.
Versions
What version of Android Studio are you using?
Android Studio 4.0.1
What API level are you targeting?
API 29 , API 30
Additional information
Compile error, and Califonia Fact is shown when not logged in or after log out
Describe the problem
since
authenticationState is a LiveData<Enum>
, the value property shall be used to compare with enum otherwise code will not compile.MainFragment#observeAuthenticationState contains logical error
viewModel.getFactToDisplay
is called before theviewModel.authenticationState.observe
call intends to return either an androidFact or a californiaFact. Unfortunately, theFirebaseUserLiveData().map
function assigningLoginViewModel#authenticationState
is only executed when theLoginViewModel
is observed. For this reason,authenticationState.value
returns null andviewModel.getFactToDisplay
returns a california fact instead of the intended android fact by default.A possible solution is to move the initialization of
factToDisplay
inside the observe statementHow to reproduce? What are the exact steps to reproduce the problem? Problem 1. Compile the solution project from Master branch, a compiler error will be shown. Problem 2. Run the compiled solution project from Master branch, a california fact is displayed by default instead of an android fact.
Versions
What version of Android Studio are you using? Android Studio 4.0.1
What API level are you targeting? API 29 , API 30
Additional information Compile error, and Califonia Fact is shown when not logged in or after log out
codelab: advanced-android-kotlin