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
import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Bundle
import android.provider.Settings
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -143,6 +146,24 @@ abstract class BaseFragment<T : ViewDataBinding, V : BaseViewModel>(
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 {

View File

@ -2,8 +2,6 @@ package es.verdnatura.presentation.view.feature.login.fragment
import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.net.Uri
import android.provider.Settings
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.feature.login.adapter.WorkFormAdapter
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 kotlinx.coroutines.runBlocking
import java.util.Date
@ -64,6 +63,7 @@ class LoginFragment(private var imageUri: Uri?) :
customDialog = CustomDialog(requireContext())
customDialogInput = CustomDialogInput(requireContext())
//createFolderSerial()
setSwitch()
saveIdentifiers()
checkUser()
binding.buttonLogin.setOnClickListener {
@ -72,31 +72,12 @@ class LoginFragment(private var imageUri: Uri?) :
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)
.isNotBlank() && binding.switchRemember.isChecked
) {
if (getDevicePDA() != null) {
viewModel.getSerialNumber(getDevicePDA()!!)
} else {
viewModel.getCurrentUserData(
getString(R.string.logAppName),
getInfoVersionNameApp(),
mobileApplication.dataStoreApp.readDataStoreKey(ANDROID_ID),
)
}
handleUserCall()
}
@ -143,25 +124,46 @@ class LoginFragment(private var imageUri: Uri?) :
}
binding.edittextUsername.requestFocus()
binding.textviewRememberPassword.setOnLongClickListener {
if (getDevicePDA() == 19591) {
binding.edittextUsername.setText("sergiodt")
binding.edittextPassword.setText("delatorre.1234")
}
private fun handleUserCall() {
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? {
return try {
Settings.Global.getString(
mobileApplication.contentResolver, Settings.Global.DEVICE_NAME
).toInt()
} catch (ex: Exception) {
null
}
/* private fun getDevicePDA(): Int? {
return try {
Settings.Global.getString(
mobileApplication.contentResolver, Settings.Global.DEVICE_NAME
).toInt()
} catch (ex: Exception) {
null
}
}
}*/
/* private fun createFolderSerial() {
@ -311,7 +313,7 @@ class LoginFragment(private var imageUri: Uri?) :
private fun checkUser() {
if (mobileApplication.dataStoreApp.readDataStoreKey(REMEMBER)) {
if (mobileApplication.dataStoreApp.readDataStoreKey<Boolean>(REMEMBER)) {
binding.edittextUsername.setText(
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() {
with(viewModel) {
handleUserResponse.observe(viewLifecycleOwner) {
runBlocking {
mobileApplication.dataStoreApp.saveWorkerData(
it
)
}
reviewVersion(it)
}
versionAppItem.observe(viewLifecycleOwner) {
if (it.version == getInfoVersionNameApp()) {
@ -382,8 +428,8 @@ class LoginFragment(private var imageUri: Uri?) :
if (it.vIsAuthorized == "0") {
customDialog.setTitle(getString(R.string.error))
.setDescription(it.vMessage!!).setOkButton(getString(R.string.close)) {
customDialog.setTitle(getString(R.string.error)).setDescription(it.vMessage!!)
.setOkButton(getString(R.string.close)) {
customDialog.dismiss()
}.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) {
@ -483,15 +516,8 @@ class LoginFragment(private var imageUri: Uri?) :
)
}
if (getDevicePDA() != null) {
viewModel.getSerialNumber(getDevicePDA()!!)
} else {
viewModel.getCurrentUserData(
getString(R.string.logAppName),
getInfoVersionNameApp(),
mobileApplication.dataStoreApp.readDataStoreKey(ANDROID_ID),
)
}
//Tarea 6868
handleUserCall()
}
}
@ -524,12 +550,12 @@ class LoginFragment(private var imageUri: Uri?) :
Firebase.crashlytics.setUserId(userName)
}
private fun getInfoVersionNameApp(): String {
val manager: PackageManager = requireContext().packageManager
val info: PackageInfo = manager.getPackageInfo(requireContext().packageName, 0)
mobileApplication.versionName = info.versionName
return info.versionName
}
/* private fun getInfoVersionNameApp(): String {
val manager: PackageManager = requireContext().packageManager
val info: PackageInfo = manager.getPackageInfo(requireContext().packageName, 0)
mobileApplication.versionName = info.versionName
return info.versionName
}*/
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.AccessConfigSalixList
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.LoginSalixVO
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.RenewToken
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 kotlinx.coroutines.runBlocking
import retrofit2.Response
@ -40,8 +39,8 @@ class LoginViewModel(val context: Context) : BaseViewModel(context) {
val loginSalixItem: LiveData<LoginSalixVO>
get() = _loginSalixItem
private val _loginApp by lazy { MutableLiveData<LoginAppData>() }
val loginApp: LiveData<LoginAppData> = _loginApp
private val _handleUserResponse by lazy { MutableLiveData<WorkerData>() }
val handleUserResponse: LiveData<WorkerData> = _handleUserResponse
private val _serialNumber by lazy { MutableLiveData<String?>() }
val serialNumber: LiveData<String?> = _serialNumber
@ -133,27 +132,17 @@ class LoginViewModel(val context: Context) : BaseViewModel(context) {
})
}
fun loginApp(
login: LoginApp,
) {
salix.handleUser(login).enqueue(object : SalixCallback<LoginAppData>(context) {
override fun onSuccess(response: Response<LoginAppData>) {
_loginApp.value = response.body()
fun handleUser(
login: MutableMap<String, Any>
) {
salix.handleUser(login).enqueue(object : SalixCallback<WorkerData>(context) {
override fun onSuccess(response: Response<WorkerData>) {
app.userName = response.body()?.nickname
app.userId = response.body()?.id
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 = ""
)
data class LoginApp(
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,
data class WorkerData(
val numberOfWagons: Int,
var warehouseFk: Int,
val trainFk: Int,
val itemPackingTypeFk: String,
val warehouseFk: Int,
val labelerFk: Int,
val sectorFk: Int,
val sector: Sector?,
val printer: Printer,
val train: Train,
val sector: Sector,
val id: Int,
val name: String,
val nickname: String,
val serialNumber: String,
val isVersionCritical: Boolean,
val version: String,
val vIsAuthorized: String,
val vMessage: String,
val url: 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(
val id: Int? = null,
val name: String = "",
@ -76,12 +72,10 @@ data class Sector(
val id: Int = 0,
val description: String = "",
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(
val id: Number = 0,
val renewPeriod: Long = 0,