-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
runtime-saveable
: supports tvos
and watchos
#3521
Comments
Hello, thanks for the issue! |
For now, |
Thanks for your reply. I cannot achieve that by using remember and rememberSaveableStateHolder |
Originally posted by @syt0r in #1932 (comment)
interface MultiplatformMainNavigationState : MainNavigationState {
val currentDestination: State<MainDestination>
val stateHolder: SaveableStateHolder
}
@Composable
fun MultiplatformMainNavigation(
state: MainNavigationState
) {
state as MultiplatformMainNavigationState
val destination = state.currentDestination.value
state.stateHolder.SaveableStateProvider(destination.toString()) {
when (destination) {
MainDestination.Home -> {
val homeNavigationState = rememberHomeNavigationState()
HomeScreen(
viewModel = getMultiplatformViewModel(),
mainNavigationState = state,
homeNavigationState = homeNavigationState
)
}
MainDestination.About -> {
AboutScreen(
mainNavigationState = state,
viewModel = getMultiplatformViewModel()
)
}
// ...others
}
}
}
@Composable
fun rememberMultiplatformMainNavigationState(): MainNavigationState {
val stack = remember { mutableStateOf<List<MainDestination>>(listOf(MainDestination.Home)) }
val currentDestinationState = remember { derivedStateOf { stack.value.last() } }
val stateHolder = rememberSaveableStateHolder()
return remember {
object : MultiplatformMainNavigationState {
override val currentDestination = currentDestinationState
override val stateHolder: SaveableStateHolder = stateHolder
override fun navigateBack() {
val lastItem = stack.value.last()
stack.value = stack.value.dropLast(1)
stateHolder.removeState(lastItem.toString())
}
override fun popUpToHome() {
val itemsToRemove = stack.value.drop(1)
stack.value = stack.value.take(1)
itemsToRemove.forEach { stateHolder.removeState(it) }
}
override fun navigate(destination: MainDestination) {
stack.value = stack.value.plus(destination)
}
}
}
} |
Closed by #3899 |
Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks. |
Currently, this modules supports android, jvm, js, ios and macos.
Please support
tvos
andwatchos
.The text was updated successfully, but these errors were encountered: