From b2edec39eb65674d250525e7e76ed5fdac1b1ff9 Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+HuanCheng65@users.noreply.github.com> Date: Sat, 22 Jul 2023 08:16:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=88=97=E8=A1=A8=E6=BB=91=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tieba/post/ui/widgets/compose/LoadMore.kt | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/widgets/compose/LoadMore.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/widgets/compose/LoadMore.kt index 4c536328..6b275848 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/widgets/compose/LoadMore.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/widgets/compose/LoadMore.kt @@ -7,6 +7,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.* import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -49,11 +50,12 @@ fun LoadMoreLayout( ) { val loadDistance = with(LocalDensity.current) { LoadDistance.toPx() } + val canLoadMore = remember(enableLoadMore, loadEnd) { enableLoadMore && !loadEnd } + val curIsLoading by rememberUpdatedState(newValue = isLoading) + val curCanLoadMore by rememberUpdatedState(newValue = canLoadMore) + var waitingStateReset by remember { mutableStateOf(false) } - val curIsLoading by rememberUpdatedState(newValue = isLoading) - val canLoadMore = remember(enableLoadMore, loadEnd) { enableLoadMore && !loadEnd } - val curCanLoadMore by rememberUpdatedState(newValue = canLoadMore) val swipeableState = rememberSwipeableState(false) { newValue -> if (newValue && !curIsLoading && curCanLoadMore) { onLoadMore() @@ -61,14 +63,14 @@ fun LoadMoreLayout( true } else !newValue } - val stateOffset by swipeableState.offset - LaunchedEffect(stateOffset) { - if (waitingStateReset && abs(stateOffset - loadDistance) < 1f) { + + val isStateReset by remember { derivedStateOf { abs(swipeableState.offset.value - loadDistance) < 1f } } + LaunchedEffect(waitingStateReset, isStateReset) { + if (waitingStateReset && isStateReset) { waitingStateReset = false } } - Box( modifier = Modifier .nestedScroll(swipeableState.LoadPreUpPostDownNestedScrollConnection) @@ -80,7 +82,7 @@ fun LoadMoreLayout( ), thresholds = { _, _ -> FractionalThreshold(0.5f) }, orientation = Orientation.Vertical, - enabled = enableLoadMore && !swipeableState.isAnimationRunning && !waitingStateReset, + enabled = enableLoadMore && !waitingStateReset, ) .fillMaxSize() ) {