feat: refs #6868 handleUser

This commit is contained in:
Sergio De la torre 2024-10-16 11:34:45 +02:00
parent 2c0f2cc72d
commit 7805b78cdf
4 changed files with 144 additions and 114 deletions

View File

@ -1,7 +1,10 @@
package es.verdnatura.presentation.base package es.verdnatura.presentation.base
import android.content.Context import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Bundle import android.os.Bundle
import android.provider.Settings
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -143,6 +146,24 @@ abstract class BaseFragment<T : ViewDataBinding, V : BaseViewModel>(
tool.backButton.visibility = View.INVISIBLE tool.backButton.visibility = View.INVISIBLE
} }
fun getInfoVersionNameApp(): String {
val manager: PackageManager = requireContext().packageManager
val info: PackageInfo = manager.getPackageInfo(requireContext().packageName, 0)
mobileApplication.versionName = info.versionName
return info.versionName
}
fun getDevicePDA(): Int? {
return try {
Settings.Global.getString(
mobileApplication.contentResolver, Settings.Global.DEVICE_NAME
).toInt()
} catch (ex: Exception) {
null
}
}
} }
fun database(myContext: Context): DeliveryDatabase { fun database(myContext: Context): DeliveryDatabase {

View File

@ -2,8 +2,6 @@ package es.verdnatura.presentation.view.feature.login.fragment
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Intent import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.net.Uri import android.net.Uri
import android.provider.Settings import android.provider.Settings
import android.view.View.GONE import android.view.View.GONE
@ -37,6 +35,7 @@ import es.verdnatura.presentation.view.component.CustomDialogInputThreeValues
import es.verdnatura.presentation.view.component.CustomDialogList import es.verdnatura.presentation.view.component.CustomDialogList
import es.verdnatura.presentation.view.feature.login.adapter.WorkFormAdapter import es.verdnatura.presentation.view.feature.login.adapter.WorkFormAdapter
import es.verdnatura.presentation.view.feature.login.model.WorkForms import es.verdnatura.presentation.view.feature.login.model.WorkForms
import es.verdnatura.presentation.view.feature.login.model.WorkerData
import es.verdnatura.presentation.view.feature.main.activity.MainActivity import es.verdnatura.presentation.view.feature.main.activity.MainActivity
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import java.util.Date import java.util.Date
@ -64,6 +63,7 @@ class LoginFragment(private var imageUri: Uri?) :
customDialog = CustomDialog(requireContext()) customDialog = CustomDialog(requireContext())
customDialogInput = CustomDialogInput(requireContext()) customDialogInput = CustomDialogInput(requireContext())
//createFolderSerial() //createFolderSerial()
setSwitch()
saveIdentifiers() saveIdentifiers()
checkUser() checkUser()
binding.buttonLogin.setOnClickListener { binding.buttonLogin.setOnClickListener {
@ -72,31 +72,12 @@ class LoginFragment(private var imageUri: Uri?) :
password = binding.edittextPassword.text.toString() password = binding.edittextPassword.text.toString()
) )
//Tarea 6868
/* viewModel.loginApp(
LoginApp(
user = binding.edittextUsername.text.toString(),
password = binding.edittextPassword.text.toString(),
deviceId = getDevicePDA(),
androidId = mobileApplication.dataStoreApp.readDataStoreKey(ANDROID_ID),
versionApp = getInfoVersionNameApp(),
nameApp = getString(R.string.nameApp),
)
)*/
}
}
if (mobileApplication.dataStoreApp.readDataStoreKey<String>(TOKEN) if (mobileApplication.dataStoreApp.readDataStoreKey<String>(TOKEN)
.isNotBlank() && binding.switchRemember.isChecked .isNotBlank() && binding.switchRemember.isChecked
) { ) {
if (getDevicePDA() != null) { handleUserCall()
viewModel.getSerialNumber(getDevicePDA()!!)
} else {
viewModel.getCurrentUserData(
getString(R.string.logAppName),
getInfoVersionNameApp(),
mobileApplication.dataStoreApp.readDataStoreKey(ANDROID_ID),
)
}
} }
@ -143,25 +124,46 @@ class LoginFragment(private var imageUri: Uri?) :
} }
binding.edittextUsername.requestFocus() binding.edittextUsername.requestFocus()
binding.textviewRememberPassword.setOnLongClickListener { }
if (getDevicePDA() == 19591) {
binding.edittextUsername.setText("sergiodt") private fun handleUserCall() {
binding.edittextPassword.setText("delatorre.1234") val jsonData = mutableMapOf<String, Any>(
"androidId" to mobileApplication.androidId,
"nameApp" to getString(R.string.nameApp),
"versionApp" to getInfoVersionNameApp()
)
viewModel.handleUser(if (getDevicePDA() != null) {
jsonData.apply {
this["deviceId"] = getDevicePDA().toString() // Añadir nuevo campo
} }
true } else {
jsonData
}
)
}
private fun setSwitch() {
binding.switchRemember.setOnCheckedChangeListener { _, isChecked ->
runBlocking {
mobileApplication.dataStoreApp.editDataStoreKey(
REMEMBER, isChecked
)
}
} }
} }
private fun getDevicePDA(): Int? { /* private fun getDevicePDA(): Int? {
return try { return try {
Settings.Global.getString( Settings.Global.getString(
mobileApplication.contentResolver, Settings.Global.DEVICE_NAME mobileApplication.contentResolver, Settings.Global.DEVICE_NAME
).toInt() ).toInt()
} catch (ex: Exception) { } catch (ex: Exception) {
null null
} }
} }*/
/* private fun createFolderSerial() { /* private fun createFolderSerial() {
@ -311,7 +313,7 @@ class LoginFragment(private var imageUri: Uri?) :
private fun checkUser() { private fun checkUser() {
if (mobileApplication.dataStoreApp.readDataStoreKey(REMEMBER)) { if (mobileApplication.dataStoreApp.readDataStoreKey<Boolean>(REMEMBER)) {
binding.edittextUsername.setText( binding.edittextUsername.setText(
mobileApplication.dataStoreApp.readDataStoreKey<String>( mobileApplication.dataStoreApp.readDataStoreKey<String>(
@ -330,9 +332,53 @@ class LoginFragment(private var imageUri: Uri?) :
} }
private fun reviewVersion(it: WorkerData) {
if (it.version == getInfoVersionNameApp()) {
goToMain()
} else {
if (binding.edittextServer.text.toString() == "Producción") {
mobileApplication.clearApplicationData()
}
customDialog.setTitle(getString(R.string.Actualizar))
.setDescription(getString(R.string.updatemng)).setOkButton(
getString(
R.string.Actualizar
)
) {
try {
val openURL = Intent(Intent.ACTION_VIEW)
openURL.data = Uri.parse(it.url)
startActivity(openURL)
} catch (ex: Exception) {
getString(R.string.problemUpdate).toast(requireActivity())
}
}
if (it.isVersionCritical) {
customDialog.setDescription(getString(R.string.updatecritical))
} else {
customDialog.setKoButton(getString(R.string.cancel)) {
customDialog.dismiss()
goToMain()
}
}
customDialog.show()
}
}
override fun observeViewModel() { override fun observeViewModel() {
with(viewModel) { with(viewModel) {
handleUserResponse.observe(viewLifecycleOwner) {
runBlocking {
mobileApplication.dataStoreApp.saveWorkerData(
it
)
}
reviewVersion(it)
}
versionAppItem.observe(viewLifecycleOwner) { versionAppItem.observe(viewLifecycleOwner) {
if (it.version == getInfoVersionNameApp()) { if (it.version == getInfoVersionNameApp()) {
@ -382,8 +428,8 @@ class LoginFragment(private var imageUri: Uri?) :
if (it.vIsAuthorized == "0") { if (it.vIsAuthorized == "0") {
customDialog.setTitle(getString(R.string.error)) customDialog.setTitle(getString(R.string.error)).setDescription(it.vMessage!!)
.setDescription(it.vMessage!!).setOkButton(getString(R.string.close)) { .setOkButton(getString(R.string.close)) {
customDialog.dismiss() customDialog.dismiss()
}.show() }.show()
@ -424,19 +470,6 @@ class LoginFragment(private var imageUri: Uri?) :
} }
} }
/* devicelogresponse.observe(viewLifecycleOwner) {
if (it.isError) {
customDialog.setTitle(getString(R.string.error)).setDescription(it.errorMessage)
.setOkButton(
getString(R.string.accept)
) {
customDialog.dismiss()
getVersion()
}.show()
}
}*/
loginSalixItem.observe(viewLifecycleOwner) { loginSalixItem.observe(viewLifecycleOwner) {
@ -483,15 +516,8 @@ class LoginFragment(private var imageUri: Uri?) :
) )
} }
if (getDevicePDA() != null) { //Tarea 6868
viewModel.getSerialNumber(getDevicePDA()!!) handleUserCall()
} else {
viewModel.getCurrentUserData(
getString(R.string.logAppName),
getInfoVersionNameApp(),
mobileApplication.dataStoreApp.readDataStoreKey(ANDROID_ID),
)
}
} }
} }
@ -524,12 +550,12 @@ class LoginFragment(private var imageUri: Uri?) :
Firebase.crashlytics.setUserId(userName) Firebase.crashlytics.setUserId(userName)
} }
private fun getInfoVersionNameApp(): String { /* private fun getInfoVersionNameApp(): String {
val manager: PackageManager = requireContext().packageManager val manager: PackageManager = requireContext().packageManager
val info: PackageInfo = manager.getPackageInfo(requireContext().packageName, 0) val info: PackageInfo = manager.getPackageInfo(requireContext().packageName, 0)
mobileApplication.versionName = info.versionName mobileApplication.versionName = info.versionName
return info.versionName return info.versionName
} }*/
private fun saveTokenPref(token: String, ttl: Long) { private fun saveTokenPref(token: String, ttl: Long) {

View File

@ -21,14 +21,13 @@ import es.verdnatura.presentation.view.feature.historicoshelvinglog.model.Device
import es.verdnatura.presentation.view.feature.login.model.AccessConfigSalix import es.verdnatura.presentation.view.feature.login.model.AccessConfigSalix
import es.verdnatura.presentation.view.feature.login.model.AccessConfigSalixList import es.verdnatura.presentation.view.feature.login.model.AccessConfigSalixList
import es.verdnatura.presentation.view.feature.login.model.DataUserSalix import es.verdnatura.presentation.view.feature.login.model.DataUserSalix
import es.verdnatura.presentation.view.feature.login.model.LoginApp
import es.verdnatura.presentation.view.feature.login.model.LoginAppData
import es.verdnatura.presentation.view.feature.login.model.LoginDevice import es.verdnatura.presentation.view.feature.login.model.LoginDevice
import es.verdnatura.presentation.view.feature.login.model.LoginSalixVO import es.verdnatura.presentation.view.feature.login.model.LoginSalixVO
import es.verdnatura.presentation.view.feature.login.model.OperatorAdd import es.verdnatura.presentation.view.feature.login.model.OperatorAdd
import es.verdnatura.presentation.view.feature.login.model.OperatorSalix import es.verdnatura.presentation.view.feature.login.model.OperatorSalix
import es.verdnatura.presentation.view.feature.login.model.RenewToken import es.verdnatura.presentation.view.feature.login.model.RenewToken
import es.verdnatura.presentation.view.feature.login.model.VersionApp import es.verdnatura.presentation.view.feature.login.model.VersionApp
import es.verdnatura.presentation.view.feature.login.model.WorkerData
import es.verdnatura.presentation.view.feature.paletizador.model.ItemsExpeditionDynamics import es.verdnatura.presentation.view.feature.paletizador.model.ItemsExpeditionDynamics
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import retrofit2.Response import retrofit2.Response
@ -40,8 +39,8 @@ class LoginViewModel(val context: Context) : BaseViewModel(context) {
val loginSalixItem: LiveData<LoginSalixVO> val loginSalixItem: LiveData<LoginSalixVO>
get() = _loginSalixItem get() = _loginSalixItem
private val _loginApp by lazy { MutableLiveData<LoginAppData>() } private val _handleUserResponse by lazy { MutableLiveData<WorkerData>() }
val loginApp: LiveData<LoginAppData> = _loginApp val handleUserResponse: LiveData<WorkerData> = _handleUserResponse
private val _serialNumber by lazy { MutableLiveData<String?>() } private val _serialNumber by lazy { MutableLiveData<String?>() }
val serialNumber: LiveData<String?> = _serialNumber val serialNumber: LiveData<String?> = _serialNumber
@ -133,27 +132,17 @@ class LoginViewModel(val context: Context) : BaseViewModel(context) {
}) })
} }
fun loginApp( fun handleUser(
login: LoginApp, login: MutableMap<String, Any>
) {
) { salix.handleUser(login).enqueue(object : SalixCallback<WorkerData>(context) {
salix.handleUser(login).enqueue(object : SalixCallback<LoginAppData>(context) { override fun onSuccess(response: Response<WorkerData>) {
override fun onSuccess(response: Response<LoginAppData>) { app.userName = response.body()?.nickname
app.userId = response.body()?.id
_loginApp.value = response.body() app.serialNumber = response.body()?.serialNumber
_handleUserResponse.value = response.body()
} }
override fun onError(t: Throwable) {/* val loginSalixVO = LoginAppData(
user,
password,
"",
isError = true,
errorMessage = getMessageFromAllResponse(
nameofFunction(this), t.message!!
)
)
_loginSalixItem.value = loginSalixVO*/
}
}) })
} }

View File

@ -10,35 +10,31 @@ class LoginSalixVO(
var errorMessage: String = "" var errorMessage: String = ""
) )
data class LoginApp( data class WorkerData(
val user: String,
val password: String,
val deviceId: Int?,
val androidId: String,
val versionApp: String,
val nameApp: String
)
data class LoginAppData(
val id: Int,
val token: String,
val ttl: Long,
val numberOfWagons: Int, val numberOfWagons: Int,
var warehouseFk: Int, val trainFk: Int,
val itemPackingTypeFk: String, val itemPackingTypeFk: String,
val warehouseFk: Int,
val labelerFk: Int,
val sectorFk: Int,
val sector: Sector?,
val printer: Printer, val printer: Printer,
val train: Train, val train: Train,
val sector: Sector, val id: Int,
val name: String, val name: String,
val nickname: String, val nickname: String,
val serialNumber: String,
val isVersionCritical: Boolean, val isVersionCritical: Boolean,
val version: String, val version: String,
val vIsAuthorized: String, val url: String?,
val vMessage: String, val message: String?,
val serialNumber: String,
val isOnReservationMode: Boolean = false
) )
data class Printer(val id: Int, val name: String)
data class Train(val id: Int, val name: String)
class DataUserSalix( class DataUserSalix(
val id: Int? = null, val id: Int? = null,
val name: String = "", val name: String = "",
@ -76,12 +72,10 @@ data class Sector(
val id: Int = 0, val id: Int = 0,
val description: String = "", val description: String = "",
val warehouseFk: Int = 0, val warehouseFk: Int = 0,
val backupPrinterFk: Int? val backupPrinterFk: Int?,
val isOnReservationMode: Boolean? = false
) )
data class Printer(val id: Int, val name: String)
data class Train(val id: Int, val name: String)
class AccessConfigSalix( class AccessConfigSalix(
val id: Number = 0, val id: Number = 0,
val renewPeriod: Long = 0, val renewPeriod: Long = 0,