Fix weird C crash on snaping scroll.

This commit is contained in:
Thomas Andres Gomez 2023-08-16 11:43:50 +02:00
parent b9d14d12ff
commit 9dbfb9c3a0

View file

@ -73,7 +73,6 @@ import com.skydoves.landscapist.ImageOptions
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlin.math.max
@Stable
@ -459,21 +458,26 @@ private fun rememberSnapConnection(
@Composable
@Stable
private fun rememberScrollConnection(
scope: CoroutineScope = rememberCoroutineScope(),
scrollState: ScrollState,
): NestedScrollConnection {
return remember(scrollState) {
object : NestedScrollConnection {
override fun onPreScroll(
available: Offset,
source: NestedScrollSource
): Offset = runBlocking {
Offset(
x = 0f,
y = when (scrollState.canScrollForward) {
true -> -scrollState.scrollBy(-available.y)
else -> 0f
},
)
source: NestedScrollSource,
): Offset {
val delta = when {
scrollState.value == 0 && available.y > 0f -> 0f
scrollState.value == scrollState.maxValue -> 0f
else -> {
scope.launch { scrollState.scrollBy(-available.y) }
available.y
}
}
return Offset(x = 0f, y = delta)
}
}
}