diff --git a/compose-destinations-codegen/src/main/java/com/ramcosta/composedestinations/codegen/commons/RawNavGraphTreeBuilder.kt b/compose-destinations-codegen/src/main/java/com/ramcosta/composedestinations/codegen/commons/RawNavGraphTreeBuilder.kt index 993760ed..384df3c9 100644 --- a/compose-destinations-codegen/src/main/java/com/ramcosta/composedestinations/codegen/commons/RawNavGraphTreeBuilder.kt +++ b/compose-destinations-codegen/src/main/java/com/ramcosta/composedestinations/codegen/commons/RawNavGraphTreeBuilder.kt @@ -62,24 +62,25 @@ internal data class RawNavGraphTree( internal val setOfPublicStartParticipatingTypes = mutableSetOf() internal fun makeNavGraphTrees( - navGraphs: List, + customNavGraphs: List, generatedDestinations: List ): List { - val navGraphsByType = (navGraphs + rootNavGraphGenParams).associateBy { it.annotationType } + val allNavGraphs = customNavGraphs + rootNavGraphGenParams + val navGraphsByType: Map = allNavGraphs.associateBy { it.annotationType } - val destinationsByNavGraphParams: Map> = + val destinationsByParent: Map> = generatedDestinations.filter { it.navGraphInfo != null }.groupBy { destination -> navGraphsByType[destination.navGraphInfo!!.graphType]!! } val rawNavGraphGenByParent: Map> = - destinationsByNavGraphParams.keys.groupBy { it.parent } + allNavGraphs.groupBy { it.parent } - return (navGraphs + destinationsByNavGraphParams.keys).toSet() - .filter { it.parent == null } - .map { + val topLevelGraphs = rawNavGraphGenByParent[null].orEmpty() + + return topLevelGraphs.mapNotNull { it.makeGraphTree( - destinationsByNavGraphParams, + destinationsByParent, rawNavGraphGenByParent, ) } @@ -88,11 +89,15 @@ internal fun makeNavGraphTrees( internal fun RawNavGraphGenParams.makeGraphTree( destinationsByNavGraphParams: Map>, navGraphsByParentType: Map> -): RawNavGraphTree { +): RawNavGraphTree? { val destinations = destinationsByNavGraphParams[this].orEmpty() val nestedNavGraphs = navGraphsByParentType[annotationType].orEmpty() - val nestedGraphs = nestedNavGraphs.map { + if (destinations.isEmpty() && nestedNavGraphs.isEmpty()) { + return null + } + + val nestedGraphs = nestedNavGraphs.mapNotNull { it.makeGraphTree(destinationsByNavGraphParams, navGraphsByParentType) }