Keep category orders unique while reordering

Reordering first moves every category to a temporary negative position, then assigns the final ones, so two categories never share a position mid-update. Deleting a category no longer renumbers the system category.
This commit is contained in:
AntsyLich
2026-09-27 22:37:39 +06:00
parent 986f46c09a
commit 4df0e6b168
2 changed files with 7 additions and 1 deletions
@@ -77,6 +77,9 @@ class CategoryRepositoryImpl(
override suspend fun updateAllOrders(orderedIds: List<Long>) { override suspend fun updateAllOrders(orderedIds: List<Long>) {
database.transaction { database.transaction {
orderedIds.forEachIndexed { index, categoryId ->
database.categoriesQueries.updateOrder(order = -index - 2L, categoryId = categoryId)
}
orderedIds.forEachIndexed { index, categoryId -> orderedIds.forEachIndexed { index, categoryId ->
database.categoriesQueries.updateOrder(order = index.toLong(), categoryId = categoryId) database.categoriesQueries.updateOrder(order = index.toLong(), categoryId = categoryId)
} }
@@ -4,6 +4,7 @@ import dev.zacsweers.metro.Inject
import logcat.LogPriority import logcat.LogPriority
import tachiyomi.core.common.util.lang.withNonCancellableContext import tachiyomi.core.common.util.lang.withNonCancellableContext
import tachiyomi.core.common.util.system.logcat import tachiyomi.core.common.util.system.logcat
import tachiyomi.domain.category.model.Category
import tachiyomi.domain.category.repository.CategoryRepository import tachiyomi.domain.category.repository.CategoryRepository
import tachiyomi.domain.download.service.DownloadPreferences import tachiyomi.domain.download.service.DownloadPreferences
import tachiyomi.domain.library.service.LibraryPreferences import tachiyomi.domain.library.service.LibraryPreferences
@@ -23,7 +24,9 @@ class DeleteCategory(
return@withNonCancellableContext Result.InternalError(e) return@withNonCancellableContext Result.InternalError(e)
} }
val orderedIds = categoryRepository.getAll().map { it.id } val orderedIds = categoryRepository.getAll()
.filterNot(Category::isSystemCategory)
.map { it.id }
val defaultCategory = libraryPreferences.defaultCategory.get() val defaultCategory = libraryPreferences.defaultCategory.get()
if (defaultCategory == categoryId.toInt()) { if (defaultCategory == categoryId.toInt()) {