Add button to reorder categories alphabetically (#9369)

Closes #6459

Co-authored-by: arkon <arkon@users.noreply.github.com>
This commit is contained in:
Pauline
2023-10-09 00:55:15 +02:00
committed by GitHub
parent 8568d5d6c3
commit 77ebc362f6
6 changed files with 106 additions and 3 deletions

View File

@@ -55,6 +55,27 @@ class ReorderCategory(
}
}
suspend fun sortAlphabetically() = withNonCancellableContext {
mutex.withLock {
val updates = categoryRepository.getAll()
.sortedBy { category -> category.name }
.mapIndexed { index, category ->
CategoryUpdate(
id = category.id,
order = index.toLong(),
)
}
try {
categoryRepository.updatePartial(updates)
Result.Success
} catch (e: Exception) {
logcat(LogPriority.ERROR, e)
Result.InternalError(e)
}
}
}
sealed interface Result {
data object Success : Result
data object Unchanged : Result