Fix handling result in BaseURLFragment and AuthenticationFragment

This commit is contained in:
Kirill Kamakin
2022-04-08 21:19:05 +05:00
parent 5b56ff9932
commit d2029438d7
10 changed files with 77 additions and 45 deletions

View File

@@ -4,7 +4,7 @@ import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import by.kirich1409.viewbindingdelegate.viewBinding
import dagger.hilt.android.AndroidEntryPoint
@@ -12,13 +12,12 @@ import gq.kirmanak.mealient.R
import gq.kirmanak.mealient.data.network.NetworkError
import gq.kirmanak.mealient.databinding.FragmentAuthenticationBinding
import gq.kirmanak.mealient.extensions.checkIfInputIsEmpty
import gq.kirmanak.mealient.extensions.launchWithViewLifecycle
import timber.log.Timber
@AndroidEntryPoint
class AuthenticationFragment : Fragment(R.layout.fragment_authentication) {
private val binding by viewBinding(FragmentAuthenticationBinding::bind)
private val viewModel by activityViewModels<AuthenticationViewModel>()
private val viewModel by viewModels<AuthenticationViewModel>()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
@@ -26,6 +25,7 @@ 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) {
@@ -45,7 +45,7 @@ class AuthenticationFragment : Fragment(R.layout.fragment_authentication) {
) ?: return
button.isClickable = false
launchWithViewLifecycle { onAuthenticationResult(viewModel.authenticate(email, pass)) }
viewModel.authenticate(email, pass)
}
private fun onAuthenticationResult(result: Result<Unit>) {

View File

@@ -5,7 +5,7 @@ import timber.log.Timber
enum class AuthenticationState {
AUTHORIZED,
UNAUTHORIZED,
UNKNOWN;
HIDDEN;
companion object {
@@ -15,7 +15,7 @@ enum class AuthenticationState {
): AuthenticationState {
Timber.v("determineState() called with: showLoginButton = $showLoginButton, isAuthorized = $isAuthorized")
val result = when {
!showLoginButton -> UNKNOWN
!showLoginButton -> HIDDEN
isAuthorized -> AUTHORIZED
else -> UNAUTHORIZED
}

View File

@@ -1,14 +1,12 @@
package gq.kirmanak.mealient.ui.auth
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
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 gq.kirmanak.mealient.extensions.runCatchingExceptCancel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
@@ -18,24 +16,16 @@ class AuthenticationViewModel @Inject constructor(
private val authRepo: AuthRepo,
) : ViewModel() {
private val showLoginButtonFlow = MutableStateFlow(false)
private val authenticationStateFlow = combine(
showLoginButtonFlow,
authRepo.isAuthorizedFlow,
AuthenticationState::determineState
)
val authenticationStateLive: LiveData<AuthenticationState>
get() = authenticationStateFlow.asLiveData()
var showLoginButton: Boolean by showLoginButtonFlow::value
private val _authenticationResult = MutableLiveData<Result<Unit>>()
val authenticationResult: LiveData<Result<Unit>>
get() = _authenticationResult
suspend fun authenticate(email: String, password: String) = runCatchingExceptCancel {
authRepo.authenticate(email, password)
}.onFailure {
Timber.e(it, "authenticate: can't authenticate")
}
fun logout() {
Timber.v("logout() called")
viewModelScope.launch { authRepo.logout() }
fun authenticate(email: String, password: String) {
Timber.v("authenticate() called with: email = $email, password = $password")
viewModelScope.launch {
_authenticationResult.value = runCatchingExceptCancel {
authRepo.authenticate(email, password)
}
}
}
}