Search code examples
androidandroid-fragmentskotlinandroid-architecture-componentsandroid-viewmodel

distinctUntilChanged in viewmodel not returning newly fetched data


I'm working with a simple searchview that when I submit a new string to search at my endpoint it should return a different list, instead , when I change that string it will not return the new requested list, instead it just returns the same one that I have fetched first

View

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setupRecyclerView()
        setupSearchView()
        setupDefaultTragosList()
    }

    private fun setupDefaultTragosList(){
        viewModel.setTrago("margarita")
        viewModel.fetchTragosList.observe(viewLifecycleOwner, Observer { result ->
            when(result){
                is Resource.Loading -> {
                    progressBar.visibility = View.VISIBLE
                }
                is Resource.Success -> {
                    progressBar.visibility = View.GONE
                    rv_tragos.adapter = MainAdapter(requireContext(),result.data,this)
                }
                is Resource.Failure -> {
                    progressBar.visibility = View.GONE
                    Log.e("MainFragment", "onRetrofitRequest: ${result.exception}")
                }
            }
        })
    }

    private fun setupSearchView(){
        searchView.setOnQueryTextListener(object:androidx.appcompat.widget.SearchView.OnQueryTextListener{

            override fun onQueryTextSubmit(query: String?): Boolean {
                viewModel.setTrago(query!!)
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {return false}
        })
    }

ViewModel

class MainViewModel(private val repo:Repo):ViewModel(){

    private val tragoNameData = MutableLiveData<String>()

    fun setTrago(tragoName:String){
        tragoNameData.value = tragoName
    }

    val fetchTragosList = tragoNameData.distinctUntilChanged().switchMap { tragoName ->
        liveData(Dispatchers.IO) {
            emit(Resource.Loading())
            try{
                emit(repo.getTragosList(tragoName))
            }catch (e: Exception){
                emit(Resource.Failure(e))
            }
        }
    }
}

it seems like distinctUntilChange is not refetching my endpoint for the newly searched drink, instead it always returns margarita list of drinks


Solution

  • With some external tech support, the answer was that

    interface WebService { 
        @GET("search.php?s=")
        suspend fun getTragoByName(@Query("tragoName") tragoName:String): DrinkList
    
        @GET("filter.php?a=")
        suspend fun getAlcoholicDrink(@Query("alcoholicOrNot") alcoholicOrNot:String): DrinkList
    }
    

    Should have been

    interface WebService {
        @GET("search.php")
        suspend fun getTragoByName(@Query("s") tragoName:String): DrinkList
    
        @GET("filter.php")
        suspend fun getAlcoholicDrink(@Query("a") alcoholicOrNot:String): DrinkList
    }