Last active
December 9, 2020 09:20
-
-
Save osipxd/db9e394d6ac13ac49a7264bf2bd73f76 to your computer and use it in GitHub Desktop.
The snippet to show when update callback is called.
This file contains hidden or 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
import androidx.paging.AsyncPagingDataDiffer | |
import androidx.paging.PagingData | |
import androidx.recyclerview.widget.DiffUtil | |
import androidx.recyclerview.widget.ListUpdateCallback | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.runBlocking | |
val differ: AsyncPagingDataDiffer<String> by lazy { | |
AsyncPagingDataDiffer( | |
diffCallback = diffCallback, | |
updateCallback = updateCallback, | |
mainDispatcher = Dispatchers.Unconfined, | |
workerDispatcher = Dispatchers.Unconfined, | |
) | |
} | |
val diffCallback = object : DiffUtil.ItemCallback<String>() { | |
override fun areItemsTheSame(oldItem: String, newItem: String) = oldItem.first() == newItem.first() | |
override fun areContentsTheSame(oldItem: String, newItem: String) = oldItem == newItem | |
} | |
val updateCallback = object : ListUpdateCallback { | |
override fun onInserted(position: Int, count: Int) { | |
println("onInserted: ${differ.snapshot()}") | |
} | |
override fun onRemoved(position: Int, count: Int) { | |
println("onRemoved: ${differ.snapshot()}") | |
} | |
override fun onMoved(fromPosition: Int, toPosition: Int) { | |
println("onMoved: ${differ.snapshot()}") | |
} | |
override fun onChanged(position: Int, count: Int, payload: Any?) { | |
println("onChanged: ${differ.snapshot()}") | |
} | |
} | |
differ.addLoadStateListener { | |
println("loadStateListener: ${differ.snapshot()}") | |
} | |
runBlocking { | |
println() | |
differ.submitData(PagingData.from(listOf("A", "B", "C"))) | |
println("after onInserted: ${differ.snapshot()}") | |
println() | |
differ.submitData(PagingData.from(listOf("A", "C"))) | |
println("after onRemoved: ${differ.snapshot()}") | |
println() | |
differ.submitData(PagingData.from(listOf("C", "A"))) | |
println("after onMoved: ${differ.snapshot()}") | |
println() | |
differ.submitData(PagingData.from(listOf("Changed", "A"))) | |
println("after onChanged: ${differ.snapshot()}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output:
Expected that snapshot in the callback and after callback will be the same. LoadStateListener not always triggered on data change.