Skip to content

Instantly share code, notes, and snippets.

@mitchtabian
Last active September 9, 2019 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitchtabian/fac3402a4f22f2b291278d5df262491f to your computer and use it in GitHub Desktop.
Save mitchtabian/fac3402a4f22f2b291278d5df262491f to your computer and use it in GitHub Desktop.
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