Skip to content

Instantly share code, notes, and snippets.

@programmerr47
Created December 29, 2019 11:22
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 programmerr47/e89e1e2fcc4d14dcae9f10ca2c8311f3 to your computer and use it in GitHub Desktop.
Save programmerr47/e89e1e2fcc4d14dcae9f10ca2c8311f3 to your computer and use it in GitHub Desktop.
The ability to choose between async and sync diff calculations
interface ListDiffer<T> {
val currentList: List<T>
fun submitList(list: List<T>)
}
class SyncListDiffer<T : Any>(
private val adapter: RecyclerView.Adapter<*>,
private val callbackFactory: DiffCallbackFactory<T> = { old, new -> SimpleDiffCallback(old, new) }
) : ListDiffer<T> {
override var currentList: List<T> = emptyList()
private set
override fun submitList(list: List<T>) {
val diffResult = DiffUtil.calculateDiff(callbackFactory(currentList, list))
currentList = list
diffResult.dispatchUpdatesTo(adapter)
}
}
class AsyncListDifferAdapter<T>(private val origin: AsyncListDiffer<T>) : ListDiffer<T> {
override val currentList: List<T> get() = origin.currentList
override fun submitList(list: List<T>) = origin.submitList(list)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment