Fix IllegalStateException when clicking login after logout
The previous login result was stored as live data and prevented AuthenticationFragment from being shown properly. However, an attempt to destroy RecipesFragment was made. This attempt caused IllegalStateException when accessing view in onDestroyView.
This commit is contained in:
@@ -14,6 +14,7 @@ import gq.kirmanak.mealient.data.network.NetworkError
|
||||
import gq.kirmanak.mealient.databinding.FragmentAuthenticationBinding
|
||||
import gq.kirmanak.mealient.extensions.checkIfInputIsEmpty
|
||||
import gq.kirmanak.mealient.extensions.executeOnceOnBackPressed
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -33,7 +34,6 @@ class AuthenticationFragment : Fragment(R.layout.fragment_authentication) {
|
||||
binding.button.setOnClickListener { onLoginClicked() }
|
||||
(requireActivity() as? AppCompatActivity)?.supportActionBar?.title =
|
||||
getString(R.string.app_name)
|
||||
viewModel.authenticationResult.observe(viewLifecycleOwner, ::onAuthenticationResult)
|
||||
}
|
||||
|
||||
private fun onLoginClicked(): Unit = with(binding) {
|
||||
@@ -48,7 +48,9 @@ class AuthenticationFragment : Fragment(R.layout.fragment_authentication) {
|
||||
} ?: return
|
||||
|
||||
button.isClickable = false
|
||||
viewModel.authenticate(email, pass)
|
||||
viewLifecycleOwner.lifecycleScope.launch {
|
||||
onAuthenticationResult(viewModel.authenticate(email, pass))
|
||||
}
|
||||
}
|
||||
|
||||
private fun onAuthenticationResult(result: Result<Unit>) {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package gq.kirmanak.mealient.ui.auth
|
||||
|
||||
import androidx.lifecycle.*
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.asLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import gq.kirmanak.mealient.data.auth.AuthRepo
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
@@ -28,10 +31,6 @@ class AuthenticationViewModel @Inject constructor(
|
||||
var authRequested: Boolean by authRequestsFlow::value
|
||||
var showLoginButton: Boolean by showLoginButtonFlow::value
|
||||
|
||||
private val _authenticationResult = MutableLiveData<Result<Unit>>()
|
||||
val authenticationResult: LiveData<Result<Unit>>
|
||||
get() = _authenticationResult
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
authRequestsFlow.collect { isRequested ->
|
||||
@@ -41,18 +40,9 @@ class AuthenticationViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun authenticate(username: String, password: String) {
|
||||
Timber.v("authenticate() called with: username = $username, password = $password")
|
||||
viewModelScope.launch {
|
||||
runCatching {
|
||||
authRepo.authenticate(username, password)
|
||||
}.onFailure {
|
||||
Timber.e(it, "authenticate: can't authenticate")
|
||||
_authenticationResult.value = Result.failure(it)
|
||||
}.onSuccess {
|
||||
Timber.d("authenticate: authenticated")
|
||||
_authenticationResult.value = Result.success(Unit)
|
||||
}
|
||||
}
|
||||
suspend fun authenticate(username: String, password: String): Result<Unit> = runCatching {
|
||||
authRepo.authenticate(username, password)
|
||||
}.onFailure {
|
||||
Timber.e(it, "authenticate: can't authenticate")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user