Current category logic update + hopper up button update

Current category header now works by the first visual item in the view, not the first completely visible item anymore. Saving current item pref still relies on first completely visible

Hopper up button now goes to the top of the current category before going to the next category
This commit is contained in:
Jay 2020-05-20 20:43:21 -04:00
parent 97367f0558
commit f78daf7156

View file

@ -119,6 +119,7 @@ class LibraryController(
* Position of the active category. * Position of the active category.
*/ */
private var activeCategory: Int = preferences.lastUsedCategory().getOrDefault() private var activeCategory: Int = preferences.lastUsedCategory().getOrDefault()
private var lastUsedCategory: Int = preferences.lastUsedCategory().getOrDefault()
private var justStarted = true private var justStarted = true
@ -215,6 +216,11 @@ class LibraryController(
showCategoryText(currentCategory.name) 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) { override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
@ -240,7 +246,6 @@ class LibraryController(
} }
fun saveActiveCategory(category: Category) { fun saveActiveCategory(category: Category) {
preferences.lastUsedCategory().set(category.order)
activeCategory = category.order activeCategory = category.order
val headerItem = getHeader() ?: return val headerItem = getHeader() ?: return
header_title.text = headerItem.category.name header_title.text = headerItem.category.name
@ -515,12 +520,16 @@ class LibraryController(
private fun jumpToNextCategory(next: Boolean) { private fun jumpToNextCategory(next: Boolean) {
val category = getVisibleHeader() ?: return val category = getVisibleHeader() ?: return
if (presenter.showAllCategories) { 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) val newOffset = adapter.headerItems.indexOf(category) + (if (next) 1 else -1)
if (if (!next) { if (if (!next) newOffset > -1 else newOffset < adapter.headerItems.size) {
newOffset > -1
} else {
newOffset < adapter.headerItems.size
}) {
val newCategory = (adapter.headerItems[newOffset] as LibraryHeaderItem).category val newCategory = (adapter.headerItems[newOffset] as LibraryHeaderItem).category
val newOrder = newCategory.order val newOrder = newCategory.order
scrollToHeader(newOrder) scrollToHeader(newOrder)
@ -546,9 +555,12 @@ class LibraryController(
} }
} }
private fun getHeader(): LibraryHeaderItem? { private fun getHeader(firstCompletelyVisible: Boolean = false): LibraryHeaderItem? {
val position = val position = if (firstCompletelyVisible) {
(recycler.layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition() (recycler.layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition()
} else {
-1
}
if (position > 0) { if (position > 0) {
when (val item = adapter.getItem(position)) { when (val item = adapter.getItem(position)) {
is LibraryHeaderItem -> return item is LibraryHeaderItem -> return item
@ -566,12 +578,6 @@ class LibraryController(
} }
private fun getVisibleHeader(): LibraryHeaderItem? { 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 = val fPosition =
(recycler.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() (recycler.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
when (val item = adapter.getItem(fPosition)) { when (val item = adapter.getItem(fPosition)) {