Add back support for drag-and-drop category reordering (#1427)

This commit is contained in:
Cuong-Tran
2025-02-26 12:37:10 +07:00
committed by GitHub
parent d91c7b6093
commit 919607cd06
8 changed files with 70 additions and 89 deletions

View File

@ -8,7 +8,6 @@ import tachiyomi.core.common.util.system.logcat
import tachiyomi.domain.category.model.Category
import tachiyomi.domain.category.model.CategoryUpdate
import tachiyomi.domain.category.repository.CategoryRepository
import java.util.Collections
class ReorderCategory(
private val categoryRepository: CategoryRepository,
@ -16,11 +15,7 @@ class ReorderCategory(
private val mutex = Mutex()
suspend fun moveUp(category: Category): Result = await(category, MoveTo.UP)
suspend fun moveDown(category: Category): Result = await(category, MoveTo.DOWN)
private suspend fun await(category: Category, moveTo: MoveTo) = withNonCancellableContext {
suspend fun changeOrder(category: Category, newIndex: Int) = withNonCancellableContext {
mutex.withLock {
val categories = categoryRepository.getAll()
.filterNot(Category::isSystemCategory)
@ -31,13 +26,8 @@ class ReorderCategory(
return@withNonCancellableContext Result.Unchanged
}
val newPosition = when (moveTo) {
MoveTo.UP -> currentIndex - 1
MoveTo.DOWN -> currentIndex + 1
}.toInt()
try {
Collections.swap(categories, currentIndex, newPosition)
categories.add(newIndex, categories.removeAt(currentIndex))
val updates = categories.mapIndexed { index, category ->
CategoryUpdate(
@ -81,9 +71,4 @@ class ReorderCategory(
data object Unchanged : Result
data class InternalError(val error: Throwable) : Result
}
private enum class MoveTo {
UP,
DOWN,
}
}