Skip to content

Commit

Permalink
Check category order before restoring from backup
Browse files Browse the repository at this point in the history
Closes #632

Co-authored-by: Cologler <10906962+Cologler@users.noreply.github.com>
  • Loading branch information
AntsyLich and Cologler committed Jun 8, 2024
1 parent 87fe644 commit 119bcbf
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 119bcbf

Please sign in to comment.