Skip to content

Commit 0b3a8f5

Browse files
committed
[Owl] Fixes multiple navigation calls with simultaneous taps
1 parent 917e60d commit 0b3a8f5

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import androidx.compose.runtime.remember
2323
import androidx.compose.ui.Modifier
2424
import androidx.navigation.NavHostController
2525
import androidx.navigation.NavType
26+
import androidx.navigation.compose.KEY_ROUTE
2627
import androidx.navigation.compose.NavHost
2728
import androidx.navigation.compose.composable
2829
import androidx.navigation.compose.navArgument
@@ -83,7 +84,7 @@ fun NavGraph(
8384
startDestination = CourseTabs.FEATURED.route
8485
) {
8586
courses(
86-
onCourseSelected = actions.selectCourse,
87+
onCourseSelected = { newCourseId: Long -> actions.openCourse(newCourseId) },
8788
onboardingComplete = onboardingComplete,
8889
navController = navController,
8990
modifier = modifier
@@ -96,9 +97,12 @@ fun NavGraph(
9697
)
9798
) { backStackEntry ->
9899
val arguments = requireNotNull(backStackEntry.arguments)
100+
val currentCourseId = arguments.getLong(COURSE_DETAIL_ID_KEY)
99101
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+
},
102106
upPress = actions.upPress
103107
)
104108
}
@@ -112,10 +116,32 @@ class MainActions(navController: NavHostController) {
112116
val onboardingComplete: () -> Unit = {
113117
navController.popBackStack()
114118
}
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+
}
117127
}
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
118140
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+
}
120146
}
121147
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.example.owl.ui.course
1818

19+
import android.util.Log
1920
import androidx.activity.compose.BackHandler
2021
import androidx.compose.animation.core.animateDpAsState
2122
import androidx.compose.foundation.Image
@@ -323,7 +324,10 @@ private fun RelatedCourses(
323324
items(relatedCourses) { related ->
324325
CourseListItem(
325326
course = related,
326-
onClick = { selectCourse(related.id) },
327+
onClick = {
328+
Log.d("jalc", "jalc")
329+
selectCourse(related.id)
330+
},
327331
titleStyle = MaterialTheme.typography.body2,
328332
modifier = Modifier
329333
.padding(end = 8.dp)

0 commit comments

Comments
 (0)