Skip to content

Instantly share code, notes, and snippets.

@vorobeij
Created July 19, 2018 14:15
Show Gist options
  • Select an option

  • Save vorobeij/a86ee2e4fc0428c1d8b25a5ac4d923f6 to your computer and use it in GitHub Desktop.

Select an option

Save vorobeij/a86ee2e4fc0428c1d8b25a5ac4d923f6 to your computer and use it in GitHub Desktop.
open class BaseDiffCallback<T>(
var itemsOld: List<T> = listOf(),
var itemsNew: List<T> = listOf()
) : DiffUtil.Callback() {
lateinit var areItemsTheSame: ((oldItemPosition: Int, newItemPosition: Int) -> Boolean)
lateinit var areContentTheSame: ((oldItemPosition: Int, newItemPosition: Int) -> Boolean)
override fun getOldListSize(): Int = itemsOld.size
override fun getNewListSize(): Int = itemsNew.size
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return areItemsTheSame.invoke(oldItemPosition, newItemPosition)
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return areContentTheSame.invoke(oldItemPosition, newItemPosition)
}
}
private val diffCallback = BaseDiffCallback<User>().apply {
areItemsTheSame = { oldItemPosition, newItemPosition ->
itemsOld[oldItemPosition].id == itemsNew[newItemPosition].id
}
areContentTheSame = { oldItemPosition, newItemPosition -> true }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment