From 38fded8aaf385ea36062faa803dc668450ec0747 Mon Sep 17 00:00:00 2001 From: Rafael Costa Date: Sat, 20 Jul 2024 12:48:19 +0100 Subject: [PATCH] Some issue fixes cherry picked from v2 --- .../composedestinations/DefaultNavHostEngine.kt | 5 ++++- .../parcelable/DefaultParcelableNavTypeSerializer.kt | 8 +++++++- .../composedestinations/spec/DestinationSpec.kt | 10 +++++----- .../ramcosta/composedestinations/spec/NavGraphSpec.kt | 2 ++ .../com/ramcosta/composedestinations/spec/Route.kt | 9 +++++++++ .../composedestinations/ksp/ProcessorProviderTests.kt | 3 +++ 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/compose-destinations/src/main/java/com/ramcosta/composedestinations/DefaultNavHostEngine.kt b/compose-destinations/src/main/java/com/ramcosta/composedestinations/DefaultNavHostEngine.kt index 0de16507..9ca2999d 100644 --- a/compose-destinations/src/main/java/com/ramcosta/composedestinations/DefaultNavHostEngine.kt +++ b/compose-destinations/src/main/java/com/ramcosta/composedestinations/DefaultNavHostEngine.kt @@ -80,7 +80,10 @@ internal class DefaultNavHostEngine( ) = with(defaultAnimationParams) { androidx.navigation.compose.NavHost( navController = navController, - startDestination = startRoute.route, + // since start destinations of NavHosts can't have + // mandatory arguments, we can use baseRoute here safely + // prevents official lib to consider weird arg values like "{argName}" + startDestination = startRoute.baseRoute, modifier = modifier, route = route, contentAlignment = navHostContentAlignment, diff --git a/compose-destinations/src/main/java/com/ramcosta/composedestinations/navargs/parcelable/DefaultParcelableNavTypeSerializer.kt b/compose-destinations/src/main/java/com/ramcosta/composedestinations/navargs/parcelable/DefaultParcelableNavTypeSerializer.kt index 9473a1db..0e04a326 100644 --- a/compose-destinations/src/main/java/com/ramcosta/composedestinations/navargs/parcelable/DefaultParcelableNavTypeSerializer.kt +++ b/compose-destinations/src/main/java/com/ramcosta/composedestinations/navargs/parcelable/DefaultParcelableNavTypeSerializer.kt @@ -25,7 +25,13 @@ class DefaultParcelableNavTypeSerializer( } override fun fromRouteString(routeStr: String): Parcelable { - val (className, base64) = routeStr.split("@").let { it[0] to it[1] } + val splits = routeStr.split("@") + // must throw IllegalArgumentException here, such as with require function + require(splits.size == 2) { + "Impossible to get Parcelable from $routeStr" + } + + val (className, base64) = splits.let { it[0] to it[1] } val creator = if (jClass.isFinal) { // Since we have this, small optimization to avoid additional reflection call of Class.forName diff --git a/compose-destinations/src/main/java/com/ramcosta/composedestinations/spec/DestinationSpec.kt b/compose-destinations/src/main/java/com/ramcosta/composedestinations/spec/DestinationSpec.kt index 50abeba8..7ad606bb 100644 --- a/compose-destinations/src/main/java/com/ramcosta/composedestinations/spec/DestinationSpec.kt +++ b/compose-destinations/src/main/java/com/ramcosta/composedestinations/spec/DestinationSpec.kt @@ -1,10 +1,12 @@ package com.ramcosta.composedestinations.spec import android.os.Bundle -import androidx.annotation.RestrictTo import androidx.compose.runtime.Composable import androidx.lifecycle.SavedStateHandle -import androidx.navigation.* +import androidx.navigation.NamedNavArgument +import androidx.navigation.NavBackStackEntry +import androidx.navigation.NavController +import androidx.navigation.NavDeepLink import com.ramcosta.composedestinations.scope.DestinationScope /** @@ -54,10 +56,8 @@ interface DestinationSpec : Route { /** * Prefix of the route - basically [route] without argument info. - * Meant for internal usage only. */ - @get:RestrictTo(RestrictTo.Scope.SUBCLASSES) - val baseRoute: String + override val baseRoute: String /** * All [NamedNavArgument]s that will be added to the navigation diff --git a/compose-destinations/src/main/java/com/ramcosta/composedestinations/spec/NavGraphSpec.kt b/compose-destinations/src/main/java/com/ramcosta/composedestinations/spec/NavGraphSpec.kt index ade3c268..535f2211 100644 --- a/compose-destinations/src/main/java/com/ramcosta/composedestinations/spec/NavGraphSpec.kt +++ b/compose-destinations/src/main/java/com/ramcosta/composedestinations/spec/NavGraphSpec.kt @@ -26,4 +26,6 @@ interface NavGraphSpec : Direction, Route { * Nested navigation graphs of this navigation graph. */ val nestedNavGraphs: List get() = emptyList() + + override val baseRoute: String get() = route } diff --git a/compose-destinations/src/main/java/com/ramcosta/composedestinations/spec/Route.kt b/compose-destinations/src/main/java/com/ramcosta/composedestinations/spec/Route.kt index 622cf50b..82e6254d 100644 --- a/compose-destinations/src/main/java/com/ramcosta/composedestinations/spec/Route.kt +++ b/compose-destinations/src/main/java/com/ramcosta/composedestinations/spec/Route.kt @@ -12,5 +12,14 @@ package com.ramcosta.composedestinations.spec */ sealed interface Route { + /** + * Full route pattern that will be added to the navigation graph. + * Navigation arguments are not filled in. + */ val route: String + + /** + * Prefix of the route - basically [route] without argument info. + */ + val baseRoute: String } diff --git a/playground/src/test/kotlin/com/ramcosta/composedestinations/ksp/ProcessorProviderTests.kt b/playground/src/test/kotlin/com/ramcosta/composedestinations/ksp/ProcessorProviderTests.kt index 79d22587..ce14b2dc 100644 --- a/playground/src/test/kotlin/com/ramcosta/composedestinations/ksp/ProcessorProviderTests.kt +++ b/playground/src/test/kotlin/com/ramcosta/composedestinations/ksp/ProcessorProviderTests.kt @@ -1,3 +1,5 @@ +@file:OptIn(ExperimentalCompilerApi::class) + package com.ramcosta.composedestinations.ksp import com.tschuchort.compiletesting.KotlinCompilation @@ -6,6 +8,7 @@ import com.tschuchort.compiletesting.SourceFile.Companion.kotlin import com.tschuchort.compiletesting.kspIncremental import com.tschuchort.compiletesting.kspSourcesDir import com.tschuchort.compiletesting.symbolProcessorProviders +import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Rule