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

(cherry picked from commit 919607cd06ee45ac667a2fd650d85aaf6ebb9762)

# Conflicts:
#	CHANGELOG.md
#	app/src/main/java/eu/kanade/presentation/category/CategoryScreen.kt
This commit is contained in:
Cuong-Tran
2025-02-26 12:37:10 +07:00
committed by Jobobby04
parent 0e2866260f
commit 1d10925829
7 changed files with 69 additions and 88 deletions
@@ -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,
}
}