-
Notifications
You must be signed in to change notification settings - Fork 8
/
NavHost.kt
42 lines (37 loc) · 1.36 KB
/
NavHost.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package me.sujanpoudel.playdeals.common.navigation
import androidx.compose.animation.AnimatedContent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveableStateHolder
@Composable
fun NavHost(navGraph: NavGraph) {
val savableStateHolder = rememberSaveableStateHolder()
val navigator = remember(navGraph) { Navigator(navGraph, savableStateHolder) }
val currentEntry by navigator.currentEntry
val backStackSize by navigator.backStackCount
val backDispatchConsumer = LocalBackPressConsumer.current
// we don't need to handle back press if backstack only has one entry
if (backStackSize > 1) {
DisposableEffect(Unit) {
backDispatchConsumer.addListener(navigator::pop)
onDispose {
backDispatchConsumer.removeListener(navigator::pop)
}
}
}
CompositionLocalProvider(
Navigator.Local provides navigator,
LocalViewModelFactory provides navigator.viewModelFactory,
) {
currentEntry?.also { entry ->
AnimatedContent(entry, transitionSpec = navigator.transitionSpec) {
savableStateHolder.SaveableStateProvider(it.id) {
it.destination.content()
}
}
}
}
}