Library presenter now fixes category order when 2 categories have the same order

It's unlikely the case and nothing awful happens with it, but it fixes the jump to category sheet
This commit is contained in:
Jays2Kings 2021-04-27 02:28:59 -04:00
parent d1c709ce2c
commit dbf96f68ff

View File

@ -102,6 +102,10 @@ class LibraryPresenter(
/** Get favorited manga for library and sort and filter it */ /** Get favorited manga for library and sort and filter it */
fun getLibrary() { fun getLibrary() {
if (categories.isEmpty()) { if (categories.isEmpty()) {
val dbCategories = db.getCategories().executeAsBlocking()
if ((dbCategories + Category.createDefault(context)).distinctBy { it.order }.size != dbCategories.size + 1) {
reorderCategories(dbCategories)
}
categories = lastCategories ?: db.getCategories().executeAsBlocking().toMutableList() categories = lastCategories ?: db.getCategories().executeAsBlocking().toMutableList()
} }
presenterScope.launch { presenterScope.launch {
@ -119,6 +123,12 @@ class LibraryPresenter(
} }
} }
private fun reorderCategories(categories: List<Category>) {
val sortedCategories = categories.sortedBy { it.order }
sortedCategories.forEachIndexed { i, category -> category.order = i }
db.insertCategories(sortedCategories).executeAsBlocking()
}
fun getCurrentCategory() = categories.find { it.id == currentCategory } fun getCurrentCategory() = categories.find { it.id == currentCategory }
fun switchSection(order: Int) { fun switchSection(order: Int) {