Skip to content

Instantly share code, notes, and snippets.

@satoshun
Created April 5, 2020 02:41
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 satoshun/7686b4fca739aade1c36f3e723f825a7 to your computer and use it in GitHub Desktop.
Save satoshun/7686b4fca739aade1c36f3e723f825a7 to your computer and use it in GitHub Desktop.
import androidx.recyclerview.widget.DiffUtil
import com.xwray.groupie.GroupieViewHolder
import com.xwray.groupie.Item
abstract class DiffCallbackItem<T, VH : GroupieViewHolder>(
private val data: T,
private val callback: DiffUtil.ItemCallback<T>
) : Item<VH>() {
override fun isSameAs(other: Item<*>): Boolean {
if (viewType != other.viewType) {
return false
}
val otherData = other.getOtherData() ?: return false
return callback.areItemsTheSame(otherData, data)
}
override fun hasSameContentAs(other: Item<*>): Boolean {
val otherData = other.getOtherData() ?: return false
return callback.areContentsTheSame(otherData, data)
}
override fun getChangePayload(newItem: Item<*>?): Any? {
val newData = newItem?.getOtherData() ?: return null
return callback.getChangePayload(data, newData)
}
private fun Item<*>.getOtherData(): T? {
val other = this
if (other !is DiffCallbackItem<*, *>) return null
@Suppress("UNCHECKED_CAST")
return other.data as? T
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment