From 1c86f3f4bf167f95dace29bea5c23b0eb463d1ef Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 17 Jun 2021 13:46:22 +0600 Subject: [PATCH] wrap with trycatch StateFlowBasedRecyclerViewAdapter listener --- .../StateFlowBasedRecyclerViewAdapter.kt | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 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 1d2ec68ed90..e7fa984741b 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,31 +15,35 @@ abstract class StateFlowBasedRecyclerViewAdapter( init { dataState.onEach { - 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 + try { + 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) + withContext(Dispatchers.Main) { + removedIndexes.sortedDescending().forEach { + notifyItemRemoved(it) + } } - } - val diffAddsAndReplaces = Diff(data, it) - data = it - withContext(Dispatchers.Main) { - diffAddsAndReplaces.replaced.forEach { (from, to) -> - notifyItemMoved(from.index, to.index) - } - diffAddsAndReplaces.added.forEach { - notifyItemInserted(it.index) + val diffAddsAndReplaces = Diff(data, it) + data = it + withContext(Dispatchers.Main) { + diffAddsAndReplaces.replaced.forEach { (from, to) -> + notifyItemMoved(from.index, to.index) + } + diffAddsAndReplaces.added.forEach { + notifyItemInserted(it.index) + } } + } catch (e: Throwable) { + // currently do nothing } }.launchIn(listeningScope) }