AppState.kt 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package com.zaojiao.app.ui
  2. import androidx.compose.runtime.Composable
  3. import androidx.compose.runtime.Stable
  4. import androidx.compose.runtime.remember
  5. import androidx.compose.runtime.rememberCoroutineScope
  6. import androidx.core.os.trace
  7. import androidx.navigation.NavDestination
  8. import androidx.navigation.NavGraph.Companion.findStartDestination
  9. import androidx.navigation.NavHostController
  10. import androidx.navigation.compose.currentBackStackEntryAsState
  11. import androidx.navigation.compose.rememberNavController
  12. import androidx.navigation.navOptions
  13. import com.zaojiao.app.navigation.HomeDestination
  14. import com.zaojiao.app.feat.home.navigation.homeCourse
  15. import com.zaojiao.app.feat.home.navigation.homeIndex
  16. import com.zaojiao.app.feat.home.navigation.homePersonal
  17. import com.zaojiao.app.feat.home.navigation.navigateToHomeCourse
  18. import com.zaojiao.app.feat.home.navigation.navigateToHomeIndex
  19. import com.zaojiao.app.feat.home.navigation.navigateToHomePersonal
  20. import kotlinx.coroutines.CoroutineScope
  21. @Composable
  22. fun rememberAppState(
  23. coroutineScope: CoroutineScope = rememberCoroutineScope(),
  24. navController: NavHostController = rememberNavController(),
  25. ): AppState {
  26. return remember(
  27. navController,
  28. coroutineScope,
  29. ) {
  30. AppState(
  31. navController,
  32. coroutineScope,
  33. )
  34. }
  35. }
  36. @Stable
  37. class AppState(
  38. val navController: NavHostController,
  39. val coroutineScope: CoroutineScope,
  40. ) {
  41. val currentDestination: NavDestination?
  42. @Composable get() = navController
  43. .currentBackStackEntryAsState().value?.destination
  44. val currentHomeDestination: HomeDestination?
  45. @Composable get() = when (currentDestination?.route) {
  46. homeIndex -> HomeDestination.INDEX
  47. homeCourse -> HomeDestination.COURSE
  48. homePersonal -> HomeDestination.PERSONAL
  49. else -> null
  50. }
  51. val homeDestinations: List<HomeDestination> = HomeDestination.values().asList()
  52. fun navigateToHomeDestination(destination: HomeDestination) {
  53. trace("Navigation: ${destination.name}") {
  54. val topLevelNavOptions = navOptions {
  55. // Pop up to the start destination of the graph to
  56. // avoid building up a large stack of destinations
  57. // on the back stack as users select items
  58. popUpTo(navController.graph.findStartDestination().id) {
  59. saveState = true
  60. }
  61. // Avoid multiple copies of the same destination when
  62. // reselecting the same item
  63. launchSingleTop = true
  64. // Restore state when reselecting a previously selected item
  65. restoreState = true
  66. }
  67. when (destination) {
  68. HomeDestination.INDEX -> navController.navigateToHomeIndex(topLevelNavOptions)
  69. HomeDestination.COURSE -> navController.navigateToHomeCourse(topLevelNavOptions)
  70. HomeDestination.PERSONAL -> navController.navigateToHomePersonal(topLevelNavOptions)
  71. }
  72. }
  73. }
  74. }