wrap with trycatch StateFlowBasedRecyclerViewAdapter listener

This commit is contained in:
InsanusMokrassar 2021-06-17 13:46:22 +06:00
parent 6d999be590
commit 1c86f3f4bf

View File

@ -15,31 +15,35 @@ abstract class StateFlowBasedRecyclerViewAdapter<T>(
init { init {
dataState.onEach { dataState.onEach {
val diffForRemoves = Diff(data, it) try {
val removedIndexes = diffForRemoves.removed.map { it.index } val diffForRemoves = Diff(data, it)
val leftRemove = removedIndexes.toMutableList() val removedIndexes = diffForRemoves.removed.map { it.index }
data = data.filterIndexed { i, _ -> val leftRemove = removedIndexes.toMutableList()
if (i in leftRemove) { data = data.filterIndexed { i, _ ->
leftRemove.remove(i) if (i in leftRemove) {
true leftRemove.remove(i)
} else { true
false } else {
false
}
} }
} withContext(Dispatchers.Main) {
withContext(Dispatchers.Main) { removedIndexes.sortedDescending().forEach {
removedIndexes.sortedDescending().forEach { notifyItemRemoved(it)
notifyItemRemoved(it) }
} }
} val diffAddsAndReplaces = Diff(data, it)
val diffAddsAndReplaces = Diff(data, it) data = it
data = it withContext(Dispatchers.Main) {
withContext(Dispatchers.Main) { diffAddsAndReplaces.replaced.forEach { (from, to) ->
diffAddsAndReplaces.replaced.forEach { (from, to) -> notifyItemMoved(from.index, to.index)
notifyItemMoved(from.index, to.index) }
} diffAddsAndReplaces.added.forEach {
diffAddsAndReplaces.added.forEach { notifyItemInserted(it.index)
notifyItemInserted(it.index) }
} }
} catch (e: Throwable) {
// currently do nothing
} }
}.launchIn(listeningScope) }.launchIn(listeningScope)
} }