Florian
10/23/2020, 8:11 AMmainViewModel.sessionManager.firebaseUser.observe(viewLifecycleOwner) {
if (it != null) {
previousSavedStateHandle.set(LOGIN_SUCCESSFUL, true)
binding.editTextEmail.clearFocus()
binding.editTextPassword.clearFocus()
findNavController().popBackStack()
}
}
The login method in the ViewModel looks like this:
fun login(email: String, password: String) {
if (email.isEmpty() || password.isEmpty()) {
viewModelScope.launch {
snackBarMessage.send("Please fill out all fields")
}
return
}
viewModelScope.launch {
_loginLoading.value = true
when (val result = authRepository.login(email, password)) {
is AsyncResult.Success -> {
val firebaseUser = sessionManager.firebaseUser.value!!
snackBarMessage.send("Logged in as ${firebaseUser.displayName}")
}
is AsyncResult.Error -> snackBarMessage.send(
result.throwable.localizedMessage ?: "Something went wrong"
)
}
_loginLoading.value = false
}
}
My question is about that confirmation snackbar ("Logged in as ${firebaseUser.displayName}"
). Does the code that triggers the snackbar belong into the when statement inside the ViewModel (where it is right now), or can/should I put it into the if (it != null)
block inside the fragment? I'm asking this because the latter option would get rid of the problem that the snackbar doesn't arrive early enough before the fragment is popped from the backstack. But I am not sure if showing a snackbar is considered "business logic" here and should be in the ViewModel.
(The snackbar event is send via a Kotlin channel here that the fragment reacts to)Javier
10/23/2020, 8:39 AMViewModel
emit states, view observes and paint themFlorian
10/23/2020, 8:55 AMSnackbar.make
into the if (it != null)
block?Javier
10/23/2020, 9:11 AMit
is not nullFlorian
10/23/2020, 9:16 AMJavier
10/23/2020, 9:23 AMFlorian
10/23/2020, 9:30 AM"Logged in as ${firebaseUser.displayName}"
). Would it be wrong to put that directly into the observe method?Florian
10/23/2020, 9:31 AMJavier
10/23/2020, 9:50 AMFlorian
10/23/2020, 9:56 AMFlorian
10/23/2020, 9:56 AMFlorian
10/23/2020, 10:52 AMFrancisco
10/23/2020, 1:55 PMif != null
. If you canât, then you might send it
to the VM, and have the VM ask the fragment to pop the snackbar if itâs not null.Florian
10/23/2020, 2:22 PM!= null
checkFlorian
10/23/2020, 2:22 PMFlorian
10/23/2020, 2:22 PMFrancisco
10/23/2020, 2:32 PMFrancisco
10/23/2020, 2:35 PMFlorian
10/23/2020, 2:48 PMFlorian
10/23/2020, 2:53 PMisLoggedIn
LiveData into the sessionManager directly? This way I don't have to create a LiveData for each fragmentFrancisco
10/23/2020, 3:45 PMFrancisco
10/23/2020, 3:48 PMFlorian
11/18/2020, 8:35 PM