fix: Исправление текста ошибки при неправильных данных
This commit is contained in:
parent
8aa252ea12
commit
f8bc61d090
@ -1,5 +1,6 @@
|
||||
package ru.myitschool.work.data.login
|
||||
|
||||
import android.content.Context
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.request.basicAuth
|
||||
import io.ktor.client.request.post
|
||||
@ -8,10 +9,13 @@ import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.http.headers
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import ru.myitschool.work.R
|
||||
import ru.myitschool.work.core.Constants
|
||||
import ru.myitschool.work.utils.NetworkModule
|
||||
|
||||
class LoginNetworkDataSource {
|
||||
class LoginNetworkDataSource(
|
||||
private val context: Context
|
||||
) {
|
||||
private val client = NetworkModule.httpClient
|
||||
suspend fun login(username: String, password: String):Result<Unit> = withContext(Dispatchers.IO){
|
||||
runCatching {
|
||||
@ -21,7 +25,10 @@ class LoginNetworkDataSource {
|
||||
basicAuth(username, password)
|
||||
}
|
||||
}
|
||||
if (result.status != HttpStatusCode.OK) {
|
||||
if(result.status == HttpStatusCode.Unauthorized){
|
||||
error(context.getString(R.string.login_unauthorized))
|
||||
}
|
||||
else if (result.status != HttpStatusCode.OK) {
|
||||
error("Status ${result.status}")
|
||||
}
|
||||
println(result.bodyAsText())
|
||||
|
@ -49,8 +49,10 @@ class LoginFragment : Fragment(R.layout.fragment_login) {
|
||||
when(state){
|
||||
is LoginViewModel.State.Error -> {
|
||||
error.visibility = View.VISIBLE
|
||||
error.text = state.message
|
||||
loading.visibility = View.GONE
|
||||
username.isEnabled = true
|
||||
|
||||
}
|
||||
is LoginViewModel.State.Idle -> {
|
||||
loading.visibility = View.GONE
|
||||
|
@ -31,7 +31,6 @@ class LoginViewModel(
|
||||
if(username != "" && password != "")
|
||||
login(username, password)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
sealed class State {
|
||||
@ -63,7 +62,9 @@ class LoginViewModel(
|
||||
val Factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory {
|
||||
override fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T {
|
||||
val repoImpl = LoginRepoImpl(
|
||||
networkDataSource = LoginNetworkDataSource()
|
||||
networkDataSource = LoginNetworkDataSource(
|
||||
context = extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as Application
|
||||
)
|
||||
)
|
||||
|
||||
val useCase = LoginUseCase(repoImpl)
|
||||
|
Loading…
x
Reference in New Issue
Block a user