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 11b8f088..12bac0ec 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,7 +15,7 @@ import es.verdnatura.presentation.view.feature.delivery.viewmodels.DeliveryViewM import es.verdnatura.presentation.view.feature.ubicador.adapter.AutomaticAdapter @Suppress("UNUSED_ANONYMOUS_PARAMETER") -class ExpeditionPreparedStateFragment : +class ExpeditionPreparedStateFragment(var codeState: String, var title: String) : BaseFragment( DeliveryViewModel::class ) { @@ -24,19 +24,22 @@ class ExpeditionPreparedStateFragment : private var contador = 0 private var isScanned = false private val listExpeditions: ArrayList = arrayListOf() + private var expeditionStateTypeId: Number = 0 companion object { - fun newInstance() = ExpeditionPreparedStateFragment() + fun newInstance(codeState: String, title: String) = + ExpeditionPreparedStateFragment(codeState = codeState, title = title) } override fun getLayoutId(): Int = R.layout.fragment_automatic_add_expedition override fun init() { ma.hideBottomNavigation(View.GONE) - binding.mainToolbar.toolbarTitle.text = getString(R.string.scanExpeditions) + binding.mainToolbar.toolbarTitle.text = title setEvents() setViews() setToolBar() + viewModel.getExpeditionStateId(codeState) super.init() } @@ -46,7 +49,7 @@ class ExpeditionPreparedStateFragment : private fun setSubtitle() { binding.mainToolbar.toolbarSubtitle.text = - getString(R.string.countNumber, getString(R.string.label), contador) + getString(R.string.countNumber, getString(R.string.label), listExpeditions.size) } private fun setViews() { @@ -54,12 +57,12 @@ class ExpeditionPreparedStateFragment : AutomaticAdapter(listExpeditions.map { it.expeditionFk.toLong() } as ArrayList, object : OnAutomaticItemClickListener { override fun onAutomaticItemClickListener(position: Int) { - contador -= 1 - setSubtitle() + if (listExpeditions.size > position) { listExpeditions.removeAt(position) } adapter!!.updateItems(listExpeditions.map { it.expeditionFk.toLong() } as ArrayList) + setSubtitle() } }) binding.itemsRecyclerview.adapter = adapter @@ -75,21 +78,25 @@ class ExpeditionPreparedStateFragment : isScanned = event != null && event.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_ENTER if (binding.editMatricula.text.toString().isNotEmpty()) { - contador += 1 - setSubtitle() try { - - listExpeditions.add( - ExpeditionSalix( - expeditionFk = itemScanValue( - textScanned_filterDouble(binding.editMatricula.text.toString()), - arrayOf("expedition"), - "id" - ).toString().toLong(), stateCode = "PREPARED", isScanned = 0 + val itemScaned = itemScanValue( + textScanned_filterDouble(binding.editMatricula.text.toString()), + arrayOf("expedition"), + "id" + ).toString().toLong() + if (!(listExpeditions.any { it.expeditionFk == itemScaned })) { + viewModel.findExpedition( + itemScaned, expeditionStateTypeId ) - ) - adapter!!.updateItems(listExpeditions.map { it.expeditionFk.toLong() } as ArrayList) - binding.itemsRecyclerview.scrollToPosition(0) + binding.itemsRecyclerview.scrollToPosition(0) + } else { + ma.messageWithSound( + message = getString(R.string.boxDuplicated), + isError = true, + isPlayed = true, + isToasted = true + ) + } } catch (ex: Exception) { ma.messageWithSound(ex.message.toString(), isError = true, isPlayed = true) } @@ -108,12 +115,12 @@ class ExpeditionPreparedStateFragment : } binding.buttonFinalizar.setOnClickListener { - - if (listExpeditions.isNotEmpty()) viewModel.expeditionStateAddSalix( - Expeditions( - listExpeditions - ) - ) else ma.onMyBackPressed() + if (listExpeditions.isNotEmpty()) + viewModel.expeditionStateAddSalix( + Expeditions( + listExpeditions + ) + ) else ma.onMyBackPressed() } binding.mainToolbar.backButton.setOnClickListener { @@ -126,8 +133,34 @@ class ExpeditionPreparedStateFragment : responseStateAdd.observe(viewLifecycleOwner) { ma.onMyBackPressed() } + responseExpeditionStateId.observe(viewLifecycleOwner) { + expeditionStateTypeId = it + } + + responseFindExpeditionId.observe(viewLifecycleOwner) { + + if (it != 0) { + listExpeditions.add( + ExpeditionSalix( + expeditionFk = it, + stateCode = codeState, + isScanned = if (isScanned) 1 else 0 + ) + ) + setSubtitle() + adapter!!.updateItems(listExpeditions.map { it.expeditionFk.toLong() } as ArrayList) + } else { + ma.messageWithSound( + if (codeState == "PREPARED") getString(R.string.expeditionPrepared) else getString( + R.string.errorBoxPicking + ), + isError = true, + isPlayed = true, + isToasted = true + ) + } + } } - } - } +