pref(LoadMore.kt): 避免重复加载

This commit is contained in:
HuanCheng65 2023-07-21 22:19:23 +08:00
parent d0ad2a6a02
commit 11a3665732
No known key found for this signature in database
GPG Key ID: 5EC9DD60A32C7360
1 changed files with 23 additions and 8 deletions

View File

@ -7,6 +7,11 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.* import androidx.compose.material.*
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Offset
@ -21,6 +26,7 @@ import androidx.compose.ui.unit.dp
import com.huanchengfly.tieba.post.R import com.huanchengfly.tieba.post.R
import com.huanchengfly.tieba.post.ui.common.theme.compose.ExtendedTheme import com.huanchengfly.tieba.post.ui.common.theme.compose.ExtendedTheme
import com.huanchengfly.tieba.post.ui.common.theme.compose.loadMoreIndicator import com.huanchengfly.tieba.post.ui.common.theme.compose.loadMoreIndicator
import kotlin.math.abs
import kotlin.math.roundToInt import kotlin.math.roundToInt
private val LoadDistance = 70.dp private val LoadDistance = 70.dp
@ -43,16 +49,25 @@ fun LoadMoreLayout(
) { ) {
val loadDistance = with(LocalDensity.current) { LoadDistance.toPx() } val loadDistance = with(LocalDensity.current) { LoadDistance.toPx() }
val canLoadMore = (enableLoadMore && !loadEnd) var waitingStateReset by remember { mutableStateOf(false) }
val swipeableState = if (canLoadMore) { val curIsLoading by rememberUpdatedState(newValue = isLoading)
rememberSwipeableState(isLoading) { newValue -> val canLoadMore = remember(enableLoadMore, loadEnd) { enableLoadMore && !loadEnd }
if (newValue && !isLoading) onLoadMore() val curCanLoadMore by rememberUpdatedState(newValue = canLoadMore)
val swipeableState = rememberSwipeableState(false) { newValue ->
if (newValue && !curIsLoading && curCanLoadMore) {
onLoadMore()
waitingStateReset = true
true true
} } else !newValue
} else {
rememberSwipeableState(false)
} }
val stateOffset by swipeableState.offset
LaunchedEffect(stateOffset) {
if (waitingStateReset && abs(stateOffset - loadDistance) < 1f) {
waitingStateReset = false
}
}
Box( Box(
modifier = Modifier modifier = Modifier
@ -65,7 +80,7 @@ fun LoadMoreLayout(
), ),
thresholds = { _, _ -> FractionalThreshold(0.5f) }, thresholds = { _, _ -> FractionalThreshold(0.5f) },
orientation = Orientation.Vertical, orientation = Orientation.Vertical,
enabled = enableLoadMore, enabled = enableLoadMore && !swipeableState.isAnimationRunning && !waitingStateReset,
) )
.fillMaxSize() .fillMaxSize()
) { ) {