feat: Add sensitivity to isScrollingUp

This commit is contained in:
Ushie 2024-07-03 02:46:01 +03:00
parent d987ac6c7a
commit e3bd8a8b22
No known key found for this signature in database
GPG key ID: B3AAD18842E34632

View file

@ -176,6 +176,9 @@ fun String.relativeTime(context: Context): String {
}
}
const val isScrollingUpSensitivity = 10
@Composable
fun LazyListState.isScrollingUp(): State<Boolean> {
return remember(this) {
@ -183,10 +186,16 @@ fun LazyListState.isScrollingUp(): State<Boolean> {
var previousScrollOffset by mutableIntStateOf(firstVisibleItemScrollOffset)
derivedStateOf {
if (previousIndex != firstVisibleItemIndex) {
val indexChanged = previousIndex != firstVisibleItemIndex
val offsetChanged =
kotlin.math.abs(previousScrollOffset - firstVisibleItemScrollOffset) > isScrollingUpSensitivity
if (indexChanged) {
previousIndex > firstVisibleItemIndex
} else if (offsetChanged) {
previousScrollOffset > firstVisibleItemScrollOffset
} else {
previousScrollOffset >= firstVisibleItemScrollOffset
true
}.also {
previousIndex = firstVisibleItemIndex
previousScrollOffset = firstVisibleItemScrollOffset
@ -195,6 +204,7 @@ fun LazyListState.isScrollingUp(): State<Boolean> {
}
}
// TODO: support sensitivity
@Composable
fun ScrollState.isScrollingUp(): State<Boolean> {
return remember(this) {