From 6d999be590497413dd0ca212ece74f75155f6296 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 17 Jun 2021 13:45:19 +0600 Subject: [PATCH] small improvement in StateFlowBasedRecyclerViewAdapter --- .../StateFlowBasedRecyclerViewAdapter.kt | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/android/recyclerview/src/main/kotlin/dev/inmo/micro_utils/android/recyclerview/StateFlowBasedRecyclerViewAdapter.kt b/android/recyclerview/src/main/kotlin/dev/inmo/micro_utils/android/recyclerview/StateFlowBasedRecyclerViewAdapter.kt index d13141c006d..1d2ec68ed90 100644 --- a/android/recyclerview/src/main/kotlin/dev/inmo/micro_utils/android/recyclerview/StateFlowBasedRecyclerViewAdapter.kt +++ b/android/recyclerview/src/main/kotlin/dev/inmo/micro_utils/android/recyclerview/StateFlowBasedRecyclerViewAdapter.kt @@ -15,16 +15,29 @@ abstract class StateFlowBasedRecyclerViewAdapter( 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) } }