From 5b8c08973453d29d29b898bae3d80ca5a8fa5edd Mon Sep 17 00:00:00 2001 From: Sergio De la torre Date: Tue, 10 Sep 2024 07:18:42 +0200 Subject: [PATCH] feat: refactorResponse Paletizado refs #7827 --- .../ExpeditionPalletDetailFragment.kt | 65 ++---- .../ExpeditionPalletDetailViewModel.kt | 88 ++------ .../fragment/ExpeditionPalletFragment.kt | 89 ++++---- .../fragment/ExpeditionPalletViewModel.kt | 96 --------- .../ExpeditionPreparedStateFragment.kt | 8 +- .../fragment/ExpeditionScanFragment.kt | 136 ++++--------- .../fragment/ExpeditionScanSorterFragment.kt | 4 +- .../fragment/ExpeditionScanSorterViewModel.kt | 4 +- .../fragment/ExpeditionScanViewModel.kt | 192 +++++------------- .../fragment/ExpeditionTruckListFragment.kt | 4 +- .../fragment/ExpeditionTruckListViewModel.kt | 11 +- 11 files changed, 185 insertions(+), 512 deletions(-) delete mode 100644 app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPalletViewModel.kt diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPalletDetailFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPalletDetailFragment.kt index 84abc19c..fdaa6aea 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPalletDetailFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPalletDetailFragment.kt @@ -3,7 +3,7 @@ package es.verdnatura.presentation.view.feature.paletizador.fragment import android.content.Context import android.graphics.drawable.Drawable import android.view.View -import androidx.lifecycle.Observer +import androidx.core.content.res.ResourcesCompat import androidx.recyclerview.widget.LinearLayoutManager import es.verdnatura.R import es.verdnatura.databinding.FragmentExpeditionPalletDetailBinding @@ -19,8 +19,6 @@ import es.verdnatura.presentation.view.feature.paletizador.adapter.ExpeditionPal import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionTruckVO import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletVO import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletViewListVO -import java.text.SimpleDateFormat -import java.util.Calendar class ExpeditionPalletDetailFragment( var itemExpeditionTruckVO: ItemExpeditionTruckVO? = null, @@ -46,6 +44,7 @@ class ExpeditionPalletDetailFragment( override fun getLayoutId(): Int = R.layout.fragment_expedition_pallet_detail override fun init() { + println("ExpeditionPalletDetail") customDialog = CustomDialog(requireContext()) binding.expeditionPalletDetailPallet.text = buildString { @@ -59,12 +58,7 @@ class ExpeditionPalletDetailFragment( append(itemExpeditionTruckVO!!.eta) append(" ") append( - if (itemExpeditionTruckVO!!.Destino.isNullOrEmpty()) { - - itemExpeditionTruckVO!!.description - } else { - itemExpeditionTruckVO!!.Destino - } + itemExpeditionTruckVO!!.description ) } @@ -90,13 +84,14 @@ class ExpeditionPalletDetailFragment( private fun setToolBar() { val listIcons: ArrayList = ArrayList() val iconReload: Drawable = - resources.getDrawable(R.drawable.ic_autorenew_black_24dp, resources.newTheme()) + ResourcesCompat.getDrawable(resources, R.drawable.ic_autorenew_black_24dp, null)!! val iconEditar: Drawable = - resources.getDrawable(R.drawable.ic_mode_edit_black_24dp, resources.newTheme()) + ResourcesCompat.getDrawable(resources, R.drawable.ic_mode_edit_black_24dp, null)!! val iconBorrar: Drawable = - resources.getDrawable(R.drawable.ic_delete_forever_black_24dp, resources.newTheme()) + ResourcesCompat.getDrawable(resources, R.drawable.ic_delete_forever_black_24dp, null)!! val iconPrint: Drawable = - resources.getDrawable(R.drawable.ic_print_black_24dp, resources.newTheme()) + ResourcesCompat.getDrawable(resources, R.drawable.ic_print_black_24dp, null)!! + listIcons.add(iconEditar) listIcons.add(iconBorrar) listIcons.add(iconPrint) @@ -130,7 +125,9 @@ class ExpeditionPalletDetailFragment( } else if (item == iconPrint) { if (ma.havePrinter() && ma.haveSector()) { - (getString(R.string.Imprimiendo) + mobileApplication.dataStoreApp.readDataStoreKey(PRINTERNAME)).toast( + (getString(R.string.Imprimiendo) + mobileApplication.dataStoreApp.readDataStoreKey( + PRINTERNAME + )).toast( requireContext() ) viewModel.expeditionPalletPrintSet( @@ -152,40 +149,22 @@ class ExpeditionPalletDetailFragment( LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false) } - private fun getCURDATE(date: String): String { - val c = Calendar.getInstance() - val df = SimpleDateFormat(getString(R.string.dateCompleteFormat)) - val df2 = SimpleDateFormat(getString(R.string.timeFormat)) - c.time = df.parse(date)!! - return df2.format(c.time) - } - override fun observeViewModel() { with(viewModel) { - loadExpeditionPalletList.observe(viewLifecycleOwner, Observer { event -> + loadExpeditionPalletList.observe(viewLifecycleOwner) { event -> event.getContentIfNotHandled().notNull { printExpeditionList(it) } - }) + } - response.observe(viewLifecycleOwner, Observer { - if (it.isError) { - ma.messageWithSound(it.errorMessage, true, false) - } else { - ma.openFragmentTruckFragment() - } - }) + response.observe(viewLifecycleOwner) { + ma.openFragmentTruckFragment() + } - responsePrintPallet.observe(viewLifecycleOwner, Observer { - if (it.isError) { - ma.messageWithSound(it.errorMessage, true, true, isToasted = false) - - } else { - viewModel.expeditionStateAddByPallet( - itemPallet!!.Pallet, - "PALLETIZED" - ) - - } - }) + responsePrintPallet.observe(viewLifecycleOwner) { + viewModel.expeditionStateAddByPallet( + itemPallet!!.Pallet, + "PALLETIZED" + ) + } } } diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPalletDetailViewModel.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPalletDetailViewModel.kt index eec8b2f6..cebe2e8d 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPalletDetailViewModel.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPalletDetailViewModel.kt @@ -7,10 +7,7 @@ import androidx.lifecycle.map import es.verdnatura.domain.SalixCallback import es.verdnatura.domain.formatWithQuotes import es.verdnatura.presentation.base.BaseViewModel -import es.verdnatura.presentation.base.getMessageFromAllResponse -import es.verdnatura.presentation.base.nameofFunction import es.verdnatura.presentation.common.Event -import es.verdnatura.presentation.common.ResponseItemVO import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletViewListVO import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletViewVO import retrofit2.Response @@ -18,72 +15,31 @@ import retrofit2.Response class ExpeditionPalletDetailViewModel(var context: Context) : BaseViewModel(context) { private val _expeditionPalletList by lazy { MutableLiveData() } - val loadExpeditionPalletList: LiveData> = _expeditionPalletList.map { Event(it) } + val loadExpeditionPalletList: LiveData> = + _expeditionPalletList.map { Event(it) } - private val _response by lazy { MutableLiveData() } - val response: LiveData + private val _response by lazy { MutableLiveData() } + val response: LiveData get() = _response - private val _responsePrintPallet by lazy { MutableLiveData() } - val responsePrintPallet: LiveData + private val _responsePrintPallet by lazy { MutableLiveData() } + val responsePrintPallet: LiveData get() = _responsePrintPallet fun expeditionPalletView(vPalletFk: Int) { salix.expeditionPalletView(arrayListOf(vPalletFk)) - .enqueue(object : - SalixCallback>(context) { + .enqueue(object : SalixCallback>(context) { override fun onSuccess(response: Response>) { - if (response.body() != null) { - _expeditionPalletList.value = - response.body()?.let { ItemPalletViewListVO(it) } - } else { - val listError: ArrayList = ArrayList() - listError.add( - ItemPalletViewVO( - isError = true, - errorMessage = getMessageFromAllResponse( - nameofFunction(this), - response.message() - ) - ) - ) - _expeditionPalletList.value = ItemPalletViewListVO(listError) - } - } - - override fun onError(t: Throwable) { - val listError: ArrayList = ArrayList() - listError.add( - ItemPalletViewVO( - isError = true, - errorMessage = getMessageFromAllResponse( - nameofFunction(this), - t.message!! - ) - ) - ) - _expeditionPalletList.value = ItemPalletViewListVO(listError) + _expeditionPalletList.value = response.body()?.let { ItemPalletViewListVO(it) } } }) } fun expeditionPalletDel(vPalletFk: Int) { salix.expeditionPalletDel(vPalletFk).enqueue(object : SalixCallback(context) { - override fun onError(t: Throwable) { - _response.value = ResponseItemVO( - isError = true, - errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!) - ) - } override fun onSuccess(response: Response) { - _response.value = ResponseItemVO( - isError = false, - errorMessage = getMessageFromAllResponse( - nameofFunction(this), - response.message() - ) - ) + _response.value = true } }) } @@ -94,16 +50,9 @@ class ExpeditionPalletDetailViewModel(var context: Context) : BaseViewModel(cont salix.expeditionPalletPrintLabel(arrayListOf(vPalletFk)) .enqueue(object : SalixCallback(context) { override fun onSuccess(response: Response) { - _responsePrintPallet.value = - ResponseItemVO(isError = false, response = response.message()) + _responsePrintPallet.value = true } - override fun onError(t: Throwable) { - _responsePrintPallet.value = ResponseItemVO( - isError = true, - errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!) - ) - } }) } @@ -113,19 +62,12 @@ class ExpeditionPalletDetailViewModel(var context: Context) : BaseViewModel(cont ) { salix.expeditionStateAddByPallet( arrayListOf(vPalletFk, vStateCode).formatWithQuotes() - ) - .enqueue(object : SalixCallback(context) { - override fun onSuccess(response: Response) { - _response.value = ResponseItemVO(isError = false, response = response.message()) - } + ).enqueue(object : SalixCallback(context) { + override fun onSuccess(response: Response) { + _response.value = true + } - override fun onError(t: Throwable) { - _response.value = ResponseItemVO( - isError = true, - errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!) - ) - } - }) + }) } } \ No newline at end of file diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPalletFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPalletFragment.kt index 81f3c7cd..b77ec98f 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPalletFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPalletFragment.kt @@ -6,7 +6,7 @@ import android.media.MediaPlayer import android.os.Bundle import android.view.View import android.view.inputmethod.EditorInfo -import androidx.lifecycle.Observer +import androidx.core.content.res.ResourcesCompat import androidx.recyclerview.widget.LinearLayoutManager import es.verdnatura.R import es.verdnatura.databinding.FragmentExpeditionPalletBinding @@ -27,14 +27,12 @@ import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionT import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletListVO import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletVO import es.verdnatura.presentation.view.feature.paletizador.model.ItemScanList -import java.text.SimpleDateFormat -import java.util.* @Suppress("UNUSED_ANONYMOUS_PARAMETER") class ExpeditionPalletFragment( var itemExpeditionTruckVO: ItemExpeditionTruckVO? = null -) : BaseFragment( - ExpeditionPalletViewModel::class +) : BaseFragment( + ExpeditionScanViewModel::class ) { private var adapter: ExpeditionPalletAdapter? = null @@ -61,6 +59,7 @@ class ExpeditionPalletFragment( override fun getLayoutId(): Int = R.layout.fragment_expedition_pallet override fun onCreate(savedInstanceState: Bundle?) { + println("ExpeditionPalletFragment") mperror = MediaPlayer.create((activity as MainActivity), R.raw.error) mpok = MediaPlayer.create((activity as MainActivity), R.raw.ok) super.onCreate(savedInstanceState) @@ -70,12 +69,11 @@ class ExpeditionPalletFragment( customDialog = CustomDialog(requireContext()) customDialogList = CustomDialogList(requireContext()) ma.hideBottomNavigation(View.GONE) - binding.mainToolbar.toolbarTitle.text = - itemExpeditionTruckVO!!.eta + " " + if (itemExpeditionTruckVO!!.Destino.isNullOrBlank()) { - itemExpeditionTruckVO!!.description - } else { - itemExpeditionTruckVO!!.Destino - } + binding.mainToolbar.toolbarTitle.text = getString( + R.string.expeditionInfo, + itemExpeditionTruckVO!!.eta, + itemExpeditionTruckVO!!.description + ) setToolBar() setEvents() viewModel.expeditionPalletList( @@ -95,9 +93,9 @@ class ExpeditionPalletFragment( private fun setToolBar() { val listIcons: ArrayList = ArrayList() val iconReload: Drawable = - resources.getDrawable(R.drawable.ic_autorenew_black_24dp, resources.newTheme()) + ResourcesCompat.getDrawable(resources, R.drawable.ic_autorenew_black_24dp, null)!! val iconPlus: Drawable = - resources.getDrawable(R.drawable.ic_add_black_24dp, resources.newTheme()) + ResourcesCompat.getDrawable(resources, R.drawable.ic_add_black_24dp, null)!! listIcons.add(iconReload) listIcons.add(iconPlus) binding.mainToolbar.toolbarIcons.adapter = @@ -108,7 +106,9 @@ class ExpeditionPalletFragment( itemExpeditionTruckVO!!.id ) } else if (item == iconPlus) { - expeditionScanAdd() + viewModel.expeditionScanAdd( + vPalletFk = 0, vTruckFk = itemExpeditionTruckVO!!.id + ) } } }) @@ -116,31 +116,29 @@ class ExpeditionPalletFragment( LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false) } - private fun getCURDATE(date: String): String { - val c = Calendar.getInstance() - val df = SimpleDateFormat(getString(R.string.dateCompleteFormat)) - val df2 = SimpleDateFormat(getString(R.string.timeFormat)) - c.time = df.parse(date)!! - return df2.format(c.time) - } - override fun observeViewModel() { with(viewModel) { - loadExpeditionPalletList.observe(viewLifecycleOwner, Observer { event -> + loadExpeditionPalletList.observe(viewLifecycleOwner) { event -> event.getContentIfNotHandled().notNull { printExpeditionList(it) } - }) + } - loadScanList.observe(viewLifecycleOwner, Observer { event -> + loadScanList.observe(viewLifecycleOwner) { event -> event.getContentIfNotHandled().notNull { showScanExpeditions(it) } - }) + } - responseCheckexpeditionScanPut.observe(viewLifecycleOwner, Observer { - - if (it.response == "1") { + responseExpeditionAddHasNotSameRoute.observe(viewLifecycleOwner) { + if (it) { mperror?.start() } - }) + } + loadResponseScanPut.observe(viewLifecycleOwner) { event -> + event.getContentIfNotHandled().notNull { + listExpeditions.add(0, BarcodeVO(code = responseScanPut.value.toString())) + customDialogList.setDescription(getString(R.string.total) + listExpeditions.size) + expeditionAdapter!!.notifyItemInserted(0) + } + } } @@ -159,12 +157,6 @@ class ExpeditionPalletFragment( } - private fun expeditionScanAdd() { - viewModel.expeditionScanAdd( - vPalletFk = 0, vTruckFk = itemExpeditionTruckVO!!.id - ) - } - private fun showScanExpeditions(it: ItemScanList) { listExpeditions = ArrayList() @@ -183,7 +175,7 @@ class ExpeditionPalletFragment( if (listExpeditions.size > 0) { onComprobarPalletViewClickListener!!.onComprobarPalletViewClickListener( - itemExpeditionTruckVO!!, ItemPalletVO(Pallet = it.list.get(0).palletFk) + itemExpeditionTruckVO!!, ItemPalletVO(Pallet = it.list[0].palletFk) ) } else { customDialog.setTitle(getString(R.string.info)) @@ -208,27 +200,22 @@ class ExpeditionPalletFragment( customDialogList.getEditText().setOnEditorActionListener { v, actionId, event -> if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5) { - if (!customDialogList.getValue().isNullOrEmpty()) { + if (customDialogList.getValue().isNotEmpty()) { try { - if (customDialogList.getValue().length >= 7) { - listExpeditions.add(0, BarcodeVO(code = customDialogList.getValue())) - customDialogList.setDescription(getString(R.string.total) + listExpeditions.size) - viewModel.expeditionScanPut( - it.list.get(0).palletFk, customDialogList.getValue().toInt() - ) - viewModel.expeditionCheckRoute( - it.list[0].palletFk, customDialogList.getValue().toInt() - ) - } else { - if (mperror != null) mperror!!.start() - } + viewModel.expeditionScanPut( + it.list[0].palletFk, customDialogList.getValue().toInt() + ) + viewModel.checkRouteExpeditionScanPut( + it.list[0].palletFk, customDialogList.getValue().toInt() + ) + } catch (e: Exception) { if (mperror != null) mperror!!.start() } - expeditionAdapter!!.notifyDataSetChanged() + } customDialogList.setValue("") ma.hideKeyboard(customDialogList.getEditText()) diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPalletViewModel.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPalletViewModel.kt deleted file mode 100644 index 5ae32dfd..00000000 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPalletViewModel.kt +++ /dev/null @@ -1,96 +0,0 @@ -package es.verdnatura.presentation.view.feature.paletizador.fragment - -import android.content.Context -import androidx.lifecycle.LiveData -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.map -import es.verdnatura.domain.SalixCallback -import es.verdnatura.presentation.base.BaseViewModel -import es.verdnatura.presentation.base.getMessageFromAllResponse -import es.verdnatura.presentation.base.nameofFunction -import es.verdnatura.presentation.common.Event -import es.verdnatura.presentation.common.ResponseItemVO -import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletListVO -import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletVO -import es.verdnatura.presentation.view.feature.paletizador.model.ItemScanList -import es.verdnatura.presentation.view.feature.paletizador.model.ItemScanVO -import retrofit2.Call -import retrofit2.Response - -class ExpeditionPalletViewModel(val context: Context) : BaseViewModel(context) { - - private val _expeditionPalletList by lazy { MutableLiveData() } - val loadExpeditionPalletList: LiveData> = - _expeditionPalletList.map { Event(it) } - - private val _scanList by lazy { MutableLiveData() } - val loadScanList: LiveData> = _scanList.map { Event(it) } - - private val _response by lazy { MutableLiveData() } - val response: LiveData - get() = _response - - //sergio: aƱadido para comprobar rutas escaneado desde PalletFragment - private val _responseCheckexpeditionScanPut by lazy { MutableLiveData() } - val responseCheckexpeditionScanPut: LiveData - get() = _responseCheckexpeditionScanPut - - fun expeditionPalletList(vTruckFk: Int) { - salix.expeditionPalletList( - arrayListOf(vTruckFk) - ) - .enqueue(object : - SalixCallback>(context) { - override fun onSuccess(response: Response>) { - - _expeditionPalletList.value = - response.body()?.let { ItemPalletListVO(it) } - - } - - }) - } - - fun expeditionScanAdd(vPalletFk: Int, vTruckFk: Int) { - salix.expeditionScanAdd( - arrayListOf(vPalletFk, vTruckFk) - ) - .enqueue(object : - SalixCallback>(context) { - override fun onSuccess(response: Response>) { - _scanList.value = response.body()?.let { ItemScanList(it) } - } - - }) - } - - fun expeditionScanPut(vPalletFk: Int, vExpeditionFk: Int) { - salix.expeditionScanPut(arrayListOf(vPalletFk, vExpeditionFk)) - .enqueue(object : SalixCallback(context) { - override fun onResponse(call: Call, response: Response) { - _response.value = ResponseItemVO(isError = false, response = response.message()) - } - - override fun onError(t: Throwable) { - _response.value = ResponseItemVO( - isError = true, - errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!) - ) - } - }) - } - - fun expeditionCheckRoute(vPalletFk: Int, vExpeditionFk: Int) { - salix.expeditionCheckRoute(arrayListOf(vPalletFk, vExpeditionFk)) - .enqueue(object : SalixCallback(context) { - override fun onSuccess(response: Response) { - - _responseCheckexpeditionScanPut.value = - ResponseItemVO(isError = false, response = response.body()!!) - - } - - }) - } - -} diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPreparedStateFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPreparedStateFragment.kt index 0e7b6f52..11b8f088 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPreparedStateFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionPreparedStateFragment.kt @@ -15,10 +15,10 @@ import es.verdnatura.presentation.view.feature.delivery.viewmodels.DeliveryViewM import es.verdnatura.presentation.view.feature.ubicador.adapter.AutomaticAdapter @Suppress("UNUSED_ANONYMOUS_PARAMETER") -class ExpeditionPreparedStateFragment( -) : BaseFragment( - DeliveryViewModel::class -) { +class ExpeditionPreparedStateFragment : + BaseFragment( + DeliveryViewModel::class + ) { private var adapter: AutomaticAdapter? = null private var contador = 0 diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionScanFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionScanFragment.kt index 89e302eb..1afd8045 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionScanFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionScanFragment.kt @@ -6,13 +6,17 @@ import android.media.MediaPlayer import android.os.Bundle import android.view.View import android.view.inputmethod.EditorInfo -import androidx.lifecycle.Observer +import androidx.core.content.res.ResourcesCompat import androidx.recyclerview.widget.LinearLayoutManager import es.verdnatura.R import es.verdnatura.databinding.FragmentExpeditionScanBinding import es.verdnatura.domain.notNull import es.verdnatura.presentation.base.BaseFragment -import es.verdnatura.presentation.common.* +import es.verdnatura.presentation.common.OnBarcodeRowClickListener +import es.verdnatura.presentation.common.OnComprobarPalletViewClickListener +import es.verdnatura.presentation.common.OnOptionsSelectedListener +import es.verdnatura.presentation.common.OnPalletClickListener +import es.verdnatura.presentation.common.OnScanLongClickListener import es.verdnatura.presentation.view.component.CustomDialog import es.verdnatura.presentation.view.component.CustomDialogList import es.verdnatura.presentation.view.component.CustomDialogTwoButtons @@ -21,9 +25,11 @@ import es.verdnatura.presentation.view.feature.articulo.model.BarcodeVO import es.verdnatura.presentation.view.feature.inventario.adapter.ToolBarAdapter import es.verdnatura.presentation.view.feature.main.activity.MainActivity import es.verdnatura.presentation.view.feature.paletizador.adapter.ExpeditionScanAdapter -import es.verdnatura.presentation.view.feature.paletizador.model.* -import java.text.SimpleDateFormat -import java.util.* +import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionScanList +import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionScanVO +import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionTruckVO +import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletVO +import es.verdnatura.presentation.view.feature.paletizador.model.ItemScanList @Suppress("UNUSED_ANONYMOUS_PARAMETER") class ExpeditionScanFragment( @@ -65,20 +71,16 @@ class ExpeditionScanFragment( } override fun init() { + println("ExpeditionScanFragment") customDialog = CustomDialog(requireContext()) customDialogList = CustomDialogList(requireContext()) customDialogTwoButtons = CustomDialogTwoButtons(requireContext()) - binding.expeditionScanDetailPallet.text = - getString(R.string.pallet) + " " + itemPalletVO!!.Pallet + binding.expeditionScanDetailPallet.text = getString( + R.string.expeditionInfo, getString(R.string.pallet), itemPalletVO!!.Pallet.toString() + ) ma.hideBottomNavigation(View.GONE) binding.mainToolbar.toolbarTitle.text = - itemExpeditionTruckVO!!.eta.toString() + " " + if (itemExpeditionTruckVO!!.Destino.isNullOrBlank()) { - itemExpeditionTruckVO!!.description - } else { - - itemExpeditionTruckVO!!.Destino - } - + getString(R.string.expeditionInfo, itemExpeditionTruckVO?.eta.orEmpty(), itemExpeditionTruckVO?.description.orEmpty()) setToolBar() setEvents() viewModel.expeditionScanList( @@ -97,9 +99,9 @@ class ExpeditionScanFragment( private fun setToolBar() { val listIcons: ArrayList = ArrayList() val iconReload: Drawable = - resources.getDrawable(R.drawable.ic_autorenew_black_24dp, resources.newTheme()) + ResourcesCompat.getDrawable(resources, R.drawable.ic_autorenew_black_24dp, null)!! val iconPlus: Drawable = - resources.getDrawable(R.drawable.ic_add_black_24dp, resources.newTheme()) + ResourcesCompat.getDrawable(resources, R.drawable.ic_add_black_24dp, null)!! listIcons.add(iconReload) listIcons.add(iconPlus) binding.mainToolbar.toolbarIcons.adapter = @@ -108,7 +110,9 @@ class ExpeditionScanFragment( if (item == iconReload) { viewModel.expeditionScanList(itemPalletVO!!.Pallet) } else if (item == iconPlus) { - expeditionScanAdd() + viewModel.expeditionScanAdd( + vPalletFk = itemPalletVO!!.Pallet, vTruckFk = itemExpeditionTruckVO!!.id + ) } } @@ -117,54 +121,25 @@ class ExpeditionScanFragment( LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false) } - private fun getCURDATE(date: String): String { - val c = Calendar.getInstance() - val df = SimpleDateFormat(getString(R.string.dateCompleteFormat)) - val df2 = SimpleDateFormat(getString(R.string.timeFormat)) - c.time = df.parse(date)!! - return df2.format(c.time) - } override fun observeViewModel() { with(viewModel) { - loadExpeditionScanList.observe(viewLifecycleOwner, Observer { event -> + loadExpeditionScanList.observe(viewLifecycleOwner) { event -> event.getContentIfNotHandled().notNull { printExpeditionList(it) } - }) - - loadScanList.observe(viewLifecycleOwner, Observer { event -> - event.getContentIfNotHandled().notNull { showScanExpeditions(it) } - }) - - response.observe(viewLifecycleOwner, Observer { - if (it.isError) { - ma.messageWithSound(it.errorMessage, true, false) + } + loadScanList.observe(viewLifecycleOwner) { event -> + event.getContentIfNotHandled().notNull { + showScanExpeditions(it) } - }) + } - responseCheckexpeditionScanPut.observe(viewLifecycleOwner, Observer { - if (it.isError) { - ma.messageWithSound(it.errorMessage, true, false) - - } else { - - - if (it.response == "1") { - mperror?.start() - - } + responseExpeditionAddHasNotSameRoute.observe(viewLifecycleOwner) { + if (it) { + mperror?.start() } - }) + } - - responseDeleteScan.observe(viewLifecycleOwner, Observer { - if (it.isError) { - ma.messageWithSound(it.errorMessage, true, false) - - } else { - viewModel.expeditionScanList(itemPalletVO!!.Pallet) - } - }) } } @@ -172,16 +147,15 @@ class ExpeditionScanFragment( val lm = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false) adapter = ExpeditionScanAdapter(it.list, object : OnScanLongClickListener { - override fun onScanLongClickListener(itemExpeditionScan: ItemExpeditionScanVO) { + override fun onScanLongClickListener(itemExpeditionScanVO: ItemExpeditionScanVO) { customDialogTwoButtons.setTitle( - getString(R.string.ticket) + itemExpeditionScan.Ticket + getString( + getString(R.string.ticket) + itemExpeditionScanVO.Ticket + getString( R.string.pallet ) + itemPalletVO!!.Pallet - ) - .setDescription(getString(R.string.expedit) + itemExpeditionScan.expeditionFk) + ).setDescription(getString(R.string.expedit) + itemExpeditionScanVO.expeditionFk) .setOkButton(getString(R.string.delete)) { viewModel.expeditionScanDel( - itemExpeditionScan.id + itemExpeditionScanVO.id, itemPalletVO!!.Pallet ) customDialogTwoButtons.dismiss() }.setKoButton(getString(R.string.cancel)) { @@ -193,23 +167,13 @@ class ExpeditionScanFragment( binding.expeditionPalletRecyclerview.adapter = adapter binding.expeditionPalletRecyclerview.layoutManager = lm - - } - - private fun expeditionScanAdd() { - viewModel.expeditionScanAdd( - vPalletFk = itemPalletVO!!.Pallet, - vTruckFk = itemExpeditionTruckVO!!.id - ) - } private fun showScanExpeditions(it: ItemScanList) { listExpeditions = ArrayList() it.list.forEach { - if (it.expeditionFk != "0") - listExpeditions.add(BarcodeVO(code = it.expeditionFk)) + if (it.expeditionFk != "0") listExpeditions.add(BarcodeVO(code = it.expeditionFk)) } customDialogList.setTitle(getString(R.string.pallet) + " " + itemPalletVO!!.Pallet + " \n(" + binding.mainToolbar.toolbarTitle.text + ")") .setOkButton(getString(R.string.test)) { @@ -219,8 +183,7 @@ class ExpeditionScanFragment( if (listExpeditions.size > 0) { onComprobarPalletViewClickListener!!.onComprobarPalletViewClickListener( - itemExpeditionTruckVO!!, - ItemPalletVO(Pallet = it.list.get(0).palletFk) + itemExpeditionTruckVO!!, ItemPalletVO(Pallet = it.list[0].palletFk) ) } else { @@ -236,7 +199,6 @@ class ExpeditionScanFragment( customDialogList.dismiss() - }.setKoButton(getString(R.string.close)) { ma.hideKeyboard(customDialogList.getEditText()) listExpeditions = ArrayList() @@ -248,24 +210,22 @@ class ExpeditionScanFragment( customDialogList.getEditText().setOnEditorActionListener { v, actionId, event -> if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5) { - if (!customDialogList.getValue().isNullOrEmpty()) { + if (customDialogList.getValue().isNotEmpty()) { try { listExpeditions.add(BarcodeVO(code = customDialogList.getValue())) viewModel.expeditionScanPut( - it.list[0].palletFk, - customDialogList.getValue().toInt() - ) - checkRouteExpeditionScanPut( - it.list[0].palletFk, - customDialogList.getValue().toInt() + it.list[0].palletFk, customDialogList.getValue().toInt() ) - expeditionAdapter!!.notifyDataSetChanged() + viewModel.checkRouteExpeditionScanPut( + it.list[0].palletFk, customDialogList.getValue().toInt() + ) + + expeditionAdapter!!.notifyItemInserted(listExpeditions.size - 1) } catch (ex: Exception) { getString(R.string.errorInput) } - } customDialogList.setValue("") ma.hideKeyboard(customDialogList.getEditText()) @@ -284,14 +244,6 @@ class ExpeditionScanFragment( customDialogList.getRecyclerView().layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false) - - } - - private fun checkRouteExpeditionScanPut(palletFk: Int, expedition: Int) { - - viewModel.checkRouteExpeditionScanPut(palletFk, expedition) - - } } \ No newline at end of file diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionScanSorterFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionScanSorterFragment.kt index ad314e9c..874deef7 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionScanSorterFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionScanSorterFragment.kt @@ -52,7 +52,7 @@ class ExpeditionScanSorterFragment( override fun observeViewModel() { with(viewModel) { - responsescan.observe(viewLifecycleOwner) { + loadResponseScan.observe(viewLifecycleOwner) { listExpeditionScan.add(GeneralItem(itemScaned, itemScaned)) customDialogList.setDescription(getString(R.string.total) + ":" + listExpeditionScan.size) expeditionScanAdapter!!.notifyItemInserted(listExpeditionScan.size - 1) @@ -91,7 +91,7 @@ class ExpeditionScanSorterFragment( expeditionScanAdapter = GeneralAdapter(listExpeditionScan, object : OnGeneralItemRowClickListener { - override fun OnGeneralItemRowClickListener(item: GeneralItem) { + override fun onGeneralItemRowClickListener(item: GeneralItem) { } }) diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionScanSorterViewModel.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionScanSorterViewModel.kt index edb5f762..4aca989f 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionScanSorterViewModel.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionScanSorterViewModel.kt @@ -9,8 +9,8 @@ import retrofit2.Response class ExpeditionScanSorterViewModel(val context: Context) : BaseViewModel(context) { - private val _responseScan by lazy { MutableLiveData() } - val responsescan: LiveData + private val _responseScan by lazy { MutableLiveData() } + val loadResponseScan: LiveData get() = _responseScan fun expeditionScan(vExpeditionId: String) { diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionScanViewModel.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionScanViewModel.kt index caa34a74..a36a056b 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionScanViewModel.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionScanViewModel.kt @@ -6,73 +6,58 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.map import es.verdnatura.domain.SalixCallback import es.verdnatura.presentation.base.BaseViewModel -import es.verdnatura.presentation.base.getMessageFromAllResponse -import es.verdnatura.presentation.base.nameofFunction import es.verdnatura.presentation.common.Event -import es.verdnatura.presentation.common.ResponseItemVO import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionScanList import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionScanVO +import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletListVO +import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletVO import es.verdnatura.presentation.view.feature.paletizador.model.ItemScanList import es.verdnatura.presentation.view.feature.paletizador.model.ItemScanVO import retrofit2.Response class ExpeditionScanViewModel(val context: Context) : BaseViewModel(context) { + private val _expeditionPalletList by lazy { MutableLiveData() } + val loadExpeditionPalletList: LiveData> = + _expeditionPalletList.map { Event(it) } + private val _expeditionScanList by lazy { MutableLiveData() } - val loadExpeditionScanList: LiveData> = _expeditionScanList.map { Event(it) } + val loadExpeditionScanList: LiveData> = + _expeditionScanList.map { Event(it) } private val _scanList by lazy { MutableLiveData() } val loadScanList: LiveData> = _scanList.map { Event(it) } - private val _response by lazy { MutableLiveData() } - val response: LiveData - get() = _response + private val _responseExpeditionAddHasNotSameRoute by lazy { MutableLiveData() } + val responseExpeditionAddHasNotSameRoute: LiveData + get() = _responseExpeditionAddHasNotSameRoute - private val _responseDeleteScan by lazy { MutableLiveData() } - val responseDeleteScan: LiveData - get() = _responseDeleteScan + private val _responseScanPut by lazy { MutableLiveData() } + val responseScanPut: LiveData = _responseScanPut - private val _responseCheckexpeditionScanPut by lazy { MutableLiveData() } - val responseCheckexpeditionScanPut: LiveData - get() = _responseCheckexpeditionScanPut + val loadResponseScanPut: LiveData> = + _responseScanPut.map { Event(it) } + fun expeditionPalletList(vTruckFk: Int) { + salix.expeditionPalletList( + arrayListOf(vTruckFk) + ) + .enqueue(object : + SalixCallback>(context) { + override fun onSuccess(response: Response>) { + + _expeditionPalletList.value = + response.body()?.let { ItemPalletListVO(it) } + + } + + }) + } fun expeditionScanList(vPalletFk: Int) { salix.expeditionScanList(arrayListOf(vPalletFk)) - .enqueue(object : - SalixCallback>(context) { + .enqueue(object : SalixCallback>(context) { override fun onSuccess(response: Response>) { - if (response.body() != null) { - _expeditionScanList.value = - response.body()?.let { ItemExpeditionScanList(it) } - } else { - val listError: ArrayList = ArrayList() - listError.add( - ItemExpeditionScanVO( - 0, - isError = true, - errorMessage = getMessageFromAllResponse( - nameofFunction(this), - response.message() - ) - ) - ) - _expeditionScanList.value = ItemExpeditionScanList(listError) - } - } - - override fun onError(t: Throwable) { - val listError: ArrayList = ArrayList() - listError.add( - ItemExpeditionScanVO( - 0, - isError = true, - errorMessage = getMessageFromAllResponse( - nameofFunction(this), - t.message!! - ) - ) - ) - _expeditionScanList.value = ItemExpeditionScanList(listError) + _expeditionScanList.value = response.body()?.let { ItemExpeditionScanList(it) } } }) @@ -80,120 +65,51 @@ class ExpeditionScanViewModel(val context: Context) : BaseViewModel(context) { fun expeditionScanAdd(vPalletFk: Int, vTruckFk: Int) { salix.expeditionScanAdd(arrayListOf(vPalletFk, vTruckFk)) - .enqueue(object : - SalixCallback>(context) { + .enqueue(object : SalixCallback>(context) { override fun onSuccess(response: Response>) { - if (response.body() != null) { - _scanList.value = response.body()?.let { ItemScanList(it) } - } else { - val listError: ArrayList = ArrayList() - listError.add( - ItemScanVO( - "", 0, - isError = true, - errorMessage = getMessageFromAllResponse( - nameofFunction(this), - response.message() - ) - ) - ) - _scanList.value = ItemScanList(listError) - } - } - override fun onError(t: Throwable) { - val listError: ArrayList = ArrayList() - listError.add( - ItemScanVO( - "", 0, - isError = true, - errorMessage = getMessageFromAllResponse( - nameofFunction(this), - t.message!! - ) - ) - ) - _scanList.value = ItemScanList(listError) + _scanList.value = response.body()?.let { ItemScanList(it) } + } }) } fun expeditionScanPut( - vPalletFk: Int, - vExpeditionFk: Int + vPalletFk: Int, vExpeditionFk: Int ) { salix.expeditionScanPut( arrayListOf(vPalletFk, vExpeditionFk) - ) - .enqueue(object : SalixCallback(context) { - override fun onSuccess(response: Response) { - _response.value = - ResponseItemVO(isError = false, response = response.message()) + ).enqueue(object : SalixCallback(context) { + override fun onSuccess(response: Response) { + _responseScanPut.value = vExpeditionFk + } - } - - override fun onError(t: Throwable) { - _response.value = ResponseItemVO( - isError = true, errorMessage = getMessageFromAllResponse( - nameofFunction(this), t.message!! - ) - ) - } - }) + }) } fun checkRouteExpeditionScanPut( - vPalletFk: Int, - vExpeditionFk: Int + vPalletFk: Int, vExpeditionFk: Int ) { salix.expeditionCheckRoute( arrayListOf( - vPalletFk, - vExpeditionFk + vPalletFk, vExpeditionFk ) - ).enqueue(object : SalixCallback(context) { - override fun onSuccess(response: Response) { - if (response.body() == null) { - _responseCheckexpeditionScanPut.value = ResponseItemVO( - isError = true, - errorMessage = getMessageFromAllResponse( - nameofFunction(this), - response.message() - ) - ) - } else { - _responseCheckexpeditionScanPut.value = - ResponseItemVO(isError = false, response = response.body()!!) + ).enqueue(object : SalixCallback(context) { + override fun onSuccess(response: Response) { - } + _responseExpeditionAddHasNotSameRoute.value = response.body()!! == 1 } - override fun onError(t: Throwable) { - _responseCheckexpeditionScanPut.value = ResponseItemVO( - isError = true, - errorMessage = getMessageFromAllResponse(nameofFunction(this), t.message!!) - ) + }) + } + + fun expeditionScanDel(expeditionFk: Int, palletId: Int) { + salix.expeditionScanDel(expeditionFk).enqueue(object : SalixCallback(context) { + + override fun onSuccess(response: Response) { + expeditionScanList(palletId) } }) } - fun expeditionScanDel(vScanFk: Int) { - salix.expeditionScanDel(vScanFk) - .enqueue(object : SalixCallback(context) { - override fun onError(t: Throwable) { - _responseDeleteScan.value = ResponseItemVO( - isError = true, errorMessage = getMessageFromAllResponse( - nameofFunction(this), t.message!! - ) - ) - } - - override fun onSuccess(response: Response) { - - _responseDeleteScan.value = - ResponseItemVO(isError = false, response = response.message()) - } - }) - } - } diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionTruckListFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionTruckListFragment.kt index 12a2bed9..a5426789 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionTruckListFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionTruckListFragment.kt @@ -91,9 +91,7 @@ class ExpeditionTruckListFragment : loadExpeditionTruckList.observe(viewLifecycleOwner) { event -> event.getContentIfNotHandled().notNull { printExpeditionList(it) } } - loadResponseExpeditionAdd.observe(viewLifecycleOwner) { event -> - viewModel.expeditionTruckList() - } + } } diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionTruckListViewModel.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionTruckListViewModel.kt index ad93508b..47695c21 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionTruckListViewModel.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/ExpeditionTruckListViewModel.kt @@ -21,9 +21,6 @@ class ExpeditionTruckListViewModel(val context: Context) : BaseViewModel(context val loadExpeditionTruckList: LiveData> = _expeditionTruckList.map { Event(it) } - private val _response by lazy { MutableLiveData() } - val loadResponseExpeditionAdd: LiveData> = _response.map { Event(it) } - fun expeditionTruckList() { salix.roadMapStopGet( """{"where": { "eta": { @@ -33,8 +30,7 @@ class ExpeditionTruckListViewModel(val context: Context) : BaseViewModel(context }"]}},"order": "eta ASC"}""" ).enqueue(object : SalixCallback>(context) { override fun onSuccess(response: Response>) { - _expeditionTruckList.value = - response.body()?.let { ItemExpeditionTruckList(it) } + _expeditionTruckList.value = response.body()?.let { ItemExpeditionTruckList(it) } } }) } @@ -42,12 +38,11 @@ class ExpeditionTruckListViewModel(val context: Context) : BaseViewModel(context fun roadMapStopAdd(vHour: String, vDescription: String) { salix.roadMapStopAdd( ExpeditionTruckSalix( - eta = "${LocalDate.now()} $vHour", - description = vDescription + eta = "${LocalDate.now()} $vHour", description = vDescription ) ).enqueue(object : SalixCallback(context) { override fun onResponse(call: Call, response: Response) { - _response.value = response.message() + expeditionTruckList() } })