In my Fragment I'm using a Recycler View with multiple item types where i want to filter my parent array list based on some conditions and transfer its child list to child recycler view. I am able to filter parent recycler view on main thread but i am worried if the parent array list has thousands of data and performing filtration on main thread might be not good. So i want to filter my array list using coroutines. This is how i tried it using coroutines.
MainFragmentAdapter
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (position) {
1 -> {
val goldAdapter = GoldAdapter()
var filterGold = methodCallBack.filterData(gold,"gold")
if (filterGold.isNotEmpty())
goldAdapter.goldList = filterGold
if (mRate.isNotEmpty())
goldAdapter.goldRate = mRate[0].value!!
holder.itemView.diamond_text.text = "Gold"
holder.itemView.diamond_recycler.apply {
setHasFixedSize(true)
adapter = goldAdapter
}
holder.itemView.view_more_button.setOnClickListener {
methodCallBack.click(gold)
}
goldAdapter.setListener(myContext)
}
MainFragment
override fun filterData(myList: MutableList<Item>,type : String) {
mainFragmentViewModel.viewModelScope.launch {
mainFragmentViewModel.myFilter(myList,type)
}
}
MainFragmentViewModel
private val filterGoldList = MutableLiveData<MutableList<Item>>()
val _filterGold : LiveData<MutableList<Item>>
get() = filterGoldList
suspend fun myFilter(myList : MutableList<Item>,type : String) =
withContext(Dispatchers.IO){
myList.filter { it.isMainPageItem == true }
.let {
when(type){
"Gold" -> filterGoldList.postValue(it as MutableList<Item>)
"Silver" -> filterSilverList.postValue(it as MutableList<Item>)
}
}
}
I am not able to return data back to adapter class.Is there any way to observe live data in adapter class?. Please help me to achieve it..