Add button to reorder categories alphabetically (#9369)

Closes #6459

Co-authored-by: arkon <arkon@users.noreply.github.com>
(cherry picked from commit 77ebc362f6d6b765ed2de6d1be18a969df4ca1f3)
This commit is contained in:
Pauline
2023-10-09 00:55:15 +02:00
committed by Jobobby04
parent 5e2700d420
commit 9f325ea106
6 changed files with 106 additions and 3 deletions
@@ -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