refs #5613 renew token

This commit is contained in:
Sergio De la torre 2023-06-30 07:55:47 +02:00
parent 362217114a
commit 56dd0d9626
1 changed files with 181 additions and 84 deletions

View File

@ -2,13 +2,15 @@ package es.verdnatura.presentation.view.feature.main.activity
//Tarea #4815
import PhotosFragment
import android.content.Context
import android.content.SharedPreferences
import android.media.MediaPlayer
import android.net.Uri
import android.os.Build
import android.view.Menu
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
@ -48,6 +50,8 @@ import es.verdnatura.presentation.view.feature.historicoshelving.fragment.itemSh
import es.verdnatura.presentation.view.feature.historicoshelvinglog.fragment.shelvingLogFragment
import es.verdnatura.presentation.view.feature.historicovehiculo.fragment.HistoricoVehiculoFragment
import es.verdnatura.presentation.view.feature.inventario.fragment.InventaryFragment
import es.verdnatura.presentation.view.feature.inventario.fragment.InventaryParkingFragment
import es.verdnatura.presentation.view.feature.login.fragment.LoginViewModel
import es.verdnatura.presentation.view.feature.main.model.ItemMenuVO
import es.verdnatura.presentation.view.feature.packaging.fragment.ObservFragment
import es.verdnatura.presentation.view.feature.packaging.fragment.PackagingCountFragment
@ -79,6 +83,11 @@ import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.time.Duration
import java.time.Instant
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickListener,
@ -93,10 +102,12 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
var mpok: MediaPlayer? = null
var mpErrorRepeat: MediaPlayer? = null
private var scheduledExecutor: ScheduledExecutorService? = null
//Tarea #4815
// var miTime: Long = System.currentTimeMillis() / 1000 / 60 / 60
private var comeFromDelivery: Boolean? = null
override fun getLayoutId(): Int = R.layout.activity_main
@RequiresApi(Build.VERSION_CODES.O)
override fun init() {
mperror = MediaPlayer.create((this), R.raw.error)
@ -106,6 +117,25 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
customDialog = CustomDialogMainActivity(this)
setFragments()
setBottomMenuFragment()
//Tarea 5613
//startRepeatingTask((application as MobileApplication).renewInterval)
}
@RequiresApi(Build.VERSION_CODES.O)
private fun startRepeatingTask(myInterval: Long) {
scheduledExecutor = Executors.newSingleThreadScheduledExecutor()
scheduledExecutor?.scheduleAtFixedRate(
{
runOnUiThread {
checkTokenRenew()
}
},
0,
myInterval,
TimeUnit.SECONDS
)
}
private fun setFragments() {
@ -113,35 +143,67 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
val extras = intent.extras
val uriImage = intent.data
if (extras != null && extras.containsKey("menu")) {
comeFromDelivery = true
val option = extras.getString("menu")
/*saveDataInt("USERFK", extras.getString("userfk")!!.toInt())
saveUserAccesPref(
extras.getString("user").toString(),
extras.getString("password").toString()
if (extras != null && extras.containsKey("menu")) {
comeFromDelivery = true
val option = extras.getString("menu")
//Tarea 5613 borrar que se guarde
saveDataInt("USERFK", extras.getString("userfk")!!.toInt())
saveUserAccesPref(
extras.getString("user").toString(),
extras.getString("password").toString()
)*/
// A borrar toda esta parte cuando está oficialmente delivery en picking
var app= application as MobileApplication
app.userName= extras.getString("user").toString()
app.userId= extras.getString("userfk").toString().toInt()
app.userPassword= extras.getString("password").toString()
)
// A borrar toda esta parte cuando está oficialmente delivery en picking
var app = application as MobileApplication
app.userName = extras.getString("user").toString()
app.userId = extras.getString("userfk").toString().toInt()
app.userPassword = extras.getString("password").toString()
when (option) {
"log" -> onPasillerosItemClickListener(
when (option) {
"log" -> onPasillerosItemClickListener(
PasillerosItemVO(
30,
R.drawable.expedition_find,
getString(R.string.titleLog),
R.string.titleLog,
getString(
R.string.titleLogDescrip
)
), getString(R.string.titleLog)
)
"info" -> onPasillerosItemClickListener(
PasillerosItemVO(
30,
R.drawable.ic_info,
getString(R.string.titleInfo),
R.string.titleInfo,
getString(
R.string.titleInfoDescription
)
), getString(R.string.titleInfo)
)
"summary" -> {
onPasillerosItemClickListener(
PasillerosItemVO(
30,
R.drawable.expedition_find,
getString(R.string.titleLog),
R.string.titleLog,
R.drawable.ic_info,
getString(R.string.titleDeliverySummary),
R.string.titleDeliverySummary,
getString(
R.string.titleLogDescrip
R.string.titleDeliverySummary
)
), getString(R.string.titleLog)
), extras.getString("route").toString()
)
"info" -> onPasillerosItemClickListener(
}
"unload" -> {
onPasillerosItemClickListener(
PasillerosItemVO(
30,
R.drawable.ic_info,
@ -153,70 +215,42 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
), getString(R.string.titleInfo)
)
"summary" -> {
onPasillerosItemClickListener(
PasillerosItemVO(
30,
R.drawable.ic_info,
getString(R.string.titleDeliverySummary),
R.string.titleDeliverySummary,
getString(
R.string.titleDeliverySummary
)
), extras.getString("route").toString()
)
}
"unload" -> {
onPasillerosItemClickListener(
PasillerosItemVO(
30,
R.drawable.ic_info,
getString(R.string.titleInfo),
R.string.titleInfo,
getString(
R.string.titleInfoDescription
)
), getString(R.string.titleInfo)
)
}
}
} else {
if (haveSector() && havePrinter()) addFragment(
PasilleroFragment.newInstance(getString(R.string.main), true),
R.id.main_frame_layout,
PasilleroFragment.TAG,
false
)
else addFragment(
AjustesFragment.newInstance(),
R.id.main_frame_layout,
AjustesFragment.TAG,
false
)
if (uriImage != null && uriImage is Uri) {
onPasillerosItemClickListener(
PasillerosItemVO(
30,
R.drawable.photo_camera,
getString(R.string.titlePhotos),
R.string.titlePhotos,
getString(
R.string.titlePhotosDescrip
)
), uriImage.toString()
)
}
}
} else {
if (haveSector() && havePrinter()) addFragment(
PasilleroFragment.newInstance(getString(R.string.main), true),
R.id.main_frame_layout,
PasilleroFragment.TAG,
false
)
else addFragment(
AjustesFragment.newInstance(),
R.id.main_frame_layout,
AjustesFragment.TAG,
false
)
if (uriImage != null && uriImage is Uri) {
onPasillerosItemClickListener(
PasillerosItemVO(
30,
R.drawable.photo_camera,
getString(R.string.titlePhotos),
R.string.titlePhotos,
getString(
R.string.titlePhotosDescrip
)
), uriImage.toString()
)
}
}
} catch (exception: Exception) {
getString(R.string.error).toast(this)
}
@ -377,6 +411,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
)
}
getString(R.string.Parking) -> {
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
delete_Fragments()
@ -389,6 +424,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
}
getString(R.string.Ajustes) -> {
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
delete_Fragments()
@ -457,6 +493,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
}
getString(R.string.titleControllers) -> {
addFragmentOnTop(ControladorFragment.newInstance())
}
@ -472,6 +509,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
// addFragment(ExpeditionStateFragment.newInstance(entryPoint),R.id.main_frame_layout, ExpeditionTruckListFragment.TAG,false)
addFragmentOnTop(ExpeditionStateFragment.newInstance(item.title))
}
getString(R.string.titleScanExpedition) -> {
addFragmentOnTop(ExpeditionScanSorterFragment.newInstance(item.title))
}
@ -484,6 +522,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
)
)
}
getString(R.string.titleClaims) -> {
addFragmentOnTop(
PasilleroFragment.newInstance(
@ -508,6 +547,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
getString(R.string.titleShowTicket) -> {
addFragmentOnTop(ShowTicketFragment.newInstance(getString(R.string.titleCorridors)))
}
getString(R.string.titleHistoricalVehicle) -> {
addFragmentOnTop(ControlVehiculoFragment.newInstance(HistoricoVehiculoFragment.TAG))
// fm.popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE)
@ -555,9 +595,11 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
getString(R.string.titleShelvingParking) -> {
addFragmentOnTop(ShelvingParkingFragment.newInstance())
}
getString(R.string.titleUbicator) -> {
addFragmentOnTop(UbicadorFragment.newInstance(entryPoint))
}
getString(R.string.titleAuto) -> {
addFragmentOnTop(AutomaticAddItemFragment.newInstance(entryPoint, item.title))
}
@ -585,18 +627,23 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
getString(R.string.titleBufferEmpty) -> {
addFragmentOnTop(BufferFragment.newInstance(item.title))
}
getString(R.string.titleBufferFill) -> {
addFragmentOnTop(BufferFragment.newInstance(item.title))
}
getString(R.string.titleBufferAccumulate) -> {
addFragmentOnTop(BufferFragment.newInstance(item.title))
}
getString(R.string.titleBufferOff) -> {
addFragmentOnTop(BufferFragment.newInstance(item.title))
}
getString(R.string.titleBufferload) -> {
addFragmentOnTop(BufferLoadFragment.newInstance(item.title))
}
getString(R.string.titleBufferStrapping) -> {
addFragmentOnTop(BufferFragment.newInstance(item.title))
}
@ -604,16 +651,20 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
getString(R.string.titlePreControl) -> {
addFragmentOnTop(PreControladorFragment.newInstance())
}
getString(R.string.titleShelvingHistorical) -> {
addFragmentOnTop(itemShelvingLogFragment.newInstance(item.title))
}
getString(R.string.titleLogShelving) -> {
addFragmentOnTop(shelvingLogFragment.newInstance(item.title))
}
getString(R.string.titleWorkerMistake) -> {
addFragmentOnTop(WorkermistakeFragment.newInstance(item.title))
}
getString(R.string.titlePackingMistake) -> {
addFragmentOnTop(PackingMistakeFragment.newInstance(item.title))
}
@ -629,6 +680,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
getString(R.string.titleReUbicator) -> {
addFragmentOnTop(ReubicationFragment.newInstance(item.title))
}
getString(R.string.titleCMRState) -> {
addFragmentOnTop(CmrExpeditionPalletFragment.newInstance(item.title))
}
@ -640,6 +692,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
getString(R.string.titleScanPalletizar) -> {
addFragmentOnTop(PalletScanFragment.newInstance(item.title, "get"))
}
getString(R.string.titlePackaging) -> {
addFragmentOnTop(SupplierFragment.newInstance(item.title))
}
@ -651,9 +704,11 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
getString(R.string.titlePackagingObs) -> {
addFragmentOnTop(ObservFragment.newInstance(item.title))
}
getString(R.string.titlePackagingSummary) -> {
addFragmentOnTop(PackagingSummaryFragment.newInstance(item.title))
}
getString(R.string.selfConsumption) -> {
addFragmentOnTop(UbicadorFragment.newInstance(entryPoint, true))
}
@ -665,6 +720,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
getString(R.string.titleChangeCategory) -> {
addFragmentOnTop(ChangeCategoryFragment.newInstance(item.title))
}
getString(R.string.titleDelivery) -> {
addFragmentOnTop(
PasilleroFragment.newInstance(
@ -755,6 +811,10 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
// PhotosFragment.newInstance(item.title))
//addFragmentOnTop(testPrint.newInstance(item.title))
}
getString(R.string.titleInventoryParking) -> {
addFragmentOnTop(InventaryParkingFragment.newInstance(entryPoint))
}
}
}
@ -859,6 +919,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
addFragmentOnTop(EndSacadorFragment.newInstance(collection, type))
}
"SHOWTICKET" -> {
addFragmentOnTop(CollectionShowTicketFragment.newInstance(collection, type))
}
@ -866,6 +927,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
"PRECHECKER" -> {
addFragmentOnTop(CollectionFragmentPreChecker.newInstance(collection, type))
}
"PREPARED" -> {
addFragmentOnTop(CollectionFragmentPicker.newInstance(collection, type))
}
@ -894,9 +956,16 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
// addFragmentOnTop(ControlVehiculoFragment.newInstance(tag))
}
fun openSummaryFragment( route: Int, state: String) {
addFragmentOnTop(SummaryFragment.newInstance(getString(R.string.titleDeliverySummary),state,route))
fun openSummaryFragment(route: Int, state: String) {
addFragmentOnTop(
SummaryFragment.newInstance(
getString(R.string.titleDeliverySummary),
state,
route
)
)
}
fun openFragmentPickers(delete: Boolean) {
//delete_Fragments()
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
@ -984,4 +1053,32 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickL
}*/
@RequiresApi(Build.VERSION_CODES.O)
//Tarea 5613
fun checkTokenRenew() {
// runBlocking {
val prefs: SharedPreferences = getSharedPreferences("es.verdnatura.user.prefs", 0)
val createdSaved = prefs.getString("tokenCreated", "")
val renewPeriod = (application as MobileApplication).renewInterval
val dateTokenCreated = Instant.parse(createdSaved)
val dateToday = Instant.now()
val duracion = Duration.between(dateTokenCreated, dateToday)
val secondsDifference = duracion.seconds
println("Renovar: Segundos diferencia entre creado y actual" + secondsDifference)
if (secondsDifference < renewPeriod) {
println("REnovar si")
val loginViewModel = LoginViewModel(application as MobileApplication)
loginViewModel.renewToken()
} else {
println("Renovar no")
/* val loginViewModel = LoginViewModel(application as MobileApplication)
//val loginViewModel = ViewModelProvider(this).get(LoginViewModel::class.java)
loginViewModel.renewToken()*/
}
}
}