Check category order before restoring from backup

Closes #632

Co-authored-by: Cologler <10906962+Cologler@users.noreply.github.com>
This commit is contained in:
AntsyLich 2024-06-08 06:38:35 +06:00
parent 87fe64468c
commit 119bcbf8ed
No known key found for this signature in database

View File

@ -17,14 +17,20 @@ class CategoriesRestorer(
if (backupCategories.isNotEmpty()) {
val dbCategories = getCategories.await()
val dbCategoriesByName = dbCategories.associateBy { it.name }
var nextOrder = dbCategories.maxOfOrNull { it.order }?.plus(1) ?: 0
val categories = backupCategories.map {
dbCategoriesByName[it.name]
?: handler.awaitOneExecutable {
categoriesQueries.insert(it.name, it.order, it.flags)
val categories = backupCategories
.sortedBy { it.order }
.map {
val dbCategory = dbCategoriesByName[it.name]
if (dbCategory != null) return@map dbCategory
val order = nextOrder++
handler.awaitOneExecutable {
categoriesQueries.insert(it.name, order, it.flags)
categoriesQueries.selectLastInsertedRowId()
}.let { id -> it.toCategory(id) }
}
}
.let { id -> it.toCategory(id).copy(order = order) }
}
libraryPreferences.categorizedDisplaySettings().set(
(dbCategories + categories)