Fix LiquidBottomTabs onTabSelected dispatch

This commit is contained in:
Kyant0 committed 2025-10-10 11:56:38 +08:00
1 parent bafcbad715
commit 1da660c416
1 file changed
+18 -8
@@ -16,8 +16,10 @@ import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@@ -49,6 +51,7 @@ import com.kyant.backdrop.shadow.InnerShadow
import com.kyant.backdrop.shadow.Shadow
import com.kyant.capsule.ContinuousCapsule
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.launch
import kotlin.math.abs
import kotlin.math.sign
@@ -98,6 +101,9 @@ fun LiquidBottomTabs(
}
val animationScope = rememberCoroutineScope()
var currentIndex by remember(selectedTabIndex) {
mutableIntStateOf(selectedTabIndex())
}
val dampedDragAnimation = remember(animationScope) {
DampedDragAnimation(
animationScope = animationScope,
@@ -109,10 +115,8 @@ fun LiquidBottomTabs(
onDragStarted = {},
onDragStopped = {
val targetIndex = targetValue.fastRoundToInt().fastCoerceIn(0, tabsCount - 1)
currentIndex = targetIndex
animateToValue(targetIndex.toFloat())
if (selectedTabIndex() != targetIndex) {
onTabSelected(targetIndex)
}
animationScope.launch {
offsetAnimation.animateTo(
0f,
@@ -130,12 +134,18 @@ fun LiquidBottomTabs(
}
)
}
LaunchedEffect(dampedDragAnimation) {
snapshotFlow { selectedTabIndex().toFloat() }
LaunchedEffect(selectedTabIndex) {
snapshotFlow { selectedTabIndex() }
.collectLatest { index ->
if (dampedDragAnimation.targetValue != index) {
dampedDragAnimation.animateToValue(index)
}
currentIndex = index
}
}
LaunchedEffect(dampedDragAnimation) {
snapshotFlow { currentIndex }
.drop(1)
.collectLatest { index ->
dampedDragAnimation.animateToValue(index.toFloat())
onTabSelected(index)
}
}