Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class BlogItemDiffCallback(
var oldBlogList: List<BlogPost>,
var newBlogList: List<BlogPost>
): DiffUtil.Callback() {
override fun areItemsTheSame(
oldItemPosition: Int,
newItemPosition: Int
): Boolean {
return (oldBlogList.get(oldItemPosition).pk
== newBlogList.get(newItemPosition).pk)
}
override fun getOldListSize(): Int {
return oldBlogList.size
}
override fun getNewListSize(): Int {
return newBlogList.size
}
override fun areContentsTheSame(
oldItemPosition: Int,
newItemPosition: Int
): Boolean {
return (oldBlogList.get(oldItemPosition)
== newBlogList.get(newItemPosition))
}
}
fun submitList(blogList: List<BlogPost>){
CoroutineScope(Main).launch{
val oldList = items
withContext(IO){
val diffResult: DiffUtil.DiffResult = DiffUtil.calculateDiff(
BlogItemDiffCallback(
oldList,
blogList
)
)
withContext(Main){
items = blogList
diffResult.dispatchUpdatesTo(this@MainRecyclerAdapter)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.