feat refactorResponse login #refs 7827

This commit is contained in:
Sergio De la torre 2024-08-12 13:40:48 +02:00
parent 48e2a10102
commit c3eaea124e
7 changed files with 104 additions and 244 deletions

View File

@ -74,8 +74,7 @@ class MobileApplication : Application(), InteceptorListener {
runBlocking {
dataStoreApp.editDataStoreKey(
ConstAndValues.BASEURLSALIX,
ConstAndValues.BASE_URL_SALIX
ConstAndValues.BASEURLSALIX, ConstAndValues.BASE_URL_SALIX
)
}
val restClient = RestClient(this, this)
@ -111,17 +110,13 @@ class MobileApplication : Application(), InteceptorListener {
}
fun playSoundIsOK(bool: Boolean) {
if (bool) mpok!!.start() else mperror!!.start()
}
fun messageToast(isError: Boolean, message: String) {
val playSound = when (isError) {
fun messageToast(isError: Boolean, message: String, isToasted: Boolean = false) {
when (isError) {
false -> mpok
true -> mperror
}
playSound?.start()
message.toast(
}?.start()
if (isToasted) message.toast(
this, color = if (isError) {
Color.RED
} else {
@ -133,6 +128,7 @@ class MobileApplication : Application(), InteceptorListener {
fun isLoginRunning(): Boolean {
return isActivityLoginRunning
}
override fun onInterceptionResult(visibility: Int) {
if (mainActivityListener != null) {
handler.post {

View File

@ -54,7 +54,11 @@ abstract class SalixCallback<T>(val context: Context) : Callback<T> {
private fun defaultErrorHandler(t: Throwable) {
//println("ErrorSalixx${t.message}")
// (nameofFunction((this)) + t.message).toast(context)
(context as MobileApplication).messageToast(true, (nameofFunction((this)) + t.message))
(context as MobileApplication).messageToast(
true,
(nameofFunction((this)) + t.message),
isToasted = true
)
}
open fun onSuccess(response: Response<T>) {
@ -84,7 +88,7 @@ abstract class SalixCallback<T>(val context: Context) : Callback<T> {
val messageResponse = response.errorBody()!!.string()
val jsonObject = Gson().fromJson(messageResponse, JsonObject::class.java)
var messageJson = jsonObject?.getAsJsonObject("error")?.get("message")?.asString
var messageJsonCode = jsonObject?.getAsJsonObject("error")?.get("code")?.asString?:""
var messageJsonCode = jsonObject?.getAsJsonObject("error")?.get("code")?.asString ?: ""
messageJson ?: "${response.message()}.$myErrorCodeBody.$messageJsonCode"
} catch (ex: Exception) {
response.message() + ".$myErrorCodeBody."

View File

@ -279,13 +279,8 @@ class AjustesFragment :
)
}
loginViewModel.workerOperator.observe(this@AjustesFragment) { iti ->
if (!iti.isError) {
runBlocking { mobileApplication.dataStoreApp.saveDataOperator(iti) }
} else {
ma.messageWithSound(iti.errorMessage, isError = true, isPlayed = true)
}
runBlocking { mobileApplication.dataStoreApp.saveDataOperator(iti) }
}
setSettings()
}

View File

@ -10,7 +10,6 @@ import es.verdnatura.presentation.common.LoginListener
import es.verdnatura.presentation.common.TAG
import es.verdnatura.presentation.common.addFragment
import es.verdnatura.presentation.view.feature.login.fragment.LoginFragment
import es.verdnatura.presentation.view.feature.main.activity.MainActivity
class LoginActivity : BaseActivity<ActivityLoginBinding>(), LoginListener {
private var imageUri: Uri? = null
@ -33,12 +32,6 @@ class LoginActivity : BaseActivity<ActivityLoginBinding>(), LoginListener {
addFragment(LoginFragment.newInstance(imageUri), R.id.main_frame_layout, LoginFragment.TAG)
}
fun goToMain() {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}
override fun onApplicationEventAnimation(visibility: Int) {
binding.splashAnimation.visibility = visibility
}

View File

@ -143,6 +143,13 @@ class LoginFragment(private var imageUri: Uri?) :
}
binding.edittextUsername.requestFocus()
binding.textviewRememberPassword.setOnLongClickListener {
if (getDevicePDA() == 19591) {
binding.edittextUsername.setText("sergiodt")
binding.edittextPassword.setText("delatorre.1234")
}
true
}
}
private fun getDevicePDA(): Int? {
@ -326,14 +333,9 @@ class LoginFragment(private var imageUri: Uri?) :
override fun observeViewModel() {
with(viewModel) {
versionappitem.observe(viewLifecycleOwner) {
versionAppItem.observe(viewLifecycleOwner) {
if (it.isError) {
customDialog.setTitle(getString(R.string.error)).setDescription(it.errorMessage)
.setOkButton(getString(R.string.close)) {
customDialog.dismiss()
}.show()
} else {
if (it.version == getInfoVersionNameApp()) {
goToMain()
} else {
@ -365,7 +367,6 @@ class LoginFragment(private var imageUri: Uri?) :
}
customDialog.show()
}
}
@ -381,41 +382,33 @@ class LoginFragment(private var imageUri: Uri?) :
}
logindevice.observe(viewLifecycleOwner) {
if (it.isError) {
customDialog.setTitle(getString(R.string.error)).setDescription(it.errorMessage)
.setOkButton(getString(R.string.close)) {
if (it.vIsAuthorized == "0") {
customDialog.setTitle(getString(R.string.error))
.setDescription(it.vMessage!!).setOkButton(getString(R.string.close)) {
customDialog.dismiss()
}.show()
} else {
if (it.vIsAuthorized == "0") {
customDialog.setTitle(getString(R.string.error))
.setDescription(it.vMessage!!).setOkButton(getString(R.string.close)) {
customDialog.dismiss()
}.show()
} else {
runBlocking {
if (binding.switchRemember.isChecked) {
mobileApplication.dataStoreApp.editDataStoreKey(REMEMBER, true)
} else {
mobileApplication.dataStoreApp.editDataStoreKey(REMEMBER, false)
}
runBlocking {
if (binding.switchRemember.isChecked) {
mobileApplication.dataStoreApp.editDataStoreKey(REMEMBER, true)
} else {
mobileApplication.dataStoreApp.editDataStoreKey(REMEMBER, false)
}
if (it.vIsAuthorized == "1") {
if (it.vMessage.isNullOrEmpty()) {
getVersion()
} else {
customDialog.setTitle(getString(R.string.info))
.setDescription(it.vMessage)
.setOkButton(getString(R.string.close)) {
customDialog.dismiss()
getVersion()
}.show()
}
}
if (it.vIsAuthorized == "1") {
if (it.vMessage.isNullOrEmpty()) {
getVersion()
} else {
customDialog.setTitle(getString(R.string.info))
.setDescription(it.vMessage)
.setOkButton(getString(R.string.close)) {
customDialog.dismiss()
getVersion()
}.show()
}
}
@ -425,31 +418,13 @@ class LoginFragment(private var imageUri: Uri?) :
}
workerOperator.observe(viewLifecycleOwner) {
if (it.isError) {
customDialog.setTitle(getString(R.string.info)).setDescription(it.errorMessage)
.setOkButton(getString(R.string.close)) {
customDialog.dismiss()
}.show()
} else {
runBlocking {
mobileApplication.dataStoreApp.saveDataOperator(
it
)
}
runBlocking {
mobileApplication.dataStoreApp.saveDataOperator(
it
)
}
}
operatorAdd.observe(viewLifecycleOwner) {
if (it.isError) {
customDialog.setTitle(getString(R.string.info)).setDescription(it.errorMessage)
.setOkButton(getString(R.string.close)) {
customDialog.dismiss()
}.show()
}
}
devicelogresponse.observe(viewLifecycleOwner) {
@ -499,33 +474,27 @@ class LoginFragment(private var imageUri: Uri?) :
event.getContentIfNotHandled().notNull {
if (it.list.isEmpty()) {
getString(R.string.errorConfigToken).toast(context)
if (it.list.isNotEmpty()) {
} else {
if (!it.list[0].isError) {
runBlocking {
mobileApplication.dataStoreApp.editDataStoreKey(
RENEWPERIOD, it.list[0].renewPeriod
)
mobileApplication.dataStoreApp.editDataStoreKey(
RENEWINTERVAL, it.list[0].renewInterval
)
}
if (getDevicePDA() != null) {
viewModel.getSerialNumber(getDevicePDA()!!)
} else {
viewModel.getCurrentUserData(
getString(R.string.logAppName),
getInfoVersionNameApp(),
mobileApplication.dataStoreApp.readDataStoreKey(ANDROID_ID),
)
}
} else {
it.list[0].errorMessage.toast(context)
runBlocking {
mobileApplication.dataStoreApp.editDataStoreKey(
RENEWPERIOD, it.list[0].renewPeriod
)
mobileApplication.dataStoreApp.editDataStoreKey(
RENEWINTERVAL, it.list[0].renewInterval
)
}
if (getDevicePDA() != null) {
viewModel.getSerialNumber(getDevicePDA()!!)
} else {
viewModel.getCurrentUserData(
getString(R.string.logAppName),
getInfoVersionNameApp(),
mobileApplication.dataStoreApp.readDataStoreKey(ANDROID_ID),
)
}
}
}
@ -546,8 +515,9 @@ class LoginFragment(private var imageUri: Uri?) :
customDialogInput.dismiss()
viewModel.validateAuth(
user = binding.edittextUsername.text.toString(),
password = binding.edittextPassword.text.toString(),
code = customDialogInput.getValue())
password = binding.edittextPassword.text.toString(),
code = customDialogInput.getValue()
)
}.show()
}

View File

@ -48,16 +48,12 @@ class LoginViewModel(val context: Context) : BaseViewModel(context) {
val devicelogresponse: LiveData<ResponseItemVO>
get() = _devicelogresponse
private val _operatorAdd by lazy { MutableLiveData<ResponseItemVO>() }
val operatorAdd: LiveData<ResponseItemVO>
get() = _operatorAdd
private val _serialNumber by lazy { MutableLiveData<String?>() }
val serialNumber: LiveData<String?> = _serialNumber
private val _versionappitem by lazy { MutableLiveData<VersionApp>() }
val versionappitem: LiveData<VersionApp>
get() = _versionappitem
private val _versionAppItem by lazy { MutableLiveData<VersionApp>() }
val versionAppItem: LiveData<VersionApp>
get() = _versionAppItem
private val _logindevice by lazy { MutableLiveData<LoginDevice>() }
val logindevice: LiveData<LoginDevice>
@ -147,12 +143,12 @@ class LoginViewModel(val context: Context) : BaseViewModel(context) {
) {
salix.loginApp(login).enqueue(object : SalixCallback<LoginAppData>(context) {
override fun onSuccess(response: Response<LoginAppData>) {
override fun onSuccess(response: Response<LoginAppData>) {
_loginApp.value = response.body()
}
_loginApp.value = response.body()
}
override fun onError(t: Throwable) {/* val loginSalixVO = LoginAppData(
override fun onError(t: Throwable) {/* val loginSalixVO = LoginAppData(
user,
password,
"",
@ -162,30 +158,21 @@ class LoginViewModel(val context: Context) : BaseViewModel(context) {
)
)
_loginSalixItem.value = loginSalixVO*/
}
})
}
})
}
fun deviceCheckLogin(userId: Int, androidId: String) {
// FALTA EL USUARI
/* fun deviceCheckLogin(androidId: String) {
silex.device_checkLogin(androidId)*/
salix.deviceCheckLogin(arrayListOf(userId, androidId).formatWithQuotes())
.enqueue(object : SalixCallback<List<LoginDevice>>(context) {
override fun onSuccess(response: Response<List<LoginDevice>>) {
response.body()?.get(0).let {
_logindevice.value = LoginDevice(
it!!.vMessage, it.vIsAuthorized, isError = false, errorMessage = ""
it!!.vMessage, it.vIsAuthorized
)
}
}
override fun onError(t: Throwable) {
_logindevice.value = LoginDevice(
"", "", isError = true, errorMessage = getMessageFromAllResponse(
nameofFunction(this), t.message!!
)
)
}
})
}
@ -238,42 +225,27 @@ class LoginViewModel(val context: Context) : BaseViewModel(context) {
versionApp = versionApp,
serialNumber = serialnumber
)
).enqueue(object : SalixCallback<Unit>(context) {
override fun onSuccess(response: Response<Unit>) {
}
})
).enqueue(object : SalixCallback<Unit>(context) {})
}
fun checkVersion(nameApp: String) {
//Tarea 6276 Modificado back
salix.getVersion(nameApp).enqueue(object : SalixCallback<VersionApp>(context) {
override fun onSuccess(response: Response<VersionApp>) {
override fun onSuccess(response: Response<VersionApp>) {
_versionappitem.value = response.body()?.let {
VersionApp(
it.appname,
it.version,
it.isVersionCritical,
it.url,
isError = false,
errorMessage = ""
)
}
}
override fun onError(t: Throwable) {
_versionappitem.value = VersionApp(
nameApp, "", isError = true, errorMessage = getMessageFromAllResponse(
nameofFunction(this), t.message!!
)
_versionAppItem.value = response.body()?.let {
VersionApp(
it.appname,
it.version,
it.isVersionCritical,
it.url,
)
}
})
}
})
}
fun getListFromJSON(json: JsonObject): MutableList<ItemsExpeditionDynamics> {
@ -295,17 +267,9 @@ class LoginViewModel(val context: Context) : BaseViewModel(context) {
.enqueue(object : SalixCallback<Any>(context) {
override fun onError(t: Throwable) {
if (!(t.message!!.contains("ER_DUP_ENTRY"))) _operatorAdd.value =
ResponseItemVO(
isError = true, errorMessage = getMessageFromAllResponse(
nameofFunction(this), t.message!!
)
)
if (!(t.message!!.contains("ER_DUP_ENTRY"))) onError(t)
}
override fun onSuccess(response: Response<Any>) {
_operatorAdd.value = ResponseItemVO("", false)
}
})
}
@ -317,29 +281,9 @@ class LoginViewModel(val context: Context) : BaseViewModel(context) {
"""{"fields":["itemPackingTypeFk","numberOfWagons","sectorFk","trainFk","labelerFk","isOnReservationMode"],"include":[{"relation":"sector","scope":{"fields":["description","warehouseFk"]}},{"relation":"printer","scope":{"fields":["name"]}},{"relation":"train","scope":{"fields":["name"]}}]}"""
salix.operatorGetData(id = workerId, filter)
.enqueue(object : SalixCallback<OperatorSalix>(context) {
override fun onError(t: Throwable) {
_workerOperator.value = OperatorSalix(
isError = true, errorMessage = getMessageFromAllResponse(
nameofFunction(this), t.message!!
)
)
}
override fun onSuccess(response: Response<OperatorSalix>) {
if (response.body() != null) {
_workerOperator.value = response.body()
} else {
_workerOperator.value = response.body()?.let {
OperatorSalix(
isError = true, errorMessage = getMessageFromAllResponse(
nameofFunction(this), response.message()
)
)
}
}
_workerOperator.value = response.body()
}
})
@ -350,38 +294,11 @@ class LoginViewModel(val context: Context) : BaseViewModel(context) {
salix.getAccessTokenConfigs()
.enqueue(object : SalixCallback<List<AccessConfigSalix>>(context) {
override fun onError(t: Throwable) {
val listError: ArrayList<AccessConfigSalix> = ArrayList()
listError.add(
AccessConfigSalix(
isError = true, errorMessage = getMessageFromAllResponse(
nameofFunction(this), t.message!!
)
)
)
_accessConfigSalixList.value = AccessConfigSalixList(listError)
}
override fun onSuccess(response: Response<List<AccessConfigSalix>>) {
if (response.body() != null) {
_accessConfigSalixList.value = response.body()?.let {
AccessConfigSalixList(it)
}
} else {
val listError: ArrayList<AccessConfigSalix> = ArrayList()
listError.add(
AccessConfigSalix(
isError = true, errorMessage = getMessageFromAllResponse(
nameofFunction(this), response.message()
)
)
)
_accessConfigSalixList.value = AccessConfigSalixList(listError)
_accessConfigSalixList.value = response.body()?.let {
AccessConfigSalixList(it)
}
}
})

View File

@ -43,15 +43,10 @@ class DataUserSalix(
val id: Int? = null,
val name: String = "",
val nickname: String = "",
var isError: Boolean = false,
var errorMessage: String = ""
)
class RenewToken(
val id: String,
val ttl: Long = 0,
var isError: Boolean = false,
var errorMessage: String = ""
val id: String, val ttl: Long = 0, var isError: Boolean = false, var errorMessage: String = ""
)
class SalixMessageVO(
@ -63,8 +58,7 @@ class SalixMessageVO(
)
class SalixGrupo(
val to: String = "",
val message: String = ""
val to: String = "", val message: String = ""
)
@ -75,8 +69,6 @@ class OperatorSalix(
val printer: Printer? = null,
val numberOfWagons: Int = 0,
var itemPackingTypeFk: String? = null,
val isError: Boolean = false,
var errorMessage: String = "",
val isOnReservationMode: Boolean = false
)
@ -94,8 +86,6 @@ class AccessConfigSalix(
val id: Number = 0,
val renewPeriod: Long = 0,
val renewInterval: Long = 0,
val isError: Boolean = false,
var errorMessage: String = ""
)
class OperatorList(
@ -107,13 +97,12 @@ class AccessConfigSalixList(
)
class Operator(
val sectorFk: Int?,
val labelerFk: Int?
val sectorFk: Int?, val labelerFk: Int?
)
data class OperatorAdd(
var workerFk:Int,
var isOnReservationMode:Boolean
var workerFk: Int, var isOnReservationMode: Boolean
)
class VersionApp(
@ -126,14 +115,10 @@ class VersionApp(
)
class LoginDevice(
val vMessage: String? = "",
val vIsAuthorized: String = "",
var isError: Boolean = false,
var errorMessage: String = ""
val vMessage: String? = "", val vIsAuthorized: String = ""
)
data class NameWorker(
val firstName : String,
val lastName :String
val firstName: String, val lastName: String
)