Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions Owl/app/src/main/java/com/example/owl/ui/NavGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.lifecycle.Lifecycle
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
Expand Down Expand Up @@ -83,7 +85,7 @@ fun NavGraph(
startDestination = CourseTabs.FEATURED.route
) {
courses(
onCourseSelected = actions.selectCourse,
onCourseSelected = actions.openCourse,
onboardingComplete = onboardingComplete,
navController = navController,
modifier = modifier
Expand All @@ -94,12 +96,15 @@ fun NavGraph(
arguments = listOf(
navArgument(COURSE_DETAIL_ID_KEY) { type = NavType.LongType }
)
) { backStackEntry ->
) { backStackEntry: NavBackStackEntry ->
val arguments = requireNotNull(backStackEntry.arguments)
val currentCourseId = arguments.getLong(COURSE_DETAIL_ID_KEY)
CourseDetails(
courseId = arguments.getLong(COURSE_DETAIL_ID_KEY),
selectCourse = actions.selectCourse,
upPress = actions.upPress
courseId = currentCourseId,
selectCourse = { newCourseId ->
actions.relatedCourse(newCourseId, backStackEntry)
},
upPress = { actions.upPress(backStackEntry) }
)
}
}
Expand All @@ -112,10 +117,36 @@ class MainActions(navController: NavHostController) {
val onboardingComplete: () -> Unit = {
navController.popBackStack()
}
val selectCourse: (Long) -> Unit = { courseId: Long ->
navController.navigate("${MainDestinations.COURSE_DETAIL_ROUTE}/$courseId")

// Used from COURSES_ROUTE
val openCourse = { newCourseId: Long, from: NavBackStackEntry ->
// In order to discard duplicated navigation events, we check the Lifecycle
if (from.lifecycleIsResumed()) {
navController.navigate("${MainDestinations.COURSE_DETAIL_ROUTE}/$newCourseId")
}
}
val upPress: () -> Unit = {
navController.navigateUp()

// Used from COURSE_DETAIL_ROUTE
val relatedCourse = { newCourseId: Long, from: NavBackStackEntry ->
// In order to discard duplicated navigation events, we check the Lifecycle
if (from.lifecycleIsResumed()) {
navController.navigate("${MainDestinations.COURSE_DETAIL_ROUTE}/$newCourseId")
}
}

// Used from COURSE_DETAIL_ROUTE
val upPress: (rom: NavBackStackEntry) -> Unit = { from ->
// In order to discard duplicated navigation events, we check the Lifecycle
if (from.lifecycleIsResumed()) {
navController.navigateUp()
}
}
}

/**
* If the lifecycle is not resumed it means this NavBackStackEntry already processed a nav event.
*
* This is used to de-duplicate navigation events.
*/
private fun NavBackStackEntry.lifecycleIsResumed() =
this.lifecycle.currentState == Lifecycle.State.RESUMED
19 changes: 14 additions & 5 deletions Owl/app/src/main/java/com/example/owl/ui/courses/Courses.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.compose.composable
Expand All @@ -44,24 +45,32 @@ import com.example.owl.model.topics
import com.example.owl.ui.MainDestinations

fun NavGraphBuilder.courses(
onCourseSelected: (Long) -> Unit,
onCourseSelected: (Long, NavBackStackEntry) -> Unit,
onboardingComplete: State<Boolean>, // https://issuetracker.google.com/174783110
navController: NavHostController,
modifier: Modifier = Modifier
) {
composable(CourseTabs.FEATURED.route) {
composable(CourseTabs.FEATURED.route) { from ->
// Show onboarding instead if not shown yet.
LaunchedEffect(onboardingComplete) {
if (!onboardingComplete.value) {
navController.navigate(MainDestinations.ONBOARDING_ROUTE)
}
}
if (onboardingComplete.value) { // Avoid glitch when showing onboarding
FeaturedCourses(courses, onCourseSelected, modifier)
FeaturedCourses(
courses = courses,
selectCourse = { id -> onCourseSelected(id, from) },
modifier = modifier
)
}
}
composable(CourseTabs.MY_COURSES.route) {
MyCourses(courses, onCourseSelected, modifier)
composable(CourseTabs.MY_COURSES.route) { from ->
MyCourses(
courses = courses,
{ id -> onCourseSelected(id, from) },
modifier
)
}
composable(CourseTabs.SEARCH.route) {
SearchCourses(topics, modifier)
Expand Down