Search doesn't work as expected after selecting an item from the BottomSheetDialogFragment and then select the bottomNavigation menu

305 views Asked by At

I have an awkward situation for which I have been trying to find the reason and fix it for two days and I failed.

Let me explain it first.

On launching the app recyclerView in the 'HomeFragment' is filled with data that fetched from Firestore. I have a search bar on the toolbar at the top and when I type in something, it narrows down the list of items in the recyclerView. I have a FloatingActionButton which will bring up a BottomSheetDialogFragment that contains item categories. When I select a category from it, the recyclerView in the 'HomeFragment' is populated based on the category selected. There is no issues until here.

If I do a search after selecting the category, it narrows down the list of items without any issues. When I select the 'HomeFragment' from the bottomNavigation, after selecting a category from the BottomSheetDialogFragment, the search result is not showing. However, if I select the 'HomeFragment' from the bottomNavigation after selecting any other bottom menu item, but not go to the BottomSheetDialogFragment and select a category, then there is no issue with the search. It happens only when I select the category and then go back to the 'HomeFragment' by hitting the bottom menu. Can somebody help me find and fix the issue?

Following is what I have in the 'HomeFragment' for search.

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    menu.clear() // THIS LINE OF CODE IS ADDED SO THAT WHEN WE SELECT A CATEGORY FROM THE BOTTOMSHEET THE MENU ITEMS WON'T DUPLICATE
    inflater.inflate(R.menu.home_menu, menu)
    super.onCreateOptionsMenu(menu, inflater)

    val item = menu.findItem(R.id.my_search_bar)
    val searchView = item?.actionView as SearchView

    searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {

        override fun onQueryTextSubmit(query: String?): Boolean {
            srchTempProductsList.clear()
            if (query != null) {
                if (query.isNotEmpty()) {
                    srchProductsList.forEach {
                        if (it.label.toLowerCase(Locale.getDefault()).contains(query)) {
                            srchTempProductsList.add(it)
                        }
                        binding.rvHomeItems.adapter?.notifyDataSetChanged()
                    }
                    if (srchTempProductsList.size == 0) {
                        showCustomAlertDialog()
                    }
                } else {
                    srchTempProductsList.clear()
                    srchTempProductsList.addAll(srchProductsList)
                    binding.rvHomeItems.adapter?.notifyDataSetChanged()
                }
            }
            return false
        }

        override fun onQueryTextChange(newText: String?): Boolean {

            srchTempProductsList.clear()
            val searchText = newText!!.toLowerCase(Locale.getDefault())

            if (searchText.isNotEmpty()) {
                srchProductsList.forEach {
                    if (it.label.toLowerCase(Locale.getDefault()).contains(searchText)) {
                        srchTempProductsList.add(it)
                    }
                    binding.rvHomeItems.adapter?.notifyDataSetChanged()
                }

                if (srchTempProductsList.size == 0) {
                    showCustomAlertDialog()
                }

            } else {
                srchTempProductsList.clear()
                srchTempProductsList.addAll(srchProductsList)
                binding.rvHomeItems.adapter?.notifyDataSetChanged()
            }

            return false

        }
    })

}

I have the following in the 'BottomSheetDialogFragment' to pass the category to the 'HomeFragment'

    categoryAdapter.setOnClickListener(object :HomeCategoryListAdapter.OnClickListener{
        override fun onClick(position: Int, category: Categories) {
            val myFragment = HomeFragment()
            val bundle = Bundle()
            bundle.putString("category", category.category)
            myFragment.arguments = bundle
            fragmentManager?.beginTransaction()?.replace(R.id.nav_host_fragment,myFragment)?.commit()
            dismiss()
        }
    })

and the following in the onCreateView of 'HomeFragment'

    val bundle = this.arguments
    if (bundle!=null) {
        if (bundle.getString("category")!="All Products"){
            filterCategory = bundle.getString("category")
        }else{
            filterCategory =null
        }
    }

and this is how I get the product list in 'HomeFragment'

fun getProductList() {
    srchProductsList.clear()
    srchTempProductsList.clear()

    if (filterCategory!=null){
        mFireStore.collection("prods")
            .whereEqualTo("category",filterCategory)
            .get()
            .addOnCompleteListener {
                if (it.isSuccessful) {
                    for (document in it.result) {
                        val product = document.toObject(Product::class.java)
                        product.product_id = document.id
                        srchProductsList.add(product)
                    }
                } else {
                }
                srchTempProductsList.addAll(srchProductsList)
                listProductBasedOnView()
            }
            .addOnFailureListener {
                Log.d("Known Error", "This ....")
            }
    }else{

        mFireStore.collection("prods")
            .get()
            .addOnCompleteListener {
                if (it.isSuccessful) {
                    for (document in it.result) {
                        val product = document.toObject(Product::class.java)
                        product.product_id = document.id
                        srchProductsList.add(product)
                    }
                } else {
                }
                srchTempProductsList.addAll(srchProductsList)
                listProductBasedOnView()
            }
            .addOnFailureListener {
                Log.d("Known Error", "This ...")
            }
    }

}
0

There are 0 answers