refs #5613 renew token

This commit is contained in:
Sergio De la torre 2023-06-30 07:39:57 +02:00
parent 37f98c8f80
commit 362217114a
10 changed files with 184 additions and 17 deletions

View File

@ -17,6 +17,8 @@ class MobileApplication : Application() {
lateinit var salix: SalixService
var userId: Int? = null
var userName: String? = null
var renewPeriod:Long = 0
var renewInterval:Long = 0
var userPassword: String? = null
var mperror: MediaPlayer? = null
var mpok: MediaPlayer? = null

View File

@ -9,9 +9,9 @@ import es.verdnatura.presentation.view.feature.collection.SalixSaleQuantity
import es.verdnatura.presentation.view.feature.collection.listSaleSalix
import es.verdnatura.presentation.view.feature.historicoshelvinglog.model.ShelvingLogSalix
import es.verdnatura.presentation.view.feature.login.model.LoginSalixVO
import es.verdnatura.presentation.view.feature.login.model.Operator
import es.verdnatura.presentation.view.feature.login.model.OperatorSalix
import es.verdnatura.presentation.view.feature.login.model.SalixGrupo
import es.verdnatura.presentation.view.feature.login.model.accessConfigSalix
import es.verdnatura.presentation.view.feature.packaging.model.EntrySalix
import es.verdnatura.presentation.view.feature.packaging.model.ItemSupplier
import es.verdnatura.presentation.view.feature.packaging.model.Supplier
@ -127,7 +127,7 @@ interface SalixService {
@POST("PackingSiteAdvanceds")
fun ticket_advancePackaging(
@Body parms : packingSiteSalix
@Body parms: packingSiteSalix
):
Call<Void>
@ -187,7 +187,7 @@ interface SalixService {
@POST("Images/upload")
@Multipart
fun uploadImage(
@Query ("collection") collection: String,
@Query("collection") collection: String,
@Query("id") id: Int,
@Part filePart: MultipartBody.Part,
):
@ -201,13 +201,22 @@ interface SalixService {
):
Call<Void>
@GET("Operators")
fun operator_getData(
@Query("filter") filter:String
@Query("filter") filter: String
):
Call<List<OperatorSalix>>
@GET("AccessTokenConfigs")
fun getAccessTokenConfigs(
):
Call<List<accessConfigSalix>>
@GET("vnusers/renewToken")
fun renewToken(
):
Call<LoginSalixVO>
}

View File

@ -45,6 +45,7 @@ abstract class BaseFragment<T : ViewDataBinding, V : BaseViewModel>(viewModelCla
protected val USER = "user"
protected val PASSWORD = "password"
protected val TOKEN = "token"
protected val TOKENCREATED = "tokenCreated"
protected val SECTORDESCRIP = "sectordescrip"
protected val SECTORFK = "sectorFk"
protected val NUMBEROFWAGONS = "operatorNumberOfWagons"

View File

@ -8,9 +8,11 @@ import android.content.SharedPreferences
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.Settings
import android.view.View
import androidx.annotation.RequiresApi
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.firebase.crashlytics.ktx.crashlytics
@ -29,6 +31,8 @@ import es.verdnatura.presentation.view.feature.login.adapter.WorkFormAdapter
import es.verdnatura.presentation.view.feature.login.model.WorkForms
import es.verdnatura.presentation.view.feature.main.activity.MainActivity
import java.io.File
import java.time.Duration
import java.time.Instant
import java.util.*
import kotlin.system.exitProcess
@ -49,13 +53,15 @@ class LoginFragment(var imageUri: Uri?) :
override fun getLayoutId(): Int = R.layout.fragment_login
@RequiresApi(Build.VERSION_CODES.O)
override fun init() {
customDialogList = CustomDialogList(requireContext())
customDialog = CustomDialog(requireContext())
saveIdentifiers()
checkUser()
//Tarea 5613
//viewModel.getAccessTokenConfigs()
/*Tarea 4815
createFolderSerial()*/
binding.buttonLogin.setOnClickListener {
@ -91,7 +97,6 @@ class LoginFragment(var imageUri: Uri?) :
listForms = getWorkForm().sortedBy { it.created }
binding.edittextServer.keyListener = null
binding.edittextServer.setOnLongClickListener {
showWorkingForms()
true
@ -100,6 +105,23 @@ class LoginFragment(var imageUri: Uri?) :
}
@RequiresApi(Build.VERSION_CODES.O)
private fun checkToken() {
if (getData(TOKEN).isNotBlank()) {
val renewPeriod = getDataInt("renewPeriod")
var renewInterval=getDataInt("renewInterval")
val dateTokenCreated = Instant.parse(getData(TOKENCREATED))
val dateToday= Instant.now()
val duracion = Duration.between(dateToday, dateTokenCreated)
val secondsDifference = duracion.seconds
}
}
private fun createFolderSerial() {
val directory = File(
@ -129,6 +151,7 @@ class LoginFragment(var imageUri: Uri?) :
"Añadir..." -> {
showNewForm()
}
else -> {
binding.edittextServer.setText(item.name)
saveData("base_url", item.urlSilex)
@ -475,7 +498,7 @@ class LoginFragment(var imageUri: Uri?) :
if (it.isError) {
binding.splashProgress.visibility = View.GONE
saveTokenPref("")
saveTokenPref("","")
customDialog.setTitle(getString(R.string.error))
.setDescription(
@ -492,7 +515,11 @@ class LoginFragment(var imageUri: Uri?) :
}.show()
} else {
saveTokenPref(it.token)
saveTokenPref(it.token, it.created)
//test
//saveTokenPref("RTymwJzRuVchAMY5u8xG2wpgcjPf7t0DQImLvMsTdNFmbFwDiVsa8lA6fr1XSaBo","2026-29-06")
// local
//saveTokenPref("rpG3yNMdViFYDh1sZfeYRCUmuHZYmKW5EqzwHkckj0zFbszSZGTWbs8xNETHyvuw","2026-29-06")
/* saveUserAccesPref(
binding.edittextUsername.text.toString(),
binding.edittextPassword.text.toString()
@ -506,6 +533,27 @@ class LoginFragment(var imageUri: Uri?) :
}
}
loadAccessConfigSalixList.observe(viewLifecycleOwner, Observer { event ->
binding.splashProgress.visibility = View.GONE
event.getContentIfNotHandled().notNull {
if (it.list.isEmpty()) {
getString(R.string.errorConfigToken).toast(context)
} else {
if (!it.list[0].isError) {
app.renewPeriod = it.list[0].renewPeriod!!
app.renewInterval = it.list[0].renewInterval!!
//saveDataInt("renewPeriod", it.list[0].renewPeriod!!)
//saveDataInt("renewInterval", it.list[0].renewInterval)
} else {
it.list[0].errorMessage.toast(context)
}
}
}
})
}
super.observeViewModel()
@ -521,10 +569,11 @@ class LoginFragment(var imageUri: Uri?) :
return info.versionName
}
private fun saveTokenPref(token: String) {
private fun saveTokenPref(token: String, date: String) {
val prefs: SharedPreferences = requireActivity().getSharedPreferences(PREFS_USER, 0)
val editor = prefs.edit()
editor.putString(TOKEN, token)
editor.putString(TOKENCREATED, date)
editor.apply()
}

View File

@ -1,5 +1,6 @@
package es.verdnatura.presentation.view.feature.login.fragment
import android.app.Application
import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
@ -21,13 +22,14 @@ 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.OperatorList
import es.verdnatura.presentation.view.feature.login.model.OperatorSalix
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.versionApp
import es.verdnatura.presentation.view.feature.login.model.workerId
import es.verdnatura.presentation.view.feature.paletizador.model.itemsExpeditionDynamics
import retrofit2.Response
class LoginViewModel(val context: Context) : BaseViewModel(context) {
private val _loginsalixitem by lazy { MutableLiveData<LoginSalixVO>() }
val loginsalixitem: LiveData<LoginSalixVO>
get() = _loginsalixitem
@ -69,9 +71,11 @@ class LoginViewModel(val context: Context) : BaseViewModel(context) {
private val _workerPrintList by lazy { MutableLiveData<WorkerPrintersList>() }
private val _workerOperatorList by lazy { MutableLiveData<OperatorList>() }
private val _accessConfigSalixList by lazy { MutableLiveData<accessConfigSalixList>() }
val loadWorkerPrintList = Transformations.map(_workerPrintList) { Event(it) }
val loadOperatorList = Transformations.map(_workerOperatorList) { Event(it) }
val loadAccessConfigSalixList = Transformations.map(_accessConfigSalixList) { Event(it) }
fun loginSalix(user: String, password: String) {
salix.login(LoginSalixVO(user, password)).enqueue(
@ -80,11 +84,12 @@ class LoginViewModel(val context: Context) : BaseViewModel(context) {
val loginSalixVO: LoginSalixVO?
if (response.body() != null) {
loginSalixVO = response.body()?.token?.let {
loginSalixVO = response.body()?.let {
LoginSalixVO(
user,
password,
it,
it.token,
it.created,
isError = false,
errorMessage = ""
)
@ -172,7 +177,7 @@ class LoginViewModel(val context: Context) : BaseViewModel(context) {
val prefs = app.getPrefs()
response.body()?.let { prefs.putInt("userFk", it) }
prefs.commit()
operator_getDataSalix("{\"where\": {\"workerFk\":${response.body()?.let {it}}}}")
// operator_getDataSalix("{\"where\": {\"workerFk\":${response.body()?.let {it}}}}")
}
@ -465,4 +470,80 @@ class LoginViewModel(val context: Context) : BaseViewModel(context) {
})
}
fun getAccessTokenConfigs(
) {
salix.getAccessTokenConfigs()
.enqueue(object : SilexCallback<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)
}
}
})
}
fun renewToken(
) {
println("Renovando loginsalix")
salix.renewToken()
.enqueue(object : SilexCallback<LoginSalixVO>(context) {
override fun onSuccess(response: Response<LoginSalixVO>) {
var loginSalixVO = response.body()
val prefs = app.getPrefs()
prefs.putString("TOKEN",loginSalixVO!!.token)
prefs.putString("TOKENCREATED",loginSalixVO!!.created)
}
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)
}
})
}
}

View File

@ -4,6 +4,7 @@ class LoginSalixVO(
val user: String = "",
val password: String = "",
val token: String = "",
val created: String = "",
var isError: Boolean = false,
var errorMessage: String = ""
)
@ -34,10 +35,22 @@ class OperatorSalix(
var errorMessage: String = ""
)
class accessConfigSalix(
val id: Number = 0,
val renewPeriod: Long = 0,
val renewInterval: Long = 0,
val isError: Boolean = false,
var errorMessage: String = ""
)
class OperatorList(
var list: List<OperatorSalix> = listOf()
)
class accessConfigSalixList(
var list: List<accessConfigSalix> = listOf()
)
class Operator(
val sectorFk: Int?,
val labelerFk: Int?

View File

@ -599,5 +599,8 @@
<string name="scanLabelTicket">Escanea ticket</string>
<string name="ticketAdvancePagkaging">El ticket ya puede ser encajado</string>
<string name="ticketFormatError">El ticket escaneado no tiene un formato correcto</string>
<string name="errorConfigToken">Póngase en contacto con el dpto. de Informática</string>
<string name="titleInventoryParking">Inventario por parking</string>
<string name="titleInventoryParkingDescrip">Permite inventariar por parking</string>
</resources>

View File

@ -599,5 +599,8 @@
<string name="scanLabelTicket">Escanea ticket</string>
<string name="ticketAdvancePagkaging">El ticket ya puede ser encajado</string>
<string name="ticketFormatError">El ticket escaneado no tiene un formato correcto</string>
<string name="errorConfigToken">Póngase en contacto con el dpto. de Informática</string>
<string name="titleInventoryParking">Inventario por parking</string>
<string name="titleInventoryParkingDescrip">Permite inventariar por parking</string>
</resources>

View File

@ -599,5 +599,8 @@
<string name="scanLabelTicket">Escanea ticket</string>
<string name="ticketAdvancePagkaging">El ticket ya puede ser encajado</string>
<string name="ticketFormatError">El ticket escaneado no tiene un formato correcto</string>
<string name="errorConfigToken">Póngase en contacto con el dpto. de Informática</string>
<string name="titleInventoryParking">Inventario por parking</string>
<string name="titleInventoryParkingDescrip">Permite inventariar por parking</string>
</resources>

View File

@ -598,4 +598,7 @@
<string name="scanLabelTicket">Escanea ticket</string>
<string name="ticketAdvancePagkaging">El ticket ya puede ser encajado</string>
<string name="ticketFormatError">El ticket escaneado no tiene un formato correcto</string>
<string name="errorConfigToken">Póngase en contacto con el dpto. de Informática</string>
<string name="titleInventoryParking">Inventory by parking</string>
<string name="titleInventoryParkingDescrip">Inventory by parking, you can select ubications</string>
</resources>