Last active
January 23, 2020 20:16
-
-
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.
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
/** | |
* 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); | |
} |
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
/** | |
* 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