-
-
Save programmerr47/e89e1e2fcc4d14dcae9f10ca2c8311f3 to your computer and use it in GitHub Desktop.
The ability to choose between async and sync diff calculations
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
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