Fixed crashes when removing draggable item

This commit is contained in:
Sad Ellie 2022-08-17 21:16:08 +03:00
parent a3a5d9ec27
commit 92ac089b2b

View File

@ -96,9 +96,16 @@ class UnitGroupsRepository @Inject constructor() {
suspend fun moveShownUnitGroups(from: ItemPosition, to: ItemPosition) { suspend fun moveShownUnitGroups(from: ItemPosition, to: ItemPosition) {
mutex.withLock { mutex.withLock {
shownUnitGroups.value = shownUnitGroups.value.toMutableList().apply { shownUnitGroups.value = shownUnitGroups.value.toMutableList().apply {
val initialIndex = shownUnitGroups.value.indexOfFirst { it == from.key }
/**
* No such item. Happens when dragging item and clicking "remove" while item is
* still being dragged.
*/
if (initialIndex == -1) return
add( add(
shownUnitGroups.value.indexOfFirst { it == to.key }, shownUnitGroups.value.indexOfFirst { it == to.key },
removeAt(shownUnitGroups.value.indexOfFirst { it == from.key }) removeAt(initialIndex)
) )
} }
} }