Skip to content

Instantly share code, notes, and snippets.

@smarteist
Last active January 23, 2020 20:16
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 smarteist/d1dc514cf2abb85fab3e770db44a24ea to your computer and use it in GitHub Desktop.
Save smarteist/d1dc514cf2abb85fab3e770db44a24ea to your computer and use it in GitHub Desktop.
It's more simple callback for calculating the diff between two non-null items in a list.
/**
* It's more simple callback for calculating the diff between two non-null items in a list.
* <p>
* {@link DiffUtil.Callback} serves two roles - list indexing, and item diffing. ItemCallback handles
* just the second of these, which allows separation of code that indexes into an array or List
* from the presentation-layer and content specific diffing code.
*
* @param <T> Type of items to compare.
*/
public abstract class DiffUtilCallback<T> extends DiffUtil.Callback {
private List<T> oldItemsList;
private List<T> newItemsList;
public final DiffUtil.DiffResult calculateDiff(List<T> oldItems, List<T> newItems, boolean detectMoves) {
this.oldItemsList = oldItems;
this.newItemsList = newItems;
return DiffUtil.calculateDiff(this, detectMoves);
}
public final DiffUtil.DiffResult calculateDiff(List<T> oldItems, List<T> newItems) {
return calculateDiff(oldItems, newItems, false);
}
@Override
public final int getOldListSize() {
return oldItemsList.size();
}
@Override
public final int getNewListSize() {
return newItemsList.size();
}
@Override
public final boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return areItemsTheSame(oldItemsList.get(oldItemPosition), newItemsList.get(newItemPosition));
}
@Override
public final boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return areContentsTheSame(oldItemsList.get(oldItemPosition), newItemsList.get(newItemPosition));
}
public abstract boolean areItemsTheSame(T oldItem, T newItem);
public abstract boolean areContentsTheSame(T oldItem, T newItem);
}
/**
* It's more simple callback for calculating the diff between two non-null items in a list.
*
*
* [DiffUtil.Callback()] serves two roles - list indexing, and item diffing. ItemCallback handles
* just the second of these, which allows separation of code that indexes into an array or List
* from the presentation-layer and content specific diffing code.
*
* @param <T> Type of items to compare.
</T> */
abstract class DiffUtilCallback<T> : DiffUtil.Callback() {
private var oldItemsList: List<T> = listOf()
private var newItemsList: List<T> = listOf()
fun calculateDiff(
oldItems: List<T>,
newItems: List<T>,
detectMoves: Boolean = false
): DiffResult {
oldItemsList = oldItems
newItemsList = newItems
return DiffUtil.calculateDiff(this, detectMoves)
}
fun calculateDiff(
newItems: List<T>, detectMoves: Boolean = false
): DiffResult {
return this.calculateDiff(this.newItemsList, newItems, detectMoves)
}
override fun getOldListSize(): Int {
return oldItemsList.size
}
override fun getNewListSize(): Int {
return newItemsList.size
}
fun getNewList(): List<T> {
return newItemsList
}
fun getOldList(): List<T> {
return oldItemsList
}
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return areItemsTheSame(oldItemsList[oldItemPosition], newItemsList[newItemPosition])
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return areContentsTheSame(oldItemsList[oldItemPosition], newItemsList[newItemPosition])
}
abstract fun areItemsTheSame(oldItem: T, newItem: T): Boolean
abstract fun areContentsTheSame(oldItem: T, newItem: T): Boolean
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment