Assign category positions inside the database

A new category's position is computed by its insert, one after the highest, so two categories created at once can't take the same one. Inserts take a NewCategory rather than a Category whose id and position would be ignored. Reordering places any category missing from the given order after the rest instead of leaving it at a stale position. Restore still creates missing categories all or none, in backup order, and the create dialog no longer accepts an empty name.
This commit is contained in:
AntsyLich
2026-09-27 22:53:09 +06:00
parent fc5592ff75
commit 8bfa495f34
8 changed files with 42 additions and 46 deletions
@@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.Flow
import tachiyomi.data.Database
import tachiyomi.data.subscribeToList
import tachiyomi.domain.category.model.Category
import tachiyomi.domain.category.model.NewCategory
import tachiyomi.domain.category.repository.CategoryRepository
@Inject
@@ -49,15 +50,11 @@ class CategoryRepositoryImpl(
.subscribeToList()
}
override suspend fun insert(category: Category) {
database.categoriesQueries.insert(
name = category.name,
order = category.order,
flags = category.flags,
)
override suspend fun insert(category: NewCategory) {
database.categoriesQueries.insert(name = category.name, flags = category.flags)
}
override suspend fun insertAll(categories: List<Category>) {
override suspend fun insertAll(categories: List<NewCategory>) {
database.transaction {
categories.forEach { insert(it) }
}
@@ -77,10 +74,12 @@ class CategoryRepositoryImpl(
override suspend fun updateAllOrders(orderedIds: List<Long>) {
database.transaction {
orderedIds.forEachIndexed { index, categoryId ->
val current = database.categoriesQueries.getUserCategoryIds().awaitAsList()
val ids = orderedIds.filter { it in current } + current.filterNot { it in orderedIds }
ids.forEachIndexed { index, categoryId ->
database.categoriesQueries.updateOrder(order = -index - 2L, categoryId = categoryId)
}
orderedIds.forEachIndexed { index, categoryId ->
ids.forEachIndexed { index, categoryId ->
database.categoriesQueries.updateOrder(order = index.toLong(), categoryId = categoryId)
}
}
@@ -44,7 +44,8 @@ WHERE MC.manga_id = :mangaId;
insert:
INSERT INTO categories(name, sort, flags)
VALUES (:name, :order, :flags);
SELECT :name, coalesce(max(sort), -1) + 1, :flags
FROM categories;
delete:
DELETE FROM categories
@@ -55,6 +56,12 @@ UPDATE categories
SET name = :name
WHERE _id = :categoryId;
getUserCategoryIds:
SELECT _id
FROM categories
WHERE _id > 0
ORDER BY sort;
updateOrder:
UPDATE categories
SET sort = :order