Created
April 29, 2024 00:43
-
-
Save hristogochev/dff5d9d5c782726f0ea7260f22337d0d to your computer and use it in GitHub Desktop.
Fix for Voyager TabNavigator not disposing of nested navigators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object FirstTab : Tab { | |
override var navigator: Navigator? = null | |
override val options: TabOptions | |
@Composable | |
get() = TODO("Not yet implemented") | |
@Composable | |
override fun Content() { | |
Navigator(FirstTabFirstScreen()) { | |
LaunchedEffect(it) { | |
navigator = it | |
} | |
FadeTransition(it) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FirstTabFirstScreen : Screen { | |
@Composable | |
override fun Content( | |
) { | |
// Content | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleTabsScreen : Screen { | |
private val tabs = listOf<Tab>() | |
@Composable | |
override fun Content() { | |
TabNavigator( | |
tab = tabs.first(), | |
tabDisposable = { navigator -> | |
TabDisposable(navigator, tabs) | |
}, | |
) { | |
CurrentTab() | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.graphics.painter.Painter | |
import cafe.adriel.voyager.core.screen.Screen | |
import cafe.adriel.voyager.navigator.Navigator | |
@Composable | |
public fun CurrentTab() { | |
val tabNavigator = LocalTabNavigator.current | |
val currentTab = tabNavigator.current | |
tabNavigator.saveableState("currentTab") { | |
currentTab.Content() | |
} | |
} | |
public data class TabOptions( | |
val index: UShort, | |
val title: String, | |
val icon: Painter? = null | |
) | |
public interface Tab : Screen { | |
public var navigator: Navigator? | |
public val options: TabOptions | |
@Composable get | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment