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.Fragment
|
||||||
import androidx.fragment.app.viewModels
|
import androidx.fragment.app.viewModels
|
||||||
import androidx.lifecycle.Observer
|
import androidx.lifecycle.Observer
|
||||||
import androidx.lifecycle.lifecycleScope
|
|
||||||
import androidx.navigation.fragment.findNavController
|
import androidx.navigation.fragment.findNavController
|
||||||
import com.google.android.material.textfield.TextInputLayout
|
import com.google.android.material.textfield.TextInputLayout
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
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 gq.kirmanak.mealient.databinding.FragmentAuthenticationBinding
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
@@ -59,30 +60,35 @@ class AuthenticationFragment : Fragment() {
|
|||||||
findNavController().navigate(AuthenticationFragmentDirections.actionAuthenticationFragmentToRecipesFragment())
|
findNavController().navigate(AuthenticationFragmentDirections.actionAuthenticationFragmentToRecipesFragment())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onLoginClicked() {
|
private fun onLoginClicked(): Unit = with(binding) {
|
||||||
Timber.v("onLoginClicked() called")
|
Timber.v("onLoginClicked() called")
|
||||||
val email: String
|
|
||||||
val pass: String
|
val email: String = checkIfInputIsEmpty(emailInput, emailInputLayout) {
|
||||||
val url: String
|
getString(R.string.fragment_authentication_email_input_empty)
|
||||||
with(binding) {
|
} ?: return
|
||||||
email = checkIfInputIsEmpty(emailInput, emailInputLayout) {
|
|
||||||
"Email is empty"
|
val pass: String = checkIfInputIsEmpty(passwordInput, passwordInputLayout) {
|
||||||
} ?: return
|
getString(R.string.fragment_authentication_password_input_empty)
|
||||||
pass = checkIfInputIsEmpty(passwordInput, passwordInputLayout) {
|
} ?: return
|
||||||
"Pass is empty"
|
|
||||||
} ?: return
|
val url: String = checkIfInputIsEmpty(urlInput, urlInputLayout) {
|
||||||
url = checkIfInputIsEmpty(urlInput, urlInputLayout) {
|
getString(R.string.fragment_authentication_url_input_empty)
|
||||||
"URL is empty"
|
} ?: return
|
||||||
} ?: return
|
|
||||||
}
|
button.isClickable = false
|
||||||
binding.button.isClickable = false
|
viewModel.authenticate(email, pass, url).observe(viewLifecycleOwner) {
|
||||||
viewLifecycleOwner.lifecycleScope.launchWhenResumed {
|
Timber.d("onLoginClicked: result $it")
|
||||||
runCatching {
|
passwordInputLayout.error = when (it.exceptionOrNull()) {
|
||||||
viewModel.authenticate(email, pass, url)
|
is Unauthorized -> getString(R.string.fragment_authentication_credentials_incorrect)
|
||||||
}.onFailure {
|
else -> null
|
||||||
Timber.e(it, "Can't authenticate")
|
|
||||||
}
|
}
|
||||||
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
|
package gq.kirmanak.mealient.ui.auth
|
||||||
|
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.*
|
||||||
import androidx.lifecycle.ViewModel
|
|
||||||
import androidx.lifecycle.asLiveData
|
|
||||||
import androidx.lifecycle.viewModelScope
|
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import gq.kirmanak.mealient.data.auth.AuthRepo
|
import gq.kirmanak.mealient.data.auth.AuthRepo
|
||||||
import gq.kirmanak.mealient.data.recipes.RecipeRepo
|
import gq.kirmanak.mealient.data.recipes.RecipeRepo
|
||||||
@@ -16,13 +13,22 @@ class AuthenticationViewModel @Inject constructor(
|
|||||||
private val authRepo: AuthRepo,
|
private val authRepo: AuthRepo,
|
||||||
private val recipeRepo: RecipeRepo
|
private val recipeRepo: RecipeRepo
|
||||||
) : ViewModel() {
|
) : 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")
|
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> {
|
fun authenticationStatuses(): LiveData<Boolean> {
|
||||||
|
|||||||
@@ -14,4 +14,11 @@
|
|||||||
<string name="fragment_disclaimer_button_okay">Хорошо</string>
|
<string name="fragment_disclaimer_button_okay">Хорошо</string>
|
||||||
<string name="fragment_disclaimer_header">ДИСКЛЕЙМЕР</string>
|
<string name="fragment_disclaimer_header">ДИСКЛЕЙМЕР</string>
|
||||||
<string name="fragment_disclaimer_main_text">Этот проект разрабатывается независимо от основного проекта Meale. Он не связан с разработчиками Mealie. О любых проблемах следует писать в репозиторий Mealient, НЕ в репозиторий Mealie.</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>
|
</resources>
|
||||||
@@ -16,4 +16,11 @@
|
|||||||
<string name="fragment_disclaimer_header">DISCLAIMER</string>
|
<string name="fragment_disclaimer_header">DISCLAIMER</string>
|
||||||
<string name="fragment_disclaimer_button_okay">Okay</string>
|
<string name="fragment_disclaimer_button_okay">Okay</string>
|
||||||
<string name="view_holder_recipe_instructions_step">Step: %d</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>
|
</resources>
|
||||||
Reference in New Issue
Block a user