I am using DiffUtil with ListAdapter in Android Kotlin. I am calling the data from the server in the onResume method. When onResume called every item whole data is redrawing the view. I want to update the view if any data change on the server side, so it will reflect in the app.
ListActivity.kt
class ListActivity : BaseActivity() {
lateinit var binding: ListActivityLayoutBinding
private val viewModel: ListViewModel by inject()
private var listAdapter: listAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setupViewModel()
binding = ListActivityLayoutBinding.inflate(layoutInflater)
setContentView(binding.root)
}
private fun setupViewModel() {
viewModel.liveData.observe(this, { list ->
setupAdapter(list)
})
}
private fun setupAdapter(list: List<XYZ>) {
initializeAdapter()
listAdapter?.submitList(list)
binding.recyclerView.adapter = listAdapter
}
private fun initializeAdapter() {
viewModel.abc?.let { abc ->
listAdapter = ListAdapter(abc, object : Listener<XYZ> {
override fun selectedItem(item: XYZ) {
// calling
}
}
})
} ?: run {
Log.e("Error", "Error for fetching data")
}
}
override fun onResume() {
super.onResume()
viewModel.fetchData()
}
}
XYZ.kt
data class XYZ(
val id: String? = null,
val title: String? = null,
val count: Int? = null,
val status: String? = null,
val item: Qqq? = null
)
QQQ.kt
data class Qqq(
val id: String? = null,
val rr: Rr? = null
)
Rr.kt
data class Rr(
val firstName: String? = null,
val lastName: String? = null,
)
ListAdapter.kt
class ListAdapter(
private val abc: Abc,
private val listener: Listener <XYZ>
) : ListAdapter<XYZ, ListViewHolder>(LIST_COMPARATOR) {
companion object {
private val LIST_COMPARATOR = object : DiffUtil.ItemCallback<XYZ>() {
override fun areItemsTheSame(oldItem: XYZ, newItem: XYZ): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: XYZ, newItem: XYZ): Boolean {
return ((oldItem.title == newItem.title) && (oldItem.status == newItem.status)
&& (oldItem.count == newItem.count)
&& (oldItem.item == newItem.item))
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListViewHolder {
return ListViewHolder.bindView(parent, abc)
}
override fun onBindViewHolder(holder: ListViewHolder, position: Int) {
holder.bindItem(getItem(position), listener)
}
}
ListViewModel.Kt
class ListViewModel : BaseViewModel() {
var abc: Abc? = null
private var xyz: List<XYZ>? = null
var liveData: MutableLiveData<List<XYZ>> = MutableLiveData()
fun fetchData() {
viewModelScope.launch {
val firstAsync = async {
if (abc == null) {
abc = getAbc() // First retrofit call
}
}
val secondAsync = async {
xyz = getXYZ() // Second retrofit call
}
firstAsync.await()
secondAsync.await()
liveData.postValue(xyz)
}
}
}
Note I want to check abc is not null in every call.
1. Is my DiffUitll Callback is correct?
2. First Initial call I want to redraw every item but, if I call viewModel.fetchData()
in onResume
if there are any changes that I need to do otherwise I don't want to redraw my whole list. Is there any suggestions?