🛠️ Fix Navigation Bar/Rail selected item

* Fixed behavior, where going in nested screen changes selected item resulting in visual glitches
This commit is contained in:
Acclorite 2024-12-28 13:40:02 +02:00
parent fad7292b34
commit f2932e1781
2 changed files with 22 additions and 2 deletions

View file

@ -2,6 +2,9 @@ package ua.acclorite.book_story.presentation.core.components.navigation_bar
import androidx.compose.material3.NavigationBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import ua.acclorite.book_story.domain.navigator.NavigatorItem
import ua.acclorite.book_story.presentation.navigator.LocalNavigator
@ -9,7 +12,14 @@ import ua.acclorite.book_story.presentation.navigator.LocalNavigator
@Composable
fun NavigationBar(tabs: List<NavigatorItem>) {
val navigator = LocalNavigator.current
val currentTab = navigator.lastItem.collectAsStateWithLifecycle()
val lastItem = navigator.lastItem.collectAsStateWithLifecycle()
val currentTab = remember { mutableStateOf(lastItem.value) }
LaunchedEffect(lastItem.value) {
if (tabs.any { it.screen::class == lastItem.value::class }) {
currentTab.value = lastItem.value
}
}
NavigationBar {
tabs.forEach { tab ->

View file

@ -14,6 +14,9 @@ import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationRail
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalLayoutDirection
@ -25,8 +28,15 @@ import ua.acclorite.book_story.presentation.navigator.LocalNavigator
@Composable
fun NavigationRail(tabs: List<NavigatorItem>) {
val navigator = LocalNavigator.current
val currentTab = navigator.lastItem.collectAsStateWithLifecycle()
val layoutDirection = LocalLayoutDirection.current
val lastItem = navigator.lastItem.collectAsStateWithLifecycle()
val currentTab = remember { mutableStateOf(lastItem.value) }
LaunchedEffect(lastItem.value) {
if (tabs.any { it.screen::class == lastItem.value::class }) {
currentTab.value = lastItem.value
}
}
NavigationRail(
modifier = Modifier