Skip to content

Commit

Permalink
PullRefresh: Invoke callback only when initiated by user (#10201)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivaniskandar authored Dec 2, 2023
1 parent e5a22ea commit e5518b7
Showing 1 changed file with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,11 @@ fun PullRefresh(
content: @Composable () -> Unit,
) {
val state = rememberPullToRefreshState(
initialRefreshing = refreshing,
isRefreshing = refreshing,
extraVerticalOffset = indicatorPadding.calculateTopPadding(),
enabled = enabled,
onRefresh = onRefresh,
)
if (state.isRefreshing) {
LaunchedEffect(true) {
onRefresh()
}
}
LaunchedEffect(refreshing) {
if (refreshing && !state.isRefreshing) {
state.startRefreshAnimated()
} else if (!refreshing && state.isRefreshing) {
state.endRefreshAnimated()
}
}

Box(modifier.nestedScroll(state.nestedScrollConnection)) {
content()
Expand Down Expand Up @@ -94,10 +83,11 @@ fun PullRefresh(

@Composable
private fun rememberPullToRefreshState(
initialRefreshing: Boolean,
isRefreshing: Boolean,
extraVerticalOffset: Dp,
positionalThreshold: Dp = 64.dp,
enabled: () -> Boolean = { true },
onRefresh: () -> Unit,
): PullToRefreshStateImpl {
val density = LocalDensity.current
val extraVerticalOffsetPx = with(density) { extraVerticalOffset.toPx() }
Expand All @@ -106,18 +96,29 @@ private fun rememberPullToRefreshState(
extraVerticalOffset,
positionalThresholdPx,
enabled,
onRefresh,
saver = PullToRefreshStateImpl.Saver(
extraVerticalOffset = extraVerticalOffsetPx,
positionalThreshold = positionalThresholdPx,
enabled = enabled,
onRefresh = onRefresh,
),
) {
PullToRefreshStateImpl(
initialRefreshing = initialRefreshing,
initialRefreshing = isRefreshing,
extraVerticalOffset = extraVerticalOffsetPx,
positionalThreshold = positionalThresholdPx,
enabled = enabled,
onRefresh = onRefresh,
)
}.also {
LaunchedEffect(isRefreshing) {
if (isRefreshing && !it.isRefreshing) {
it.startRefreshAnimated()
} else if (!isRefreshing && it.isRefreshing) {
it.endRefreshAnimated()
}
}
}
}

Expand All @@ -128,13 +129,15 @@ private fun rememberPullToRefreshState(
* @param extraVerticalOffset Extra vertical offset, in pixels, for the "refreshing" state
* @param initialRefreshing The initial refreshing value of [PullToRefreshState]
* @param enabled a callback used to determine whether scroll events are to be handled by this
* @param onRefresh a callback to run when pull-to-refresh action is triggered by user
* [PullToRefreshState]
*/
private class PullToRefreshStateImpl(
initialRefreshing: Boolean,
private val extraVerticalOffset: Float,
override val positionalThreshold: Float,
enabled: () -> Boolean,
private val onRefresh: () -> Unit,
) : PullToRefreshState {

override val progress get() = adjustedDistancePulled / positionalThreshold
Expand Down Expand Up @@ -215,6 +218,7 @@ private class PullToRefreshStateImpl(
if (isRefreshing) return 0f // Already refreshing, do nothing
// Trigger refresh
if (adjustedDistancePulled > positionalThreshold) {
onRefresh()
startRefreshAnimated()
} else {
animateTo(0f)
Expand Down Expand Up @@ -263,6 +267,7 @@ private class PullToRefreshStateImpl(
extraVerticalOffset: Float,
positionalThreshold: Float,
enabled: () -> Boolean,
onRefresh: () -> Unit,
) = Saver<PullToRefreshStateImpl, Boolean>(
save = { it.isRefreshing },
restore = { isRefreshing ->
Expand All @@ -271,6 +276,7 @@ private class PullToRefreshStateImpl(
extraVerticalOffset = extraVerticalOffset,
positionalThreshold = positionalThreshold,
enabled = enabled,
onRefresh = onRefresh,
)
},
)
Expand Down

0 comments on commit e5518b7

Please sign in to comment.