diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index 806202008b..2064d3d90b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -119,6 +119,7 @@ class LibraryController( * Position of the active category. */ private var activeCategory: Int = preferences.lastUsedCategory().getOrDefault() + private var lastUsedCategory: Int = preferences.lastUsedCategory().getOrDefault() private var justStarted = true @@ -215,6 +216,11 @@ class LibraryController( showCategoryText(currentCategory.name) } } + val savedCurrentCategory = getHeader(true)?.category ?: return + if (savedCurrentCategory.order != lastUsedCategory) { + lastUsedCategory = savedCurrentCategory.order + preferences.lastUsedCategory().set(savedCurrentCategory.order) + } } override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { @@ -240,7 +246,6 @@ class LibraryController( } fun saveActiveCategory(category: Category) { - preferences.lastUsedCategory().set(category.order) activeCategory = category.order val headerItem = getHeader() ?: return header_title.text = headerItem.category.name @@ -515,12 +520,16 @@ class LibraryController( private fun jumpToNextCategory(next: Boolean) { val category = getVisibleHeader() ?: return if (presenter.showAllCategories) { + if (!next) { + val fPosition = + (recycler.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() + if (fPosition != adapter.currentItems.indexOf(category)) { + scrollToHeader(category.category.order) + return + } + } val newOffset = adapter.headerItems.indexOf(category) + (if (next) 1 else -1) - if (if (!next) { - newOffset > -1 - } else { - newOffset < adapter.headerItems.size - }) { + if (if (!next) newOffset > -1 else newOffset < adapter.headerItems.size) { val newCategory = (adapter.headerItems[newOffset] as LibraryHeaderItem).category val newOrder = newCategory.order scrollToHeader(newOrder) @@ -546,9 +555,12 @@ class LibraryController( } } - private fun getHeader(): LibraryHeaderItem? { - val position = + private fun getHeader(firstCompletelyVisible: Boolean = false): LibraryHeaderItem? { + val position = if (firstCompletelyVisible) { (recycler.layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition() + } else { + -1 + } if (position > 0) { when (val item = adapter.getItem(position)) { is LibraryHeaderItem -> return item @@ -566,12 +578,6 @@ class LibraryController( } private fun getVisibleHeader(): LibraryHeaderItem? { - val position = - (recycler.layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition() - when (val item = adapter.getItem(position)) { - is LibraryHeaderItem -> return item - is LibraryItem -> return item.header - } val fPosition = (recycler.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() when (val item = adapter.getItem(fPosition)) {