mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-20 07:37:15 +00:00
small improvement in StateFlowBasedRecyclerViewAdapter
This commit is contained in:
parent
e715772dbf
commit
6d999be590
@ -15,16 +15,29 @@ abstract class StateFlowBasedRecyclerViewAdapter<T>(
|
||||
|
||||
init {
|
||||
dataState.onEach {
|
||||
val diff = Diff(data, it)
|
||||
val diffForRemoves = Diff(data, it)
|
||||
val removedIndexes = diffForRemoves.removed.map { it.index }
|
||||
val leftRemove = removedIndexes.toMutableList()
|
||||
data = data.filterIndexed { i, _ ->
|
||||
if (i in leftRemove) {
|
||||
leftRemove.remove(i)
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
withContext(Dispatchers.Main) {
|
||||
removedIndexes.sortedDescending().forEach {
|
||||
notifyItemRemoved(it)
|
||||
}
|
||||
}
|
||||
val diffAddsAndReplaces = Diff(data, it)
|
||||
data = it
|
||||
withContext(Dispatchers.Main) {
|
||||
diff.removed.forEach {
|
||||
notifyItemRemoved(it.index)
|
||||
}
|
||||
diff.replaced.forEach { (from, to) ->
|
||||
diffAddsAndReplaces.replaced.forEach { (from, to) ->
|
||||
notifyItemMoved(from.index, to.index)
|
||||
}
|
||||
diff.added.forEach {
|
||||
diffAddsAndReplaces.added.forEach {
|
||||
notifyItemInserted(it.index)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user