Skip to content

Commit 27652bb

Browse files
committed
A more elegant approach by Ian
1 parent 0b3a8f5 commit 27652bb

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

Owl/app/src/main/java/com/example/owl/ui/NavGraph.kt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ import androidx.compose.runtime.Composable
2121
import androidx.compose.runtime.mutableStateOf
2222
import androidx.compose.runtime.remember
2323
import androidx.compose.ui.Modifier
24+
import androidx.lifecycle.Lifecycle
25+
import androidx.navigation.NavBackStackEntry
2426
import androidx.navigation.NavHostController
2527
import androidx.navigation.NavType
26-
import androidx.navigation.compose.KEY_ROUTE
2728
import androidx.navigation.compose.NavHost
2829
import androidx.navigation.compose.composable
2930
import androidx.navigation.compose.navArgument
@@ -84,7 +85,7 @@ fun NavGraph(
8485
startDestination = CourseTabs.FEATURED.route
8586
) {
8687
courses(
87-
onCourseSelected = { newCourseId: Long -> actions.openCourse(newCourseId) },
88+
onCourseSelected = actions.openCourse,
8889
onboardingComplete = onboardingComplete,
8990
navController = navController,
9091
modifier = modifier
@@ -95,15 +96,15 @@ fun NavGraph(
9596
arguments = listOf(
9697
navArgument(COURSE_DETAIL_ID_KEY) { type = NavType.LongType }
9798
)
98-
) { backStackEntry ->
99+
) { backStackEntry: NavBackStackEntry ->
99100
val arguments = requireNotNull(backStackEntry.arguments)
100101
val currentCourseId = arguments.getLong(COURSE_DETAIL_ID_KEY)
101102
CourseDetails(
102103
courseId = currentCourseId,
103104
selectCourse = { newCourseId ->
104-
actions.relatedCourse(newCourseId, currentCourseId)
105+
actions.relatedCourse(newCourseId, backStackEntry)
105106
},
106-
upPress = actions.upPress
107+
upPress = { actions.upPress(backStackEntry) }
107108
)
108109
}
109110
}
@@ -118,30 +119,29 @@ class MainActions(navController: NavHostController) {
118119
}
119120

120121
// 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()) {
125125
navController.navigate("${MainDestinations.COURSE_DETAIL_ROUTE}/$newCourseId")
126126
}
127127
}
128128

129129
// 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()) {
135133
navController.navigate("${MainDestinations.COURSE_DETAIL_ROUTE}/$newCourseId")
136134
}
137135
}
138136

139137
// 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()) {
144141
navController.navigateUp()
145142
}
146143
}
147144
}
145+
146+
private fun NavBackStackEntry.lifecycleIsResumed() =
147+
this.lifecycle.currentState == Lifecycle.State.RESUMED

Owl/app/src/main/java/com/example/owl/ui/courses/Courses.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import androidx.compose.ui.Modifier
3434
import androidx.compose.ui.res.painterResource
3535
import androidx.compose.ui.res.stringResource
3636
import androidx.compose.ui.unit.dp
37+
import androidx.navigation.NavBackStackEntry
3738
import androidx.navigation.NavGraphBuilder
3839
import androidx.navigation.NavHostController
3940
import androidx.navigation.compose.composable
@@ -44,24 +45,32 @@ import com.example.owl.model.topics
4445
import com.example.owl.ui.MainDestinations
4546

4647
fun NavGraphBuilder.courses(
47-
onCourseSelected: (Long) -> Unit,
48+
onCourseSelected: (Long, NavBackStackEntry) -> Unit,
4849
onboardingComplete: State<Boolean>, // https://issuetracker.google.com/174783110
4950
navController: NavHostController,
5051
modifier: Modifier = Modifier
5152
) {
52-
composable(CourseTabs.FEATURED.route) {
53+
composable(CourseTabs.FEATURED.route) { from ->
5354
// Show onboarding instead if not shown yet.
5455
LaunchedEffect(onboardingComplete) {
5556
if (!onboardingComplete.value) {
5657
navController.navigate(MainDestinations.ONBOARDING_ROUTE)
5758
}
5859
}
5960
if (onboardingComplete.value) { // Avoid glitch when showing onboarding
60-
FeaturedCourses(courses, onCourseSelected, modifier)
61+
FeaturedCourses(
62+
courses = courses,
63+
selectCourse = { id -> onCourseSelected(id, from) },
64+
modifier = modifier
65+
)
6166
}
6267
}
63-
composable(CourseTabs.MY_COURSES.route) {
64-
MyCourses(courses, onCourseSelected, modifier)
68+
composable(CourseTabs.MY_COURSES.route) { from ->
69+
MyCourses(
70+
courses = courses,
71+
{ id -> onCourseSelected(id, from) },
72+
modifier
73+
)
6574
}
6675
composable(CourseTabs.SEARCH.route) {
6776
SearchCourses(topics, modifier)

0 commit comments

Comments
 (0)