Show auth errors to the user
This commit is contained in:
@@ -8,10 +8,11 @@ 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
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import gq.kirmanak.mealient.R
|
||||
import gq.kirmanak.mealient.data.auth.impl.AuthenticationError.*
|
||||
import gq.kirmanak.mealient.databinding.FragmentAuthenticationBinding
|
||||
import timber.log.Timber
|
||||
|
||||
@@ -59,30 +60,35 @@ class AuthenticationFragment : Fragment() {
|
||||
findNavController().navigate(AuthenticationFragmentDirections.actionAuthenticationFragmentToRecipesFragment())
|
||||
}
|
||||
|
||||
private fun onLoginClicked() {
|
||||
private fun onLoginClicked(): Unit = with(binding) {
|
||||
Timber.v("onLoginClicked() called")
|
||||
val email: String
|
||||
val pass: String
|
||||
val url: String
|
||||
with(binding) {
|
||||
email = checkIfInputIsEmpty(emailInput, emailInputLayout) {
|
||||
"Email is empty"
|
||||
} ?: return
|
||||
pass = checkIfInputIsEmpty(passwordInput, passwordInputLayout) {
|
||||
"Pass is empty"
|
||||
} ?: return
|
||||
url = checkIfInputIsEmpty(urlInput, urlInputLayout) {
|
||||
"URL is empty"
|
||||
} ?: return
|
||||
}
|
||||
binding.button.isClickable = false
|
||||
viewLifecycleOwner.lifecycleScope.launchWhenResumed {
|
||||
runCatching {
|
||||
viewModel.authenticate(email, pass, url)
|
||||
}.onFailure {
|
||||
Timber.e(it, "Can't authenticate")
|
||||
|
||||
val email: String = checkIfInputIsEmpty(emailInput, emailInputLayout) {
|
||||
getString(R.string.fragment_authentication_email_input_empty)
|
||||
} ?: return
|
||||
|
||||
val pass: String = checkIfInputIsEmpty(passwordInput, passwordInputLayout) {
|
||||
getString(R.string.fragment_authentication_password_input_empty)
|
||||
} ?: return
|
||||
|
||||
val url: String = checkIfInputIsEmpty(urlInput, urlInputLayout) {
|
||||
getString(R.string.fragment_authentication_url_input_empty)
|
||||
} ?: return
|
||||
|
||||
button.isClickable = false
|
||||
viewModel.authenticate(email, pass, url).observe(viewLifecycleOwner) {
|
||||
Timber.d("onLoginClicked: result $it")
|
||||
passwordInputLayout.error = when (it.exceptionOrNull()) {
|
||||
is Unauthorized -> getString(R.string.fragment_authentication_credentials_incorrect)
|
||||
else -> null
|
||||
}
|
||||
binding.button.isClickable = true
|
||||
urlInputLayout.error = when (it.exceptionOrNull()) {
|
||||
is NoServerConnection -> getString(R.string.fragment_authentication_no_connection)
|
||||
is NotMealie -> getString(R.string.fragment_authentication_unexpected_response)
|
||||
is Unauthorized, null -> null
|
||||
else -> getString(R.string.fragment_authentication_unknown_error)
|
||||
}
|
||||
button.isClickable = true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package gq.kirmanak.mealient.ui.auth
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.asLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.lifecycle.*
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import gq.kirmanak.mealient.data.auth.AuthRepo
|
||||
import gq.kirmanak.mealient.data.recipes.RecipeRepo
|
||||
@@ -16,13 +13,22 @@ class AuthenticationViewModel @Inject constructor(
|
||||
private val authRepo: AuthRepo,
|
||||
private val recipeRepo: RecipeRepo
|
||||
) : ViewModel() {
|
||||
init {
|
||||
Timber.v("constructor called")
|
||||
}
|
||||
|
||||
suspend fun authenticate(username: String, password: String, baseUrl: String) {
|
||||
fun authenticate(username: String, password: String, baseUrl: String): LiveData<Result<Unit>> {
|
||||
Timber.v("authenticate() called with: username = $username, password = $password, baseUrl = $baseUrl")
|
||||
authRepo.authenticate(username, password, baseUrl)
|
||||
val result = MutableLiveData<Result<Unit>>()
|
||||
viewModelScope.launch {
|
||||
runCatching {
|
||||
authRepo.authenticate(username, password, baseUrl)
|
||||
}.onFailure {
|
||||
Timber.e(it, "authenticate: can't authenticate")
|
||||
result.value = Result.failure(it)
|
||||
}.onSuccess {
|
||||
Timber.d("authenticate: authenticated")
|
||||
result.value = Result.success(Unit)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun authenticationStatuses(): LiveData<Boolean> {
|
||||
|
||||
@@ -14,4 +14,11 @@
|
||||
<string name="fragment_disclaimer_button_okay">Хорошо</string>
|
||||
<string name="fragment_disclaimer_header">ДИСКЛЕЙМЕР</string>
|
||||
<string name="fragment_disclaimer_main_text">Этот проект разрабатывается независимо от основного проекта Meale. Он не связан с разработчиками Mealie. О любых проблемах следует писать в репозиторий Mealient, НЕ в репозиторий Mealie.</string>
|
||||
<string name="fragment_authentication_email_input_empty">E-mail не может быть пустым</string>
|
||||
<string name="fragment_authentication_password_input_empty">Пароль не может быть пустым</string>
|
||||
<string name="fragment_authentication_url_input_empty">URL не может быть пустым</string>
|
||||
<string name="fragment_authentication_credentials_incorrect">E-mail или пароль не подходит.</string>
|
||||
<string name="fragment_authentication_no_connection">Ошибка подключения, проверьте адрес.</string>
|
||||
<string name="fragment_authentication_unexpected_response">Неожиданный ответ. Это Mealie?</string>
|
||||
<string name="fragment_authentication_unknown_error">Что-то пошло не так, попробуйте еще раз.</string>
|
||||
</resources>
|
||||
@@ -16,4 +16,11 @@
|
||||
<string name="fragment_disclaimer_header">DISCLAIMER</string>
|
||||
<string name="fragment_disclaimer_button_okay">Okay</string>
|
||||
<string name="view_holder_recipe_instructions_step">Step: %d</string>
|
||||
<string name="fragment_authentication_email_input_empty">E-mail can\'t be empty</string>
|
||||
<string name="fragment_authentication_password_input_empty">Password can\'t be empty</string>
|
||||
<string name="fragment_authentication_url_input_empty">URL can\'t be empty</string>
|
||||
<string name="fragment_authentication_credentials_incorrect">E-mail or password is incorrect.</string>
|
||||
<string name="fragment_authentication_no_connection">Can\'t connect, check address.</string>
|
||||
<string name="fragment_authentication_unexpected_response">Unexpected response. Is it Mealie?</string>
|
||||
<string name="fragment_authentication_unknown_error">Something went wrong, please try again.</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user