Fix PTR initial refreshing state (#10173)

This commit is contained in:
Ivan Iskandar 2023-11-25 22:27:49 +07:00 committed by GitHub
parent 356fc5b524
commit 53edae1b6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -46,6 +46,7 @@ fun PullRefresh(
content: @Composable () -> Unit, content: @Composable () -> Unit,
) { ) {
val state = rememberPullToRefreshState( val state = rememberPullToRefreshState(
initialRefreshing = refreshing,
extraVerticalOffset = indicatorPadding.calculateTopPadding(), extraVerticalOffset = indicatorPadding.calculateTopPadding(),
enabled = enabled, enabled = enabled,
) )
@ -90,6 +91,7 @@ fun PullRefresh(
@Composable @Composable
private fun rememberPullToRefreshState( private fun rememberPullToRefreshState(
initialRefreshing: Boolean,
extraVerticalOffset: Dp, extraVerticalOffset: Dp,
positionalThreshold: Dp = 64.dp, positionalThreshold: Dp = 64.dp,
enabled: () -> Boolean = { true }, enabled: () -> Boolean = { true },
@ -108,7 +110,7 @@ private fun rememberPullToRefreshState(
), ),
) { ) {
PullToRefreshStateImpl( PullToRefreshStateImpl(
initialRefreshing = false, initialRefreshing = initialRefreshing,
extraVerticalOffset = extraVerticalOffsetPx, extraVerticalOffset = extraVerticalOffsetPx,
positionalThreshold = positionalThresholdPx, positionalThreshold = positionalThresholdPx,
enabled = enabled, enabled = enabled,
@ -133,18 +135,21 @@ private class PullToRefreshStateImpl(
) : PullToRefreshState { ) : PullToRefreshState {
override val progress get() = adjustedDistancePulled / positionalThreshold override val progress get() = adjustedDistancePulled / positionalThreshold
override var verticalOffset by mutableFloatStateOf(0f) override var verticalOffset by mutableFloatStateOf(if (initialRefreshing) refreshingVerticalOffset else 0f)
override var isRefreshing by mutableStateOf(initialRefreshing) override var isRefreshing by mutableStateOf(initialRefreshing)
private val refreshingVerticalOffset: Float
get() = positionalThreshold + extraVerticalOffset
override fun startRefresh() { override fun startRefresh() {
isRefreshing = true isRefreshing = true
verticalOffset = positionalThreshold + extraVerticalOffset verticalOffset = refreshingVerticalOffset
} }
suspend fun startRefreshAnimated() { suspend fun startRefreshAnimated() {
isRefreshing = true isRefreshing = true
animateTo(positionalThreshold + extraVerticalOffset) animateTo(refreshingVerticalOffset)
} }
override fun endRefresh() { override fun endRefresh() {