diff --git a/navigation/navigation-common/src/androidTest/java/androidx/navigation/serialization/RouteFilledTest.kt b/navigation/navigation-common/src/androidTest/java/androidx/navigation/serialization/RouteFilledTest.kt index 6268f33a53b7d..a46d8dd620dfa 100644 --- a/navigation/navigation-common/src/androidTest/java/androidx/navigation/serialization/RouteFilledTest.kt +++ b/navigation/navigation-common/src/androidTest/java/androidx/navigation/serialization/RouteFilledTest.kt @@ -841,6 +841,32 @@ class RouteFilledTest { } assertThatRouteFilledFrom(clazz, listOf(arg)).isEqualTo("$PATH_SERIAL_NAME") } + + @Test + fun encodeEnumList() { + @Serializable @SerialName(PATH_SERIAL_NAME) class TestClass(val arg: List) + + val clazz = TestClass(listOf(TestEnum.ONE, TestEnum.TWO)) + val arg = + navArgument("arg") { + type = InternalNavType.EnumListType(TestEnum::class.java) + nullable = false + } + assertThatRouteFilledFrom(clazz, listOf(arg)).isEqualTo("$PATH_SERIAL_NAME?arg=ONE&arg=TWO") + } + + @Test + fun encodeEnumListNullable() { + @Serializable @SerialName(PATH_SERIAL_NAME) class TestClass(val arg: List?) + + val clazz = TestClass(null) + val arg = + navArgument("arg") { + type = InternalNavType.EnumListType(TestEnum::class.java) + nullable = true + } + assertThatRouteFilledFrom(clazz, listOf(arg)).isEqualTo(PATH_SERIAL_NAME) + } } private fun assertThatRouteFilledFrom( diff --git a/navigation/navigation-common/src/main/java/androidx/navigation/serialization/NavTypeConverter.kt b/navigation/navigation-common/src/main/java/androidx/navigation/serialization/NavTypeConverter.kt index 444b8ced9e6c6..4bbf0992ee34b 100644 --- a/navigation/navigation-common/src/main/java/androidx/navigation/serialization/NavTypeConverter.kt +++ b/navigation/navigation-common/src/main/java/androidx/navigation/serialization/NavTypeConverter.kt @@ -101,6 +101,11 @@ internal fun SerialDescriptor.getNavType(): NavType<*> { InternalType.LONG -> NavType.LongListType InternalType.STRING -> NavType.StringListType InternalType.STRING_NULLABLE -> InternalNavType.StringNullableListType + InternalType.ENUM -> + @Suppress("UNCHECKED_CAST") + InternalNavType.EnumListType( + getElementDescriptor(0).getClass() as Class> + ) else -> UNKNOWN } } @@ -471,6 +476,46 @@ internal object InternalNavType { override fun emptyCollection(): List = emptyList() } + class EnumListType>(type: Class) : CollectionNavType?>(true) { + private val enumNavType = EnumType(type) + + override val name: String + get() = "List<${enumNavType.name}}>" + + override fun put(bundle: Bundle, key: String, value: List?) { + bundle.putSerializable(key, value?.let { ArrayList(value) }) + } + + @Suppress("DEPRECATION", "UNCHECKED_CAST") + override fun get(bundle: Bundle, key: String): List? = (bundle[key] as? List?) + + override fun parseValue(value: String): List = listOf(enumNavType.parseValue(value)) + + override fun parseValue(value: String, previousValue: List?): List? = + previousValue?.plus(parseValue(value)) ?: parseValue(value) + + override fun valueEquals(value: List?, other: List?): Boolean { + val valueArrayList = value?.let { ArrayList(value) } + val otherArrayList = other?.let { ArrayList(other) } + return valueArrayList == otherArrayList + } + + override fun serializeAsValues(value: List?): List = + value?.map { it.toString() } ?: emptyList() + + override fun emptyCollection(): List = emptyList() + + public override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is EnumListType<*>) return false + return enumNavType == other.enumNavType + } + + public override fun hashCode(): Int { + return enumNavType.hashCode() + } + } + class EnumNullableType?>(type: Class) : SerializableNullableType(type) { private val type: Class diff --git a/navigation/navigation-common/src/test/java/androidx/navigation/serialization/NavArgumentGeneratorTest.kt b/navigation/navigation-common/src/test/java/androidx/navigation/serialization/NavArgumentGeneratorTest.kt index 5fc3f778fd1d9..818d0450f2705 100644 --- a/navigation/navigation-common/src/test/java/androidx/navigation/serialization/NavArgumentGeneratorTest.kt +++ b/navigation/navigation-common/src/test/java/androidx/navigation/serialization/NavArgumentGeneratorTest.kt @@ -652,6 +652,34 @@ class NavArgumentGeneratorTest { assertThat(converted[0].argument.isDefaultValueUnknown).isFalse() } + @Test + fun convertToEnumList() { + @Serializable class TestClass(val arg: List) + + val converted = serializer().generateNavArguments() + val expected = + navArgument("arg") { + type = InternalNavType.EnumListType(TestEnum::class.java) + nullable = false + } + assertThat(converted).containsExactlyInOrder(expected) + assertThat(converted[0].argument.isDefaultValueUnknown).isFalse() + } + + @Test + fun convertToEnumListNullable() { + @Serializable class TestClass(val arg: List?) + + val converted = serializer().generateNavArguments() + val expected = + navArgument("arg") { + type = InternalNavType.EnumListType(TestEnum::class.java) + nullable = true + } + assertThat(converted).containsExactlyInOrder(expected) + assertThat(converted[0].argument.isDefaultValueUnknown).isFalse() + } + @Test fun convertToParcelable() { @Serializable diff --git a/navigation/navigation-runtime/src/androidTest/java/androidx/navigation/NavControllerRouteTest.kt b/navigation/navigation-runtime/src/androidTest/java/androidx/navigation/NavControllerRouteTest.kt index 22c047f11eeea..680db8615fd43 100644 --- a/navigation/navigation-runtime/src/androidTest/java/androidx/navigation/NavControllerRouteTest.kt +++ b/navigation/navigation-runtime/src/androidTest/java/androidx/navigation/NavControllerRouteTest.kt @@ -5250,6 +5250,42 @@ class NavControllerRouteTest { assertThat(route2!!.arg).containsExactly(11E123, 11.11) } + @UiThreadTest + @Test + fun testNavigateWithObjectEnumList() { + @Serializable @SerialName("test") class TestClass(val arg: List) + + val navController = createNavController() + navController.graph = + navController.createGraph( + startDestination = TestClass(listOf(TestEnum.ONE, TestEnum.TWO)) + ) { + test() + } + assertThat(navController.currentDestination?.route).isEqualTo("test?arg={arg}") + val route = navController.currentBackStackEntry?.toRoute() + assertThat(route!!.arg).containsExactly(TestEnum.ONE, TestEnum.TWO) + } + + @UiThreadTest + @Test + fun testNavigateWithObjectNullEnumList() { + @Serializable @SerialName("test") class TestClass(val arg: List? = null) + + val navController = createNavController() + navController.graph = + navController.createGraph(startDestination = TestClass(null)) { test() } + + assertThat(navController.currentDestination?.route).isEqualTo("test?arg={arg}") + val route = navController.currentBackStackEntry?.toRoute() + assertThat(route!!.arg).isNull() + + navController.navigate(TestClass(listOf(TestEnum.ONE, TestEnum.TWO))) + assertThat(navController.currentDestination?.route).isEqualTo("test?arg={arg}") + val route2 = navController.currentBackStackEntry?.toRoute() + assertThat(route2!!.arg).containsExactly(TestEnum.ONE, TestEnum.TWO) + } + @UiThreadTest @Test fun testNavigateWithObjectValueClass() {