1

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 ...")
            }
    }

}
Codist
  • 737
  • 8
  • 23
  • As far as I understand your code, you are passing the "category" as argument in the fragment. But if you select a category in the bottomsheet, and then click on the bottomnav to change the fragment, you are essentially not passing any argument there. You should try doing that and check. – Priyansh Kedia Nov 27 '21 at 11:20
  • Well, on launching the app I want all the items to be shown regardless of their category. Hence, 'filterCategory' is set to `null` as `private var filterCategory: String?= null`. Also, if someone select "All Products" from the category, I do not want to filter my query and hence I check `if (bundle!=null)` and accordingly the value of 'filterCategory' is changed. Did you mean something else? – Codist Nov 27 '21 at 11:40
  • Well, that is what I meant. You are not saving the category when the user selects it from the bottomsheet, and hence whenever you go to the home fragment using the bottom nav, you see no filter applied to your list. Either you need to store the selected category from the bottomsheet as a static variable, or maybe in your shared preferences, or maybe even pass it as a bundle, and you will be good to go. You can also open the home fragment as soon as the user selects a category. (This depends on your choice for the flow of the app). – Priyansh Kedia Nov 27 '21 at 15:15
  • Can you check the code I have in BottomSheetDialogFragment and tell me if I am not doing it right to pass it as a bundle also in the `onCreate` of 'HomeFragment' where I am checking what category is returned as a bundle from the BottomSheetDialogFragment. Yes, I want to show the fragment as soon as the user selects a category and I have done that with `dismiss()` to close the `BottomSheetDialogFragment`. – Codist Nov 27 '21 at 15:28
  • Can somebody help me with this, thanks? – Codist Nov 28 '21 at 18:53
  • You can add onDismiss listener to your dialog, and open your fragment there – Priyansh Kedia Nov 29 '21 at 06:09
  • Will it solve the issue with the search? – Codist Nov 29 '21 at 06:12
  • 1
    `R.id.nav_host_fragment` sounds like you use Architecture components with navController in some parts of the app? or do you do all fragment transactions manually? – Zain Nov 29 '21 at 10:37
  • I replaced that with `findNavController().navigate(R.id.action_global_nav_home,bundle)` and it seems to work. – Codist Nov 29 '21 at 19:17
  • Your last comment helped me find and fix the issue, If you post an answer then I can mark that as the Answer. – Codist Dec 01 '21 at 19:57
  • Can you check if you can help me with https://stackoverflow.com/q/70150870/13935956 – Codist Dec 01 '21 at 20:06

0 Answers0