forked from android/compose-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NavGraph.kt
151 lines (139 loc) · 5.27 KB
/
NavGraph.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.owl.ui
import androidx.activity.compose.BackHandler
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
import androidx.navigation.compose.composable
import androidx.navigation.compose.navArgument
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navigation
import com.example.owl.ui.MainDestinations.COURSE_DETAIL_ID_KEY
import com.example.owl.ui.course.CourseDetails
import com.example.owl.ui.courses.CourseTabs
import com.example.owl.ui.courses.courses
import com.example.owl.ui.onboarding.Onboarding
/**
* Destinations used in the ([OwlApp]).
*/
object MainDestinations {
const val ONBOARDING_ROUTE = "onboarding"
const val COURSES_ROUTE = "courses"
const val COURSE_DETAIL_ROUTE = "course"
const val COURSE_DETAIL_ID_KEY = "courseId"
}
@Composable
fun NavGraph(
modifier: Modifier = Modifier,
finishActivity: () -> Unit = {},
navController: NavHostController = rememberNavController(),
startDestination: String = MainDestinations.COURSES_ROUTE,
showOnboardingInitially: Boolean = true
) {
// Onboarding could be read from shared preferences.
val onboardingComplete = remember(showOnboardingInitially) {
mutableStateOf(!showOnboardingInitially)
}
val actions = remember(navController) { MainActions(navController) }
NavHost(
navController = navController,
startDestination = startDestination
) {
composable(MainDestinations.ONBOARDING_ROUTE) {
// Intercept back in Onboarding: make it finish the activity
BackHandler {
finishActivity()
}
Onboarding(
onboardingComplete = {
// Set the flag so that onboarding is not shown next time.
onboardingComplete.value = true
actions.onboardingComplete()
}
)
}
navigation(
route = MainDestinations.COURSES_ROUTE,
startDestination = CourseTabs.FEATURED.route
) {
courses(
onCourseSelected = actions.openCourse,
onboardingComplete = onboardingComplete,
navController = navController,
modifier = modifier
)
}
composable(
"${MainDestinations.COURSE_DETAIL_ROUTE}/{$COURSE_DETAIL_ID_KEY}",
arguments = listOf(
navArgument(COURSE_DETAIL_ID_KEY) { type = NavType.LongType }
)
) { backStackEntry: NavBackStackEntry ->
val arguments = requireNotNull(backStackEntry.arguments)
val currentCourseId = arguments.getLong(COURSE_DETAIL_ID_KEY)
CourseDetails(
courseId = currentCourseId,
selectCourse = { newCourseId ->
actions.relatedCourse(newCourseId, backStackEntry)
},
upPress = { actions.upPress(backStackEntry) }
)
}
}
}
/**
* Models the navigation actions in the app.
*/
class MainActions(navController: NavHostController) {
val onboardingComplete: () -> Unit = {
navController.popBackStack()
}
// 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")
}
}
// 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