-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavHostControllerHolder.kt
More file actions
47 lines (43 loc) · 1.72 KB
/
NavHostControllerHolder.kt
File metadata and controls
47 lines (43 loc) · 1.72 KB
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
43
44
45
46
47
package st.slex.csplashscreen.ui.components
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.navigation.NavController.OnDestinationChangedListener
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import st.slex.csplashscreen.core.navigation.Screen
import st.slex.csplashscreen.ui.components.bottom_appbar.BottomAppBarResource
@Stable
class NavHostControllerHolder private constructor(
val navController: NavHostController,
val bottomBarDestination: State<Screen?>
) {
companion object {
@Composable
fun rememberNavHostControllerHolder(): NavHostControllerHolder {
val controller = rememberNavController()
val bottomBarDestination = remember {
mutableStateOf<Screen?>(Screen.Home)
}
DisposableEffect(controller) {
val listener = OnDestinationChangedListener { _, destination, _ ->
bottomBarDestination.value =
destination.route?.let(BottomAppBarResource::getByRoute)
}
controller.addOnDestinationChangedListener(listener)
onDispose {
controller.removeOnDestinationChangedListener(listener)
}
}
return remember(controller) {
NavHostControllerHolder(
navController = controller,
bottomBarDestination = bottomBarDestination
)
}
}
}
}