Created
October 10, 2018 13:38
-
-
Save GeoffreyMetais/7c97389fed6b1674e4113d10bc656b92 to your computer and use it in GitHub Desktop.
Example adapter implementation with DiffUtil
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
abstract class DiffUtilAdapter<D, VH : RecyclerView.ViewHolder> : RecyclerView.Adapter<VH>(), CoroutineScope { | |
override val coroutineContext = Dispatchers.Main.immediate | |
protected var dataset: List<D> = listOf() | |
private set | |
private val diffCallback by lazy(LazyThreadSafetyMode.NONE) { DiffCallback<D>() } | |
protected val detectMoves = true | |
private val updateActor = actor<List<D>>(capacity = Channel.CONFLATED) { | |
for (list in channel) internalUpdate(list) | |
} | |
protected abstract fun onUpdateFinished() | |
@MainThread | |
fun update (list: List<D>) { | |
updateActor.offer(list) | |
} | |
@MainThread | |
private suspend fun internalUpdate(list: List<D>) { | |
val (finalList, result) = withContext(Dispatchers.Default) { | |
val finalList = list.toList() | |
val result = DiffUtil.calculateDiff(diffCallback.apply { update(dataset, finalList) }, detectMoves) | |
Pair(finalList, result) | |
} | |
dataset = finalList | |
result.dispatchUpdatesTo(this@DiffUtilAdapter) | |
onUpdateFinished() | |
} | |
@MainThread | |
override fun getItemCount() = dataset.size | |
@MainThread | |
fun isEmpty() = dataset.isEmpty() | |
@MainThread | |
open fun getItem(position: Int) = dataset[position] | |
class DiffCallback<D> : DiffUtil.Callback() { | |
lateinit var oldList: List<D> | |
lateinit var newList: List<D> | |
fun update(oldList: List<D>, newList: List<D>) { | |
this.oldList = oldList | |
this.newList = newList | |
} | |
override fun getOldListSize() = oldList.size | |
override fun getNewListSize() = newList.size | |
override fun areContentsTheSame(oldItemPosition : Int, newItemPosition : Int) = true | |
override fun areItemsTheSame(oldItemPosition : Int, newItemPosition : Int) = oldList[oldItemPosition] == newList[newItemPosition] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For simplicity as wrapper for DiffUtil adapter class use ListAdapter
But, if you wish for more control over adapter behavior, or to provide a specific base class should refer to AsyncListDiffer as stated in the official docs
Or use Paging library instead