mirror of
https://github.com/mihonapp/mihon.git
synced 2026-10-16 13:48:09 -07:00
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:
+2
-11
@@ -4,7 +4,7 @@ import dev.zacsweers.metro.Inject
|
||||
import logcat.LogPriority
|
||||
import tachiyomi.core.common.util.lang.withNonCancellableContext
|
||||
import tachiyomi.core.common.util.system.logcat
|
||||
import tachiyomi.domain.category.model.Category
|
||||
import tachiyomi.domain.category.model.NewCategory
|
||||
import tachiyomi.domain.category.repository.CategoryRepository
|
||||
import tachiyomi.domain.library.service.LibraryPreferences
|
||||
|
||||
@@ -21,17 +21,8 @@ class CreateCategoryWithName(
|
||||
}
|
||||
|
||||
suspend fun await(name: String): Result = withNonCancellableContext {
|
||||
val categories = categoryRepository.getAll()
|
||||
val nextOrder = categories.maxOfOrNull { it.order }?.plus(1) ?: 0
|
||||
val newCategory = Category(
|
||||
id = 0,
|
||||
name = name,
|
||||
order = nextOrder,
|
||||
flags = initialFlags,
|
||||
)
|
||||
|
||||
try {
|
||||
categoryRepository.insert(newCategory)
|
||||
categoryRepository.insert(NewCategory(name = name, flags = initialFlags))
|
||||
Result.Success
|
||||
} catch (e: Exception) {
|
||||
logcat(LogPriority.ERROR, e)
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package tachiyomi.domain.category.model
|
||||
|
||||
data class NewCategory(
|
||||
val name: String,
|
||||
val flags: Long,
|
||||
)
|
||||
@@ -2,6 +2,7 @@ package tachiyomi.domain.category.repository
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import tachiyomi.domain.category.model.Category
|
||||
import tachiyomi.domain.category.model.NewCategory
|
||||
|
||||
interface CategoryRepository {
|
||||
|
||||
@@ -15,9 +16,9 @@ interface CategoryRepository {
|
||||
|
||||
fun getCategoriesByMangaIdAsFlow(mangaId: Long): Flow<List<Category>>
|
||||
|
||||
suspend fun insert(category: Category)
|
||||
suspend fun insert(category: NewCategory)
|
||||
|
||||
suspend fun insertAll(categories: List<Category>)
|
||||
suspend fun insertAll(categories: List<NewCategory>)
|
||||
|
||||
suspend fun updateName(categoryId: Long, name: String)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user