Simpler scrolling

This commit is contained in:
Yuri Schimke 2021-12-19 12:24:28 +00:00
parent ee6408a5b0
commit 4ab4ea1830

View file

@ -1,10 +1,10 @@
package com.surrus.peopleinspace
import android.content.Context
import android.view.MotionEvent
import android.view.ViewConfiguration
import androidx.compose.foundation.gestures.ScrollableState
import androidx.compose.foundation.gestures.scrollBy
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
@ -14,34 +14,27 @@ import androidx.compose.ui.input.pointer.pointerInteropFilter
import androidx.compose.ui.platform.LocalContext
import androidx.core.view.InputDeviceCompat
import androidx.core.view.MotionEventCompat
import androidx.core.view.ViewConfigurationCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
fun processEvent(context: Context, scope: CoroutineScope, event: MotionEvent, scrollState: ScrollableState): Boolean {
if (event.action != MotionEvent.ACTION_SCROLL ||
!event.isFromSource(InputDeviceCompat.SOURCE_ROTARY_ENCODER)
) {
return false
}
val delta = -event.getAxisValue(MotionEventCompat.AXIS_SCROLL) *
ViewConfigurationCompat.getScaledVerticalScrollFactor(
ViewConfiguration.get(context), context
)
scope.launch {
scrollState.scrollBy(delta)
}
return true
}
@OptIn(ExperimentalComposeUiApi::class)
fun Modifier.scrollHandler(scrollState: ScrollableState): Modifier = composed {
val context = LocalContext.current
val scope = rememberCoroutineScope()
val scaledVerticalScrollFactor =
remember { ViewConfiguration.get(context).getScaledVerticalScrollFactor() }
this.pointerInteropFilter(RequestDisallowInterceptTouchEvent()) { event ->
println(event)
processEvent(context, scope, event, scrollState)
if (event.action != MotionEvent.ACTION_SCROLL ||
!event.isFromSource(InputDeviceCompat.SOURCE_ROTARY_ENCODER)
) {
false
} else {
val delta = -event.getAxisValue(MotionEventCompat.AXIS_SCROLL) *
scaledVerticalScrollFactor
scope.launch {
scrollState.scrollBy(delta)
}
true
}
}
}