pref(LoadMore.kt): 避免重复加载
This commit is contained in:
parent
d0ad2a6a02
commit
11a3665732
|
|
@ -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()
|
||||||
) {
|
) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue