Created
April 5, 2020 02:41
-
-
Save satoshun/7686b4fca739aade1c36f3e723f825a7 to your computer and use it in GitHub Desktop.
This file contains 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 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