Skip to content

Instantly share code, notes, and snippets.

@Jericho2Code
Last active July 31, 2019 08:36
Show Gist options
  • Save Jericho2Code/bcc0760afd701ee07fa665fad9854b34 to your computer and use it in GitHub Desktop.
Save Jericho2Code/bcc0760afd701ee07fa665fad9854b34 to your computer and use it in GitHub Desktop.
Simple implementation of DiffUtil.ItemCallback uses Kotlin
class BaseDiffCallback<T>(
val isItemsSame: (old: T, new: T) -> Boolean,
val isContentsSame: (old: T, new: T) -> Boolean = { old, new -> old == new }
) : DiffUtil.ItemCallback<T>() {
override fun areItemsTheSame(old: T, new: T): Boolean = isItemsSame(old, new)
override fun areContentsTheSame(old: T, new: T): Boolean = isContentsSame(old, new)
}
//sample
data class Item(val id: Long, val data: String)
fun test() {
val callback = BaseDiffCallback<Item>({ old, new -> old.id == new.id })
//use of callback
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment