@@ -23,6 +23,7 @@ import androidx.compose.runtime.remember
23
23
import androidx.compose.ui.Modifier
24
24
import androidx.navigation.NavHostController
25
25
import androidx.navigation.NavType
26
+ import androidx.navigation.compose.KEY_ROUTE
26
27
import androidx.navigation.compose.NavHost
27
28
import androidx.navigation.compose.composable
28
29
import androidx.navigation.compose.navArgument
@@ -83,7 +84,7 @@ fun NavGraph(
83
84
startDestination = CourseTabs .FEATURED .route
84
85
) {
85
86
courses(
86
- onCourseSelected = actions.selectCourse ,
87
+ onCourseSelected = { newCourseId : Long -> actions.openCourse(newCourseId) } ,
87
88
onboardingComplete = onboardingComplete,
88
89
navController = navController,
89
90
modifier = modifier
@@ -96,9 +97,12 @@ fun NavGraph(
96
97
)
97
98
) { backStackEntry ->
98
99
val arguments = requireNotNull(backStackEntry.arguments)
100
+ val currentCourseId = arguments.getLong(COURSE_DETAIL_ID_KEY )
99
101
CourseDetails (
100
- courseId = arguments.getLong(COURSE_DETAIL_ID_KEY ),
101
- selectCourse = actions.selectCourse,
102
+ courseId = currentCourseId,
103
+ selectCourse = { newCourseId ->
104
+ actions.relatedCourse(newCourseId, currentCourseId)
105
+ },
102
106
upPress = actions.upPress
103
107
)
104
108
}
@@ -112,10 +116,32 @@ class MainActions(navController: NavHostController) {
112
116
val onboardingComplete: () -> Unit = {
113
117
navController.popBackStack()
114
118
}
115
- val selectCourse: (Long ) -> Unit = { courseId: Long ->
116
- navController.navigate(" ${MainDestinations .COURSE_DETAIL_ROUTE } /$courseId " )
119
+
120
+ // Used from COURSES_ROUTE
121
+ val openCourse: (Long ) -> Unit = { newCourseId: Long ->
122
+ // In order to discard duplicated navigation events, check the current route
123
+ val currentRoute = navController.currentBackStackEntry?.arguments?.getString(KEY_ROUTE )
124
+ if (currentRoute?.startsWith(MainDestinations .COURSES_ROUTE ) == true ) {
125
+ navController.navigate(" ${MainDestinations .COURSE_DETAIL_ROUTE } /$newCourseId " )
126
+ }
117
127
}
128
+
129
+ // Used from COURSE_DETAIL_ROUTE
130
+ val relatedCourse: (Long , Long ) -> Unit = { newCourseId: Long , currentCourseId: Long ->
131
+ // In order to discard duplicated navigation events, check the current courseId
132
+ val navControllerCurrentCourseId = navController.currentBackStackEntry?.arguments
133
+ ?.getLong(COURSE_DETAIL_ID_KEY )
134
+ if (navControllerCurrentCourseId == currentCourseId) {
135
+ navController.navigate(" ${MainDestinations .COURSE_DETAIL_ROUTE } /$newCourseId " )
136
+ }
137
+ }
138
+
139
+ // Used from COURSE_DETAIL_ROUTE
118
140
val upPress: () -> Unit = {
119
- navController.navigateUp()
141
+ // In order to discard duplicated navigation events, check the current route
142
+ val currentRoute = navController.currentBackStackEntry?.arguments?.getString(KEY_ROUTE )
143
+ if (currentRoute?.startsWith(" ${MainDestinations .COURSE_DETAIL_ROUTE } /" ) == true ) {
144
+ navController.navigateUp()
145
+ }
120
146
}
121
147
}
0 commit comments