@@ -21,9 +21,10 @@ 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
- import androidx.navigation.compose.KEY_ROUTE
27
28
import androidx.navigation.compose.NavHost
28
29
import androidx.navigation.compose.composable
29
30
import androidx.navigation.compose.navArgument
@@ -84,7 +85,7 @@ fun NavGraph(
84
85
startDestination = CourseTabs .FEATURED .route
85
86
) {
86
87
courses(
87
- onCourseSelected = { newCourseId : Long -> actions.openCourse(newCourseId) } ,
88
+ onCourseSelected = actions.openCourse,
88
89
onboardingComplete = onboardingComplete,
89
90
navController = navController,
90
91
modifier = modifier
@@ -95,15 +96,15 @@ fun NavGraph(
95
96
arguments = listOf (
96
97
navArgument(COURSE_DETAIL_ID_KEY ) { type = NavType .LongType }
97
98
)
98
- ) { backStackEntry ->
99
+ ) { backStackEntry: NavBackStackEntry ->
99
100
val arguments = requireNotNull(backStackEntry.arguments)
100
101
val currentCourseId = arguments.getLong(COURSE_DETAIL_ID_KEY )
101
102
CourseDetails (
102
103
courseId = currentCourseId,
103
104
selectCourse = { newCourseId ->
104
- actions.relatedCourse(newCourseId, currentCourseId )
105
+ actions.relatedCourse(newCourseId, backStackEntry )
105
106
},
106
- upPress = actions.upPress
107
+ upPress = { actions.upPress(backStackEntry) }
107
108
)
108
109
}
109
110
}
@@ -118,30 +119,29 @@ class MainActions(navController: NavHostController) {
118
119
}
119
120
120
121
// 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 ) {
122
+ val openCourse = { newCourseId: Long , from: NavBackStackEntry ->
123
+ // In order to discard duplicated navigation events, we check the Lifecycle
124
+ if (from.lifecycleIsResumed()) {
125
125
navController.navigate(" ${MainDestinations .COURSE_DETAIL_ROUTE } /$newCourseId " )
126
126
}
127
127
}
128
128
129
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) {
130
+ val relatedCourse = { newCourseId: Long , from: NavBackStackEntry ->
131
+ // In order to discard duplicated navigation events, we check the Lifecycle
132
+ if (from.lifecycleIsResumed()) {
135
133
navController.navigate(" ${MainDestinations .COURSE_DETAIL_ROUTE } /$newCourseId " )
136
134
}
137
135
}
138
136
139
137
// Used from COURSE_DETAIL_ROUTE
140
- val upPress: () -> Unit = {
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 ) {
138
+ val upPress: (rom: NavBackStackEntry ) -> Unit = { from ->
139
+ // In order to discard duplicated navigation events, we check the Lifecycle
140
+ if (from.lifecycleIsResumed()) {
144
141
navController.navigateUp()
145
142
}
146
143
}
147
144
}
145
+
146
+ private fun NavBackStackEntry.lifecycleIsResumed () =
147
+ this .lifecycle.currentState == Lifecycle .State .RESUMED
0 commit comments