From 5f4825465e5764ddf9742eb71a833274ef9bfcc7 Mon Sep 17 00:00:00 2001 From: arkon Date: Thu, 15 Dec 2022 23:06:05 -0500 Subject: [PATCH] Use actual indexes instead of existing order number when reordering categories Fixes #8738 --- .../category/interactor/ReorderCategory.kt | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/eu/kanade/domain/category/interactor/ReorderCategory.kt b/app/src/main/java/eu/kanade/domain/category/interactor/ReorderCategory.kt index 11b4318b96..3687e4880b 100644 --- a/app/src/main/java/eu/kanade/domain/category/interactor/ReorderCategory.kt +++ b/app/src/main/java/eu/kanade/domain/category/interactor/ReorderCategory.kt @@ -28,26 +28,26 @@ class ReorderCategory( .filterNot(Category::isSystemCategory) .toMutableList() - val newPosition = when (moveTo) { - MoveTo.UP -> category.order - 1 - MoveTo.DOWN -> category.order + 1 - }.toInt() - val currentIndex = categories.indexOfFirst { it.id == category.id } - if (currentIndex == newPosition) { + if (currentIndex == -1) { return@withNonCancellableContext Result.Unchanged } - Collections.swap(categories, currentIndex, newPosition) - - val updates = categories.mapIndexed { index, category -> - CategoryUpdate( - id = category.id, - order = index.toLong(), - ) - } + val newPosition = when (moveTo) { + MoveTo.UP -> currentIndex - 1 + MoveTo.DOWN -> currentIndex + 1 + }.toInt() try { + Collections.swap(categories, currentIndex, newPosition) + + val updates = categories.mapIndexed { index, category -> + CategoryUpdate( + id = category.id, + order = index.toLong(), + ) + } + categoryRepository.updatePartial(updates) Result.Success } catch (e: Exception) {