bugfix, added login exception logic
This commit is contained in:
parent
bfb54c8e27
commit
9cc7462ad2
@ -22,7 +22,6 @@ class GateServiceST {
|
|||||||
}
|
}
|
||||||
private val gateRepository: GateRepository = GateRepository()
|
private val gateRepository: GateRepository = GateRepository()
|
||||||
suspend fun openDoor(code: String): Result<Int> {
|
suspend fun openDoor(code: String): Result<Int> {
|
||||||
Log.d("1234", UserServiceST.getInstance().getTokenPair().accessToken)
|
return gateRepository.openDoor(UserServiceST.getInstance().getTokenPair()?.accessToken!!, code = code)
|
||||||
return gateRepository.openDoor(UserServiceST.getInstance().getTokenPair().accessToken, code = code)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -28,7 +28,7 @@ class VisitServiceST {
|
|||||||
if (!UserServiceST.getInstance().hasTokens()) {
|
if (!UserServiceST.getInstance().hasTokens()) {
|
||||||
throw RuntimeException("access token is null")
|
throw RuntimeException("access token is null")
|
||||||
}
|
}
|
||||||
return visitRepository.getLastVisitsByLogin(pageNum, pageSize, UserServiceST.getInstance().getTokenPair().accessToken, login).map { pagingDto -> pagingDto.content }
|
return visitRepository.getLastVisitsByLogin(pageNum, pageSize, UserServiceST.getInstance().getTokenPair()!!.accessToken, login).map { pagingDto -> pagingDto.content }
|
||||||
}
|
}
|
||||||
suspend fun getMyLastVisits(pageNum: Int,
|
suspend fun getMyLastVisits(pageNum: Int,
|
||||||
pageSize: Int): Result<List<VisitDto>> {
|
pageSize: Int): Result<List<VisitDto>> {
|
||||||
@ -39,7 +39,7 @@ class VisitServiceST {
|
|||||||
return visitRepository.getMyLastVisits(
|
return visitRepository.getMyLastVisits(
|
||||||
pageNum = pageNum,
|
pageNum = pageNum,
|
||||||
pageSize = pageSize,
|
pageSize = pageSize,
|
||||||
token = UserServiceST.getInstance().getTokenPair().accessToken
|
token = UserServiceST.getInstance().getTokenPair()!!.accessToken
|
||||||
).map { pagingDto -> pagingDto.content }
|
).map { pagingDto -> pagingDto.content }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -41,8 +41,8 @@ class UserServiceST(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fun getTokenPair(): AuthTokenPair {
|
fun getTokenPair(): AuthTokenPair? {
|
||||||
return tokenManager.authTokenPair!!
|
return tokenManager.authTokenPair
|
||||||
}
|
}
|
||||||
fun hasTokens(): Boolean {
|
fun hasTokens(): Boolean {
|
||||||
return tokenManager.hasTokens()
|
return tokenManager.hasTokens()
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.displaynone.acss.components.auth.models.user.repository
|
package com.displaynone.acss.components.auth.models.user.repository
|
||||||
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import com.bumptech.glide.load.HttpException
|
||||||
import com.displaynone.acss.components.auth.models.AuthTokenPair
|
import com.displaynone.acss.components.auth.models.AuthTokenPair
|
||||||
import com.displaynone.acss.config.Constants.serverUrl
|
import com.displaynone.acss.config.Constants.serverUrl
|
||||||
import com.displaynone.acss.config.Network
|
import com.displaynone.acss.config.Network
|
||||||
@ -45,16 +46,14 @@ class UserRepository(
|
|||||||
setBody(UserLoginDto(login, password))
|
setBody(UserLoginDto(login, password))
|
||||||
}
|
}
|
||||||
if (result.status != HttpStatusCode.OK) {
|
if (result.status != HttpStatusCode.OK) {
|
||||||
error("Status ${result.status}: ${result.body<String>()}")
|
error(result.status)
|
||||||
}
|
}
|
||||||
// val gson = Gson()
|
// val gson = Gson()
|
||||||
// val tokenPair = gson.fromJson(result.bodyAsText(), AuthTokenPair::class.java)
|
// val tokenPair = gson.fromJson(result.bodyAsText(), AuthTokenPair::class.java)
|
||||||
Log.d("UserRepository", result.bodyAsText())
|
Log.d("UserRepository", result.bodyAsText())
|
||||||
// result.body()
|
// result.body()
|
||||||
val tokenPair = Json.decodeFromString<AuthTokenPair>(result.bodyAsText())
|
val tokenPair = Json.decodeFromString<AuthTokenPair>(result.bodyAsText())
|
||||||
Result.success(tokenPair)
|
tokenPair
|
||||||
}.getOrElse { exception ->
|
|
||||||
Result.failure(exception)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
suspend fun openDoor(token: String, code: String): Result<Int> = withContext(Dispatchers.IO){
|
suspend fun openDoor(token: String, code: String): Result<Int> = withContext(Dispatchers.IO){
|
||||||
|
@ -20,6 +20,6 @@ class InitServiceST {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
suspend fun ping(): Result<Boolean> {
|
suspend fun ping(): Result<Boolean> {
|
||||||
return initRepository.ping(UserServiceST.getInstance().getTokenPair().accessToken)
|
return initRepository.ping()
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,13 +16,10 @@ import kotlinx.coroutines.Dispatchers
|
|||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
class InitRepository {
|
class InitRepository {
|
||||||
suspend fun ping(token: String): Result<Boolean> = withContext(
|
suspend fun ping(): Result<Boolean> = withContext(
|
||||||
Dispatchers.IO){
|
Dispatchers.IO){
|
||||||
runCatching {
|
runCatching {
|
||||||
val result = Network.client.get("$serverUrl/api/utils/ping") {
|
val result = Network.client.get("$serverUrl/api/utils/ping") {
|
||||||
headers {
|
|
||||||
append(HttpHeaders.Authorization, "Bearer $token")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result.status == HttpStatusCode.OK
|
result.status == HttpStatusCode.OK
|
||||||
|
@ -28,17 +28,16 @@ class AuthFragment: Fragment(R.layout.fragment_auth) {
|
|||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
_binding = FragmentAuthBinding.bind(view)
|
_binding = FragmentAuthBinding.bind(view)
|
||||||
setupLoginButton()
|
|
||||||
viewModel.action.collectWithLifecycle(this) { action ->
|
viewModel.action.collectWithLifecycle(this) { action ->
|
||||||
if (action is Action.GotoProfile) {
|
if (action is Action.GotoProfile) {
|
||||||
blockLoginButton() // FIXME() При двойном нажатии вылетает с ошибкой
|
blockLoginButton() // FIXME() При двойном нажатии вылетает с ошибкой
|
||||||
navigateTo(view, R.id.action_authFragment_to_profileFragment)
|
navigateTo(view, R.id.action_authFragment_to_profileFragment)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
viewModel.errorState.collectWithLifecycle(this) { errorMessage ->
|
viewModel.errorState.collectWithLifecycle(this) { errorStatus ->
|
||||||
errorMessage?.let {
|
errorStatus?.let {
|
||||||
Toast.makeText(requireContext(), it, Toast.LENGTH_SHORT).show()
|
Toast.makeText(requireContext(), it, Toast.LENGTH_SHORT).show()
|
||||||
binding.errorTitle.text = errorMessage
|
binding.errorTitle.text = errorStatus
|
||||||
binding.errorTitle.visibility = View.VISIBLE
|
binding.errorTitle.visibility = View.VISIBLE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,49 +48,6 @@ class AuthFragment: Fragment(R.layout.fragment_auth) {
|
|||||||
private fun blockLoginButton() {
|
private fun blockLoginButton() {
|
||||||
binding.next.isEnabled = false
|
binding.next.isEnabled = false
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupLoginButton() {
|
|
||||||
binding.login.addTextChangedListener(object : TextWatcher {
|
|
||||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
|
|
||||||
@SuppressLint("ResourceAsColor")
|
|
||||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
|
||||||
binding.error.visibility = View.GONE
|
|
||||||
// val username = s.toString()
|
|
||||||
// val valid = isUsernameValid(username)
|
|
||||||
//
|
|
||||||
// if (valid) {
|
|
||||||
// binding.hint.visibility = View.INVISIBLE
|
|
||||||
// }else{
|
|
||||||
// binding.login.error = getString(R.string.login_hint)
|
|
||||||
// }
|
|
||||||
// binding.hint.visibility = if(valid) View.INVISIBLE else View.VISIBLE
|
|
||||||
// binding.next.isEnabled = valid
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun afterTextChanged(s: Editable?) {}
|
|
||||||
})
|
|
||||||
// binding.password.addTextChangedListener(object : TextWatcher {
|
|
||||||
// override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
|
|
||||||
// @SuppressLint("ResourceAsColor")
|
|
||||||
// override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
|
||||||
// binding.error.visibility = View.GONE
|
|
||||||
// val username = s.toString()
|
|
||||||
// val valid = isPasswordValid(username)
|
|
||||||
//
|
|
||||||
// binding.hint.visibility = if(valid) View.INVISIBLE else View.VISIBLE
|
|
||||||
// if (!valid){
|
|
||||||
// val errorMessage = getPasswordValidError(s.toString())
|
|
||||||
// binding.password.setError(errorMessage)
|
|
||||||
// }
|
|
||||||
// binding.next.isEnabled = valid
|
|
||||||
// val color = if (valid) R.color.primary else R.color.secondary
|
|
||||||
// binding.next.backgroundTintList = ContextCompat.getColorStateList(requireContext(), color)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// override fun afterTextChanged(s: Editable?) {}
|
|
||||||
// })
|
|
||||||
}
|
|
||||||
private fun getPasswordValidError(password: String): String {
|
private fun getPasswordValidError(password: String): String {
|
||||||
if (password.length < 8) {
|
if (password.length < 8) {
|
||||||
return "LenError"
|
return "LenError"
|
||||||
|
@ -12,6 +12,7 @@ import kotlinx.coroutines.flow.StateFlow
|
|||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.receiveAsFlow
|
import kotlinx.coroutines.flow.receiveAsFlow
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlin.reflect.typeOf
|
||||||
|
|
||||||
class AuthViewModel(): ViewModel() {
|
class AuthViewModel(): ViewModel() {
|
||||||
private val _action = Channel<Action>(
|
private val _action = Channel<Action>(
|
||||||
@ -28,6 +29,7 @@ class AuthViewModel(): ViewModel() {
|
|||||||
UserServiceST.getInstance().login(login, password).fold(
|
UserServiceST.getInstance().login(login, password).fold(
|
||||||
onSuccess = { openProfile() },
|
onSuccess = { openProfile() },
|
||||||
onFailure = { error ->
|
onFailure = { error ->
|
||||||
|
Log.d("AuthViewModel", error::class.simpleName.toString())
|
||||||
Log.e("AuthViewModel", "Login failed: ${error.message ?: "Unknown error"}")
|
Log.e("AuthViewModel", "Login failed: ${error.message ?: "Unknown error"}")
|
||||||
_errorState.value = error.message ?: "Ошибка входа"
|
_errorState.value = error.message ?: "Ошибка входа"
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user