mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-20 15:47: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 {
|
init {
|
||||||
dataState.onEach {
|
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
|
data = it
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
diff.removed.forEach {
|
diffAddsAndReplaces.replaced.forEach { (from, to) ->
|
||||||
notifyItemRemoved(it.index)
|
|
||||||
}
|
|
||||||
diff.replaced.forEach { (from, to) ->
|
|
||||||
notifyItemMoved(from.index, to.index)
|
notifyItemMoved(from.index, to.index)
|
||||||
}
|
}
|
||||||
diff.added.forEach {
|
diffAddsAndReplaces.added.forEach {
|
||||||
notifyItemInserted(it.index)
|
notifyItemInserted(it.index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user