Last active
January 24, 2020 13:32
-
-
Save lgawin/22fd8cc4d90674deb4487980b9e94552 to your computer and use it in GitHub Desktop.
Some helper methods for RecyclerView's `DiffUtil`s
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.recyclerview.widget.DiffUtil | |
import kotlin.reflect.KProperty1 | |
@Suppress("NOTHING_TO_INLINE") | |
inline fun <T : Any, R: Any> byProperty(kProperty: KProperty1<T, R>): (T, T) -> Boolean = | |
{ old, new -> kProperty.get(old) == kProperty.get(new) } | |
@Suppress("FunctionName") | |
inline fun <T : Any> DiffItemCallback( | |
crossinline areItemsTheSame: (T, T) -> Boolean, | |
crossinline areContentsTheSame: (T, T) -> Boolean = { old, new -> old == new } | |
) = object : DiffUtil.ItemCallback<T>() { | |
override fun areItemsTheSame(oldItem: T, newItem: T) = areItemsTheSame(oldItem, newItem) | |
override fun areContentsTheSame(oldItem: T, newItem: T) = areContentsTheSame(oldItem, newItem) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment