From 61e6aa2a5ac59a11a0eaac7b6a2a57ca310938d6 Mon Sep 17 00:00:00 2001 From: Sergio De la torre Date: Wed, 20 Nov 2024 07:55:47 +0100 Subject: [PATCH] feat: refs #8176 refactorSearchDialog --- .../view/commom/SearchableRecyclerView.kt | 7 +- .../ajustes/fragment/AjustesFragment.kt | 3 +- .../buscaritem/fragment/BuscarItemFragment.kt | 2 +- .../packaging/fragment/SupplierFragment.kt | 188 ++++++++---------- .../packaging/fragment/SupplierViewModel.kt | 2 +- 5 files changed, 94 insertions(+), 108 deletions(-) diff --git a/app/src/main/java/es/verdnatura/presentation/view/commom/SearchableRecyclerView.kt b/app/src/main/java/es/verdnatura/presentation/view/commom/SearchableRecyclerView.kt index 90d568fe..b44a7043 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/commom/SearchableRecyclerView.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/commom/SearchableRecyclerView.kt @@ -40,7 +40,7 @@ class SearchableRecyclerView @JvmOverloads constructor( } override fun onQueryTextChange(newText: String?): Boolean { - adapter.filter(newText ?: "") + if (::adapter.isInitialized) adapter.filter(newText ?: "") return true } }) @@ -72,4 +72,9 @@ class SearchableRecyclerView @JvmOverloads constructor( return this } + + fun setSearchHintWithoutFocus(searchHint: String): SearchableRecyclerView { + searchView.queryHint = searchHint + return this + } } diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/ajustes/fragment/AjustesFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/ajustes/fragment/AjustesFragment.kt index 314f2759..5787f391 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/ajustes/fragment/AjustesFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/ajustes/fragment/AjustesFragment.kt @@ -121,8 +121,7 @@ class AjustesFragment : binding.searchableRecyclerView.setAdapter(adapter, listNames) binding.searchableRecyclerView.visibility = View.VISIBLE - binding.searchableRecyclerView.setSearchHint(getString(R.string.sectorSearch)) - + binding.searchableRecyclerView.setSearchHintWithoutFocus(getString(R.string.sectorSearch)) } private fun setToolBar() { diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/buscaritem/fragment/BuscarItemFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/buscaritem/fragment/BuscarItemFragment.kt index a9cdddd0..f95a65e5 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/buscaritem/fragment/BuscarItemFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/buscaritem/fragment/BuscarItemFragment.kt @@ -45,7 +45,7 @@ class BuscarItemFragment( if (binding.editItemFk.text.toString().isNotEmpty()) try { getLocations( itemScanValue( - binding.editItemFk.text.toString(), arrayOf("buy"), "more" + binding.editItemFk.text.toString(), arrayOf("buy"), "more" ) ) } catch (ex: Exception) { diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/SupplierFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/SupplierFragment.kt index 3bcc6d8d..f146f0e0 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/SupplierFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/SupplierFragment.kt @@ -12,15 +12,13 @@ import es.verdnatura.domain.ConstAndValues.SUPPLIERID import es.verdnatura.domain.ConstAndValues.SUPPLIERNAME import es.verdnatura.presentation.base.BaseFragment import es.verdnatura.presentation.common.OnPasillerosItemClickListener +import es.verdnatura.presentation.view.commom.NameWithId +import es.verdnatura.presentation.view.commom.SearchableAdapter import es.verdnatura.presentation.view.component.CustomDialog -import es.verdnatura.presentation.view.feature.inventario.fragment.SearchSupplierModel import es.verdnatura.presentation.view.feature.packaging.model.EntrySalix -import es.verdnatura.presentation.view.feature.packaging.model.Supplier import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO -import ir.mirrajabi.searchdialog.SimpleSearchDialogCompat import kotlinx.coroutines.runBlocking -@Suppress("UNUSED_ANONYMOUS_PARAMETER") class SupplierFragment( var entryPoint: String = "" @@ -31,8 +29,7 @@ class SupplierFragment( private var pasillerosItemClickListener: OnPasillerosItemClickListener? = null private var layoutManager: LinearLayoutManager? = null private var onBack = false - private val suppliers = ArrayList() - private val entries = ArrayList() + private val entries = ArrayList() private lateinit var customDialog: CustomDialog companion object { @@ -59,27 +56,14 @@ class SupplierFragment( override fun init() { binding.filterSupplier.setOnClickListener { - SimpleSearchDialogCompat( - context, - getString(R.string.suppliers), - getString(R.string.escribirparteNombre), - null, - suppliers - ) { baseSearchDialogCompat, nombre, position -> - binding.filterSupplier.text = (nombre.getName()) - runBlocking { - mobileApplication.dataStoreApp.editDataStoreKey( - SUPPLIERNAME, - nombre.getName() - ) - mobileApplication.dataStoreApp.editDataStoreKey( - SUPPLIERID, - nombre.getId().toInt() - ) - } - viewModel.getEntriesFromSupplier(nombre.getId().toInt()) - baseSearchDialogCompat.dismiss() - }.show() + binding.searchableSuplierRecyclerView.visibility = View.VISIBLE + binding.searchableSuplierRecyclerView.setSearchHint(getString(R.string.supplierSearch)) + + } + binding.filterEntry.setOnClickListener { + binding.searchableEntriesRecyclerView.visibility = View.VISIBLE + binding.searchableEntriesRecyclerView.setSearchHint(getString(R.string.entrySearch)) + } customDialog = CustomDialog(requireContext()) ma.hideBottomNavigation(View.GONE) @@ -89,6 +73,64 @@ class SupplierFragment( super.init() } + private fun setSearchable(listNames: MutableList) { + + val adapter = + SearchableAdapter( + listElements = listNames, + context = requireContext() + ) { elementSelected -> + binding.filterSupplier.text = (elementSelected.name) + runBlocking { + mobileApplication.dataStoreApp.editDataStoreKey( + SUPPLIERNAME, + elementSelected.name + ) + mobileApplication.dataStoreApp.editDataStoreKey( + SUPPLIERID, + elementSelected.id + ) + } + viewModel.getEntriesFromSupplier(elementSelected.id) + binding.searchableSuplierRecyclerView.visibility = View.GONE + + } + + binding.searchableSuplierRecyclerView.setAdapter(adapter, listNames) + + } + + private fun setSearchableEntries(listNames: MutableList) { + + val adapter = + SearchableAdapter( + listElements = listNames, + context = requireContext() + ) { elementSelected -> + binding.filterEntry.text = elementSelected.name + runBlocking { + mobileApplication.dataStoreApp.editDataStoreKey( + ENTRYID, + elementSelected.id + ) + mobileApplication.dataStoreApp.editDataStoreKey( + ENTRYOBSERVATIONORIGINAL, + entries.find { it.id == elementSelected.id }?.observation ?: "" + ) + mobileApplication.dataStoreApp.deleteImages() + } + ma.onPasillerosItemClickListener( + PasillerosItemVO(title = R.string.titlePackagingCount), + getString(R.string.titlePackagingCount) + ) + binding.searchableEntriesRecyclerView.visibility = View.GONE + + } + + binding.searchableEntriesRecyclerView.setAdapter(adapter, listNames) + + } + private fun setToolBar() { binding.mainToolbar.toolbarTitle.text = entryPoint } @@ -103,41 +145,18 @@ class SupplierFragment( private fun setEntryDialog() { binding.filterEntry.visibility = View.VISIBLE - binding.filterEntry.setOnClickListener { - SimpleSearchDialogCompat( - context, - getString(R.string.entry), - getString(R.string.escribirparteNombre), - null, - entries - ) { baseSearchDialogCompat, nombre, position -> - binding.filterEntry.text = nombre.getName() - runBlocking { - mobileApplication.dataStoreApp.editDataStoreKey( - ENTRYID, - nombre.getId().toInt() - ) - mobileApplication.dataStoreApp.editDataStoreKey( - ENTRYOBSERVATIONORIGINAL, - nombre.getObservation() - ) - mobileApplication.dataStoreApp.deleteImages() - } - ma.onPasillerosItemClickListener( - PasillerosItemVO(title = R.string.titlePackagingCount), - getString(R.string.titlePackagingCount) - ) - // } - baseSearchDialogCompat.dismiss() - }.show() - } } override fun observeViewModel() { with(viewModel) { - supplierList.observe(viewLifecycleOwner) { - createSupplierList(it.list) + supplierList.observe(viewLifecycleOwner) { item -> + setSearchable(item.list.map { + NameWithId( + it.id!!, + it.name!! + ) + } as MutableList) } entryList.observe(viewLifecycleOwner) { createEntryList(it.list) @@ -162,57 +181,20 @@ class SupplierFragment( } } - private fun createSupplierList(list: List) { - suppliers.clear() - if (list.isNotEmpty()) - - list.forEach { supplier -> - - try { - suppliers.add( - SearchSupplierModel( - supplier.name, - supplier.id.toString(), - observation = "" - ) - ) - } catch (ex: Exception) { - ma.messageWithSound( - message = ex.message.toString(), - isError = true, - isPlayed = true - ) - } - - } - } - private fun createEntryList(list: List) { list.sortedBy { it.travel?.landed } entries.clear() - list.forEach { entry -> - if (!entry.isError) { - - try { - entries.add( - SearchSupplierModel( - entry.id.toString() + "->" + (entry.travel?.landed), - id = entry.id.toString(), - observation = entry.observation - ) - ) - - } catch (ex: Exception) { - ma.messageWithSound( - message = ex.message.toString(), - isError = true, - isPlayed = true - ) - } - } + if (list.isNotEmpty() && !list[0].isError) { + setSearchableEntries(list.map { entry -> + NameWithId( + entry.id!!, + entry.id.toString() + "->" + (entry.travel?.landed) + ) + } as MutableList) + entries.addAll(list.sortedBy { it.travel?.landed }) } + binding.filterEntry.visibility = View.VISIBLE - setEntryDialog() } } diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/SupplierViewModel.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/SupplierViewModel.kt index 1c1a8be1..28fa47fd 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/SupplierViewModel.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/SupplierViewModel.kt @@ -70,7 +70,7 @@ class SupplierViewModel(val context: Context) : BaseViewModel(context) { }) } - fun getEntriesFromSupplier(supplier: Int) { + fun getEntriesFromSupplier(supplier: Number) { val calendar = Calendar.getInstance() calendar.add(Calendar.DAY_OF_YEAR, 0)