From 1da660c416216651faa687f5d127cd8e3d9e4c88 Mon Sep 17 00:00:00 2001 From: Kyant0 Date: Fri, 10 Oct 2025 11:54:11 +0800 Subject: [PATCH] Fix LiquidBottomTabs `onTabSelected` dispatch --- .../catalog/components/LiquidBottomTabs.kt | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/catalog/src/main/java/com/kyant/backdrop/catalog/components/LiquidBottomTabs.kt b/catalog/src/main/java/com/kyant/backdrop/catalog/components/LiquidBottomTabs.kt index 898c1c1..09d5962 100644 --- a/catalog/src/main/java/com/kyant/backdrop/catalog/components/LiquidBottomTabs.kt +++ b/catalog/src/main/java/com/kyant/backdrop/catalog/components/LiquidBottomTabs.kt @@ -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) } }