Skip to content

Commit

Permalink
Update to compose destinations 1.11.7
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomanaia committed Nov 24, 2024
1 parent 89af4ef commit b6b37d5
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 41 deletions.
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
android:appCategory="game"
android:isGame="true"
android:theme="@style/Theme.NewQuiz.Starting"
tools:ignore="UnusedAttribute">
tools:ignore="UnusedAttribute"
android:enableOnBackInvokedCallback="true">
<activity
android:name="com.infinitepower.newquiz.ui.main.MainActivity"
android:exported="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal object AppNavGraphs {
internal fun DestinationScopeWithNoDependencies<*>.currentNavigator(
remoteConfig: RemoteConfig
): CommonNavGraphNavigator {
return CommonNavGraphNavigator(navController, remoteConfig)
return CommonNavGraphNavigator(destinationsNavigator, navController, remoteConfig)
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,26 @@ import com.infinitepower.newquiz.model.wordle.WordleQuizType
import com.infinitepower.newquiz.multi_choice_quiz.destinations.MultiChoiceQuizScreenDestination
import com.infinitepower.newquiz.multi_choice_quiz.saved_questions.SavedQuestionsScreenNavigator
import com.infinitepower.newquiz.wordle.destinations.WordleScreenDestination
import com.ramcosta.composedestinations.navigation.navigate
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.utils.destination

class CommonNavGraphNavigator(
private val navigator: DestinationsNavigator,
private val navController: NavController,
private val remoteConfig: RemoteConfig
) : SavedQuestionsScreenNavigator,
MazeNavigator,
DailyChallengeScreenNavigator {

override fun navigateToWordleQuiz(type: WordleQuizType) {
navController.navigate(WordleScreenDestination(quizType = type))
navigator.navigate(WordleScreenDestination(quizType = type))
}

override fun navigateToComparisonQuiz(
category: ComparisonQuizCategory,
mode: ComparisonMode
) {
navController.navigate(ComparisonQuizScreenDestination(category.id, mode))
navigator.navigate(ComparisonQuizScreenDestination(category.id, mode))
}

override fun navigateToMultiChoiceQuiz(initialQuestions: ArrayList<MultiChoiceQuestion>) {
Expand All @@ -45,7 +47,7 @@ class CommonNavGraphNavigator(
if (this == "random") null else this
}

navController.navigate(
navigator.navigate(
MultiChoiceQuizScreenDestination(
initialQuestions = initialQuestions,
difficulty = remoteConfigDifficulty
Expand All @@ -60,7 +62,7 @@ class CommonNavGraphNavigator(
if (this == "random") null else this
}

navController.navigate(
navigator.navigate(
MultiChoiceQuizScreenDestination(
difficulty = remoteConfigDifficulty,
category = category
Expand Down Expand Up @@ -99,21 +101,21 @@ class CommonNavGraphNavigator(
}
}

navController.navigate(destination) {
popUpTo(LevelResultsScreenDestination.route) {
navigator.navigate(destination) {
popUpTo(LevelResultsScreenDestination) {
inclusive = true
}
}
}

override fun navigateToMazeResults(itemId: Int) {
navController.navigate(LevelResultsScreenDestination(itemId)) {
navigator.navigate(LevelResultsScreenDestination(itemId)) {
launchSingleTop = true

// Remove current screen from back stack
val currentRoute = navController.currentDestination?.route
if (currentRoute != null) {
popUpTo(currentRoute) {
val currentDestination = navController.currentBackStackEntry?.destination()
if (currentDestination != null) {
popUpTo(currentDestination) {
inclusive = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.PreviewLightDark
import androidx.navigation.NavController
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.rememberNavController
import com.infinitepower.newquiz.core.navigation.NavigationItem
import com.infinitepower.newquiz.core.theme.NewQuizTheme
import com.infinitepower.newquiz.multi_choice_quiz.destinations.MultiChoiceQuizScreenDestination
import com.infinitepower.newquiz.ui.components.DiamondsCounter
import com.ramcosta.composedestinations.navigation.navigate
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.navigation.EmptyDestinationsNavigator
import kotlinx.collections.immutable.ImmutableList
import kotlinx.coroutines.launch

Expand All @@ -43,6 +44,7 @@ import kotlinx.coroutines.launch
@Composable
@ExperimentalMaterial3Api
internal fun CompactContainer(
navigator: DestinationsNavigator,
navController: NavController,
primaryItems: ImmutableList<NavigationItem.Item>,
otherItems: ImmutableList<NavigationItem>,
Expand Down Expand Up @@ -72,7 +74,7 @@ internal fun CompactContainer(
items = otherItems,
onItemClick = { item ->
scope.launch { drawerState.close() }
navController.navigate(item.direction)
navigator.navigate(item.direction)
},
)
}
Expand Down Expand Up @@ -134,11 +136,12 @@ private fun CompactBottomBar(
NavigationBarItem(
selected = item == selectedItem,
onClick = {
navController.navigate(item.direction) {
navController.navigate(item.direction.route) {
// Pop up to the start destination of the graph to
// avoid building up a large stack of destinations
// on the back stack as users select items
popUpTo(navController.graph.findStartDestination().id)
val startDestination = navController.graph.startDestinationRoute
popUpTo(startDestination ?: MultiChoiceQuizScreenDestination.route)
// Avoid multiple copies of the same destination when re-selecting the same item
launchSingleTop = true
}
Expand Down Expand Up @@ -168,6 +171,7 @@ private fun CompactContainerPreview() {
Surface {
CompactContainer(
navController = rememberNavController(),
navigator = EmptyDestinationsNavigator,
content = {
Text(text = "NewQuiz")
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.infinitepower.newquiz.core.navigation.NavigationItem
import com.infinitepower.newquiz.core.theme.NewQuizTheme
import com.infinitepower.newquiz.ui.components.DiamondsCounter
import com.ramcosta.composedestinations.navigation.navigate
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.navigation.EmptyDestinationsNavigator
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList

Expand All @@ -34,7 +33,7 @@ import kotlinx.collections.immutable.toImmutableList
@Composable
@ExperimentalMaterial3Api
internal fun ExpandedContainer(
navController: NavController,
navigator: DestinationsNavigator,
primaryItems: ImmutableList<NavigationItem.Item>,
otherItems: ImmutableList<NavigationItem>,
selectedItem: NavigationItem.Item?,
Expand Down Expand Up @@ -62,7 +61,7 @@ internal fun ExpandedContainer(
selectedItem = selectedItem,
items = allNavigationItems,
onItemClick = { item ->
navController.navigate(item.direction)
navigator.navigate(item.direction)
},
)
}
Expand Down Expand Up @@ -114,7 +113,7 @@ private fun MediumContainerPreview() {
NewQuizTheme {
Surface {
ExpandedContainer(
navController = rememberNavController(),
navigator = EmptyDestinationsNavigator,
content = {
Text(text = "NewQuiz")
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.infinitepower.newquiz.core.navigation.NavigationItem
import com.infinitepower.newquiz.core.theme.NewQuizTheme
import com.infinitepower.newquiz.ui.components.DiamondsCounter
import com.ramcosta.composedestinations.navigation.navigate
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.navigation.EmptyDestinationsNavigator
import kotlinx.collections.immutable.ImmutableList
import kotlinx.coroutines.launch

Expand All @@ -44,7 +43,7 @@ import kotlinx.coroutines.launch
@Composable
@ExperimentalMaterial3Api
internal fun MediumContainer(
navController: NavController,
navigator: DestinationsNavigator,
primaryItems: ImmutableList<NavigationItem.Item>,
otherItems: ImmutableList<NavigationItem>,
selectedItem: NavigationItem.Item?,
Expand Down Expand Up @@ -73,7 +72,7 @@ internal fun MediumContainer(
items = otherItems,
onItemClick = { item ->
scope.launch { drawerState.close() }
navController.navigate(item.direction)
navigator.navigate(item.direction)
}
)
}
Expand All @@ -96,7 +95,7 @@ internal fun MediumContainer(
primaryItems.forEach { item ->
NavigationRailItem(
selected = item == selectedItem,
onClick = { navController.navigate(item.direction) },
onClick = { navigator.navigate(item.direction) },
icon = {
Icon(
imageVector = item.getIcon(item == selectedItem),
Expand Down Expand Up @@ -155,7 +154,7 @@ private fun MediumContainerPreview() {
NewQuizTheme {
Surface {
MediumContainer(
navController = rememberNavController(),
navigator = EmptyDestinationsNavigator,
content = {
Text(text = "NewQuiz")
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.infinitepower.newquiz.ui.navigation

import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.material.icons.Icons
Expand Down Expand Up @@ -42,6 +41,7 @@ import com.infinitepower.newquiz.multi_choice_quiz.destinations.MultiChoiceQuizL
import com.infinitepower.newquiz.wordle.destinations.WordleListScreenDestination
import com.ramcosta.composedestinations.spec.DestinationSpec
import com.ramcosta.composedestinations.utils.currentDestinationAsState
import com.ramcosta.composedestinations.utils.rememberDestinationsNavigator
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -110,7 +110,6 @@ private fun List<NavigationItem>.getNavigationItemBy(
): NavigationItem.Item? = filterIsInstance<NavigationItem.Item>()
.find { item -> item.direction == route }

@OptIn(ExperimentalLayoutApi::class)
@Composable
@ExperimentalMaterial3Api
internal fun NavigationContainer(
Expand All @@ -122,6 +121,7 @@ internal fun NavigationContainer(
) {
val scope = rememberCoroutineScope()

val navigator = navController.rememberDestinationsNavigator()
val destination by navController.currentDestinationAsState()

val primaryItems = remember { getPrimaryItems() }
Expand Down Expand Up @@ -162,6 +162,7 @@ internal fun NavigationContainer(
if (navigationVisible) {
when (windowWidthSize) {
WindowWidthSizeClass.Compact -> CompactContainer(
navigator = navigator,
navController = navController,
primaryItems = primaryItems,
otherItems = otherItems,
Expand All @@ -172,7 +173,7 @@ internal fun NavigationContainer(
)

WindowWidthSizeClass.Medium -> MediumContainer(
navController = navController,
navigator = navigator,
primaryItems = primaryItems,
otherItems = otherItems,
selectedItem = selectedItem,
Expand All @@ -182,7 +183,7 @@ internal fun NavigationContainer(
)

WindowWidthSizeClass.Expanded -> ExpandedContainer(
navController = navController,
navigator = navigator,
primaryItems = primaryItems,
otherItems = otherItems,
selectedItem = selectedItem,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fun ComparisonQuizScreen(
comparisonMode = comparisonMode
)
) {
popUpTo(ComparisonQuizScreenDestination.route) {
popUpTo(ComparisonQuizScreenDestination) {
inclusive = true
}
}
Expand Down
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ androidGradlePlugin = "8.7.2"
androidxAnnotation = "1.9.1"
androidxActivity = "1.9.3"
androidxAppCompat = "1.7.0"
androidxCoreKtx = "1.13.1"
androidxCoreKtx = "1.15.0"
androidxCoreSplashscreen = "1.0.1"
androidxComposeCompiler = "1.5.14"
androidxComposeBom = "2024.11.00"
Expand All @@ -18,8 +18,8 @@ androidxTestRules = "1.6.1"
androidxTestRunner = "1.6.1"
androidxTracing = "1.2.0"
coil = "2.7.0"
composeDestinations = "1.10.2"
constraintlayoutCompose = "1.0.1"
composeDestinations = "1.11.7"
constraintlayoutCompose = "1.1.0"
firebaseCrashlyticsPlugin = "3.0.2"
firebasePerfPlugin = "1.4.2"
gmsPlugin = "4.4.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import com.infinitepower.newquiz.multi_choice_quiz.destinations.MultiChoiceQuizS
import com.ramcosta.composedestinations.annotation.DeepLink
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.navigation.popUpTo
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlin.time.Duration.Companion.seconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import com.infinitepower.newquiz.multi_choice_quiz.destinations.MultiChoiceQuizR
import com.infinitepower.newquiz.multi_choice_quiz.destinations.MultiChoiceQuizScreenDestination
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.navigation.popUpTo
import kotlinx.serialization.json.Json
import com.infinitepower.newquiz.core.R as CoreR

Expand Down

0 comments on commit b6b37d5

Please sign in to comment.