Use actual indexes instead of existing order number when reordering categories

Fixes #8738
This commit is contained in:
arkon 2022-12-15 23:06:05 -05:00
parent bc6a12a4f7
commit 5f4825465e

View File

@ -28,16 +28,17 @@ class ReorderCategory(
.filterNot(Category::isSystemCategory) .filterNot(Category::isSystemCategory)
.toMutableList() .toMutableList()
val newPosition = when (moveTo) {
MoveTo.UP -> category.order - 1
MoveTo.DOWN -> category.order + 1
}.toInt()
val currentIndex = categories.indexOfFirst { it.id == category.id } val currentIndex = categories.indexOfFirst { it.id == category.id }
if (currentIndex == newPosition) { if (currentIndex == -1) {
return@withNonCancellableContext Result.Unchanged return@withNonCancellableContext Result.Unchanged
} }
val newPosition = when (moveTo) {
MoveTo.UP -> currentIndex - 1
MoveTo.DOWN -> currentIndex + 1
}.toInt()
try {
Collections.swap(categories, currentIndex, newPosition) Collections.swap(categories, currentIndex, newPosition)
val updates = categories.mapIndexed { index, category -> val updates = categories.mapIndexed { index, category ->
@ -47,7 +48,6 @@ class ReorderCategory(
) )
} }
try {
categoryRepository.updatePartial(updates) categoryRepository.updatePartial(updates)
Result.Success Result.Success
} catch (e: Exception) { } catch (e: Exception) {