Library fast scroll now snaps to categories

This commit is contained in:
Jays2Kings 2022-07-12 19:00:35 -04:00
parent 9a08175280
commit 8094345714
4 changed files with 36 additions and 2 deletions

View file

@ -15,7 +15,8 @@ import eu.kanade.tachiyomi.util.system.dpToPxEnd
import eu.kanade.tachiyomi.util.system.isLTR
import kotlin.math.abs
class MaterialFastScroll @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
@Suppress("LeakingThis")
open class MaterialFastScroll @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
FastScroller(context, attrs) {
var canScroll = false

View file

@ -1269,6 +1269,12 @@ class LibraryController(
}
}
fun scrollToCategory(category: Category?) {
if (category != null && activeCategory != category.order) {
scrollToHeader(category.order)
}
}
private fun scrollToHeader(pos: Int, removeObserver: Boolean = true) {
if (removeObserver) {
removeStaggeredObserver()

View file

@ -0,0 +1,27 @@
package eu.kanade.tachiyomi.ui.library
import android.content.Context
import android.util.AttributeSet
import eu.kanade.tachiyomi.ui.base.MaterialFastScroll
class LibraryFastScroll @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
MaterialFastScroll(context, attrs) {
override fun setRecyclerViewPosition(y: Float) {
if (recyclerView != null) {
val adapter = recyclerView.adapter as? LibraryCategoryAdapter ?: return super.setRecyclerViewPosition(y)
if (adapter.isSingleCategory) return super.setRecyclerViewPosition(y)
var targetPos = getTargetPos(y)
val category = when (val item = adapter.getItem(targetPos)) {
is LibraryItem -> {
targetPos = adapter.currentItems.indexOf(item.header)
item.header.category
}
is LibraryHeaderItem -> item.category
else -> return super.setRecyclerViewPosition(y)
}
adapter.controller?.scrollToCategory(category)
updateBubbleText(targetPos)
}
}
}

View file

@ -121,7 +121,7 @@
</FrameLayout>
<eu.kanade.tachiyomi.ui.base.MaterialFastScroll
<eu.kanade.tachiyomi.ui.library.LibraryFastScroll
android:id="@+id/fast_scroller"
android:layout_width="match_parent"
android:layout_height="match_parent"