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()) { if (backupCategories.isNotEmpty()) {
val dbCategories = getCategories.await() val dbCategories = getCategories.await()
val dbCategoriesByName = dbCategories.associateBy { it.name } val dbCategoriesByName = dbCategories.associateBy { it.name }
var nextOrder = dbCategories.maxOfOrNull { it.order }?.plus(1) ?: 0
val categories = backupCategories.map { val categories = backupCategories
dbCategoriesByName[it.name] .sortedBy { it.order }
?: handler.awaitOneExecutable { .map {
categoriesQueries.insert(it.name, it.order, it.flags) 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() categoriesQueries.selectLastInsertedRowId()
}.let { id -> it.toCategory(id) } }
} .let { id -> it.toCategory(id).copy(order = order) }
}
libraryPreferences.categorizedDisplaySettings().set( libraryPreferences.categorizedDisplaySettings().set(
(dbCategories + categories) (dbCategories + categories)