Skip to content

Commit

Permalink
fix: check order before restore from backup (#1156)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cologler authored Apr 15, 2024
1 parent ed20d25 commit 03f32eb
Showing 1 changed file with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ 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)
categoriesQueries.selectLastInsertedRowId()
}.let { id -> it.toCategory(id) }
}
val categories = backupCategories
.sortedBy { it.order }
.distinctBy { it.name }
.map {
val newOrder = nextOrder++
dbCategoriesByName[it.name]
?: handler.awaitOneExecutable {
categoriesQueries.insert(it.name, newOrder, it.flags)
categoriesQueries.selectLastInsertedRowId()
}.let { id -> it.toCategory(id).copy(order = newOrder) }
}

libraryPreferences.categorizedDisplaySettings().set(
(dbCategories + categories)
Expand Down

0 comments on commit 03f32eb

Please sign in to comment.