Liza
Liza

Reputation: 33

MutableLivedata isn't triggered after first time

I have a list of categories and every category has subcategories and products. when I launch the activity the data of subcategories and products are loaded in the UI correctly but when the user click on another category, the API gets the data right but the UI doesn't change

this is the call from the adapter

private fun handleClick(categoryId:Int,categoryName:String) {


    handler(categoryId,categoryName)
    notifyDataSetChanged()

}

this is the fragment that holds the categories and subcategories and products

private fun handleUserClickOnCategory(categoryId: Int, categoryName: String) {
    binding.categoryName.text = "\t"+categoryName
    viewModel.getSubcategoryAndProducts(categoryId)


}

and onViewCreated I observe the change

     viewModel.subCategoryAndProductsResult.observe(
        viewLifecycleOwner, Observer { it ->

            if(it.success =="")
            {
                Toast.makeText(context, "empty", Toast.LENGTH_LONG).show()
            }
           else if (it.error != "") {

                Toast.makeText(context, it.error.toString(), Toast.LENGTH_LONG).show()
            } else if (it.success != "") {
                var subcategoryList = it.success as ArrayList<Any>
                subcategories = viewModel.getSubCategoriesListFormatted(subcategoryList)
                Log.d("TAG", "onViewCreated: "+subcategories[0].nameEn)
                addSubcategoryAndProducts()

            }
        })
    }

My viewmodel

 var subCategoryAndProductsResult = MutableLiveData<ResultData>()
  fun getSubcategoryAndProducts(categoryId: Int) {
    Log.d("calling api", "Calling products Api")


    subCategoryAndProductsResult=
        categoriesDataSource.getSubcategoryAndProducts(categoryId, 0, 0)


}

the API function

  fun getSubcategoryAndProducts(
    categoryId: Int,
    userId: Int,
    storeId: Int
): MutableLiveData<ResultData> {

    val productsResultData = MutableLiveData<ResultData>()


    val categoriesAndProductsApis =
        retrofitClient.createService(CategoriesAndProductsApis::class.java)


    val response =
        categoriesAndProductsApis.getSubcategoryAndProducts(categoryId, userId, storeId)




                    response.enqueue(object : Callback<ApiResponse> {
                        override fun onResponse(
                            call: Call<ApiResponse>,
                            response: Response<ApiResponse>
                        ) {

                            Log.d("Products ", response.code().toString())
                            if (response.isSuccessful) {
                                val body = response.body()

                                if (body != null && body.statusCode == 200) {
                                    val result = ResultData(body.data!!, "")
                                    productsResultData.setValue(result)


                                    Log.v("Products", result.success.toString())
                                } else {
                                    val result = ResultData("", "Something went wrong")
                                    productsResultData.postValue(result)
                                    Log.v("result", result.error.toString())
                                }
                            }
                        }

                        override fun onFailure(call: Call<ApiResponse>, t: Throwable) {
                            val result = ResultData("", "Something went wrong")
                            productsResultData.setValue(result)
                            Log.v("result", result.error.toString())
                        }

                    })







    return productsResultData
}

Upvotes: 0

Views: 42

Answers (0)

Related Questions