Extract Base URL from authentication
This commit is contained in:
@@ -2,7 +2,6 @@ package gq.kirmanak.mealient.ui.auth
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.widget.EditText
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.viewModels
|
||||
@@ -10,14 +9,11 @@ import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import by.kirich1409.viewbindingdelegate.viewBinding
|
||||
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.data.network.NetworkError.Unauthorized
|
||||
import gq.kirmanak.mealient.databinding.FragmentAuthenticationBinding
|
||||
import gq.kirmanak.mealient.ui.textChangesFlow
|
||||
import kotlinx.coroutines.flow.filterNotNull
|
||||
import kotlinx.coroutines.flow.first
|
||||
import gq.kirmanak.mealient.ui.checkIfInputIsEmpty
|
||||
import timber.log.Timber
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -57,61 +53,23 @@ class AuthenticationFragment : Fragment(R.layout.fragment_authentication) {
|
||||
private fun onLoginClicked(): Unit = with(binding) {
|
||||
Timber.v("onLoginClicked() called")
|
||||
|
||||
val email: String = checkIfInputIsEmpty(emailInput, emailInputLayout) {
|
||||
val email: String = emailInput.checkIfInputIsEmpty(emailInputLayout, lifecycleScope) {
|
||||
getString(R.string.fragment_authentication_email_input_empty)
|
||||
} ?: return
|
||||
|
||||
val pass: String = checkIfInputIsEmpty(passwordInput, passwordInputLayout) {
|
||||
val pass: String = passwordInput.checkIfInputIsEmpty(passwordInputLayout, lifecycleScope) {
|
||||
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) {
|
||||
viewModel.authenticate(email, pass).observe(viewLifecycleOwner) {
|
||||
Timber.d("onLoginClicked: result $it")
|
||||
passwordInputLayout.error = when (it.exceptionOrNull()) {
|
||||
is Unauthorized -> getString(R.string.fragment_authentication_credentials_incorrect)
|
||||
else -> null
|
||||
}
|
||||
urlInputLayout.error = when (val exception = it.exceptionOrNull()) {
|
||||
is NoServerConnection -> getString(R.string.fragment_authentication_no_connection)
|
||||
is NotMealie -> getString(R.string.fragment_authentication_unexpected_response)
|
||||
is MalformedUrl -> {
|
||||
val cause = exception.cause?.message ?: exception.message
|
||||
getString(R.string.fragment_authentication_url_invalid, cause)
|
||||
}
|
||||
is Unauthorized, null -> null
|
||||
else -> getString(R.string.fragment_authentication_unknown_error)
|
||||
}
|
||||
|
||||
button.isClickable = true
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkIfInputIsEmpty(
|
||||
input: EditText,
|
||||
inputLayout: TextInputLayout,
|
||||
errorText: () -> String
|
||||
): String? {
|
||||
Timber.v("checkIfInputIsEmpty() called with: input = $input, inputLayout = $inputLayout, errorText = $errorText")
|
||||
val text = input.text?.toString()
|
||||
Timber.d("Input text is \"$text\"")
|
||||
if (text.isNullOrEmpty()) {
|
||||
inputLayout.error = errorText()
|
||||
viewLifecycleOwner.lifecycleScope.launchWhenResumed {
|
||||
waitUntilNotEmpty(input)
|
||||
inputLayout.error = null
|
||||
}
|
||||
return null
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
private suspend fun waitUntilNotEmpty(input: EditText) {
|
||||
Timber.v("waitUntilNotEmpty() called with: input = $input")
|
||||
input.textChangesFlow().filterNotNull().first { it.isNotEmpty() }
|
||||
Timber.v("waitUntilNotEmpty() returned")
|
||||
}
|
||||
}
|
||||
@@ -14,12 +14,12 @@ class AuthenticationViewModel @Inject constructor(
|
||||
private val recipeRepo: RecipeRepo
|
||||
) : ViewModel() {
|
||||
|
||||
fun authenticate(username: String, password: String, baseUrl: String): LiveData<Result<Unit>> {
|
||||
Timber.v("authenticate() called with: username = $username, password = $password, baseUrl = $baseUrl")
|
||||
fun authenticate(username: String, password: String): LiveData<Result<Unit>> {
|
||||
Timber.v("authenticate() called with: username = $username, password = $password")
|
||||
val result = MutableLiveData<Result<Unit>>()
|
||||
viewModelScope.launch {
|
||||
runCatching {
|
||||
authRepo.authenticate(username, password, baseUrl)
|
||||
authRepo.authenticate(username, password)
|
||||
}.onFailure {
|
||||
Timber.e(it, "authenticate: can't authenticate")
|
||||
result.value = Result.failure(it)
|
||||
|
||||
Reference in New Issue
Block a user