@@ -21,6 +21,8 @@ import androidx.compose.runtime.Composable
21
21
import androidx.compose.runtime.mutableStateOf
22
22
import androidx.compose.runtime.remember
23
23
import androidx.compose.ui.Modifier
24
+ import androidx.lifecycle.Lifecycle
25
+ import androidx.navigation.NavBackStackEntry
24
26
import androidx.navigation.NavHostController
25
27
import androidx.navigation.NavType
26
28
import androidx.navigation.compose.NavHost
@@ -83,7 +85,7 @@ fun NavGraph(
83
85
startDestination = CourseTabs .FEATURED .route
84
86
) {
85
87
courses(
86
- onCourseSelected = actions.selectCourse ,
88
+ onCourseSelected = actions.openCourse ,
87
89
onboardingComplete = onboardingComplete,
88
90
navController = navController,
89
91
modifier = modifier
@@ -94,12 +96,15 @@ fun NavGraph(
94
96
arguments = listOf (
95
97
navArgument(COURSE_DETAIL_ID_KEY ) { type = NavType .LongType }
96
98
)
97
- ) { backStackEntry ->
99
+ ) { backStackEntry: NavBackStackEntry ->
98
100
val arguments = requireNotNull(backStackEntry.arguments)
101
+ val currentCourseId = arguments.getLong(COURSE_DETAIL_ID_KEY )
99
102
CourseDetails (
100
- courseId = arguments.getLong(COURSE_DETAIL_ID_KEY ),
101
- selectCourse = actions.selectCourse,
102
- upPress = actions.upPress
103
+ courseId = currentCourseId,
104
+ selectCourse = { newCourseId ->
105
+ actions.relatedCourse(newCourseId, backStackEntry)
106
+ },
107
+ upPress = { actions.upPress(backStackEntry) }
103
108
)
104
109
}
105
110
}
@@ -112,10 +117,36 @@ class MainActions(navController: NavHostController) {
112
117
val onboardingComplete: () -> Unit = {
113
118
navController.popBackStack()
114
119
}
115
- val selectCourse: (Long ) -> Unit = { courseId: Long ->
116
- navController.navigate(" ${MainDestinations .COURSE_DETAIL_ROUTE } /$courseId " )
120
+
121
+ // Used from COURSES_ROUTE
122
+ val openCourse = { newCourseId: Long , from: NavBackStackEntry ->
123
+ // In order to discard duplicated navigation events, we check the Lifecycle
124
+ if (from.lifecycleIsResumed()) {
125
+ navController.navigate(" ${MainDestinations .COURSE_DETAIL_ROUTE } /$newCourseId " )
126
+ }
117
127
}
118
- val upPress: () -> Unit = {
119
- navController.navigateUp()
128
+
129
+ // Used from COURSE_DETAIL_ROUTE
130
+ val relatedCourse = { newCourseId: Long , from: NavBackStackEntry ->
131
+ // In order to discard duplicated navigation events, we check the Lifecycle
132
+ if (from.lifecycleIsResumed()) {
133
+ navController.navigate(" ${MainDestinations .COURSE_DETAIL_ROUTE } /$newCourseId " )
134
+ }
135
+ }
136
+
137
+ // Used from COURSE_DETAIL_ROUTE
138
+ val upPress: (rom: NavBackStackEntry ) -> Unit = { from ->
139
+ // In order to discard duplicated navigation events, we check the Lifecycle
140
+ if (from.lifecycleIsResumed()) {
141
+ navController.navigateUp()
142
+ }
120
143
}
121
144
}
145
+
146
+ /* *
147
+ * If the lifecycle is not resumed it means this NavBackStackEntry already processed a nav event.
148
+ *
149
+ * This is used to de-duplicate navigation events.
150
+ */
151
+ private fun NavBackStackEntry.lifecycleIsResumed () =
152
+ this .lifecycle.currentState == Lifecycle .State .RESUMED
0 commit comments