adrielcafe / voyager

🛸 A pragmatic navigation library for Jetpack Compose
https://voyager.adriel.cafe
MIT License
2.54k stars 130 forks source link

Wear OS support? #81

Open shaacker opened 2 years ago

shaacker commented 2 years ago

While Voyager can be set up on Wear OS and compiles fine, the actual navigation isn't working, as Wear OS has a concept of SwipeToDismiss and the normal backstack handling isn't supported.

Is there a chance Wear OS support can be added?

andrewfluck commented 1 year ago

I am taking this library and slimming it down for internal use, however, if you take a the few variable names and rename them to what they should be in this library, you can get something workable:

@Composable
fun ScreenCardStack() {
    val navigator = LocalNavigator.currentOrThrow
    val history = navigator.history

    val swipeState = rememberSwipeToDismissBoxState()
    val previous = if (history.size <= 1) null else history[history.lastIndex - 1]
    val current = navigator.lastEntry

    SwipeToDismissBox(
        navigator::pop,
        Modifier.fillMaxSize(),
        swipeState,
        backgroundScrimColor = Theme.colors.background,
        contentScrimColor = Color.Black,
        hasBackground = navigator.canPop,
        backgroundKey = previous?.key ?: SwipeToDismissKeys.Background,
        contentKey = current.key,
    ) { background ->
        Box(Modifier.fillMaxWidth(1F).background(Theme.colors.background)) {
            CompositionLocalProvider(
                LocalContentColor provides Theme.colors.contentColorFor(Theme.colors.background),
                content = { (if (background) previous else current)?.Content() }
            )
        }
    }
}

You'll probably need to change the design system to whatever you're using too

Tolriq commented 4 months ago

@andrewfluck Do you still use this? This solution does not work properly with state restoration as the background during the swipe have the wrong state that will then be proper after the swipe.

andrewfluck commented 4 months ago

@andrewfluck Do you still use this? This solution does not work properly with state restoration as the background during the swipe have the wrong state that will then be proper after the swipe.

I will have to update a bunch of dependencies for this project, but yes, the goal is at least to make a case for feasibility of the technology being used, so even if used internally, I still would like to make sure things are stable. If there's a better way to approach this, I'd be happy to take suggestions!

Tolriq commented 4 months ago

I was hoping you had one :)

The issue is that since the code is called at different place the key generation of the saveables are different and we recreate the content when moving from previous to current.

I suppose I'll have to look at the compose navigation part to see how they handle this.