Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.ramcosta.composedestinations.codegen.templates.core.FileTemplate
import com.ramcosta.composedestinations.codegen.templates.core.setOfImportable

const val NAV_GRAPHS_PLACEHOLDER = "[NAV_GRAPHS_PLACEHOLDER]"
const val NAV_GRAPHS_LIST_PLACEHOLDER = "[NAV_GRAPHS_LIST_PLACEHOLDER]"

val navGraphsObjectTemplate = FileTemplate(
packageStatement = "package $codeGenBasePackageName",
Expand All @@ -24,6 +25,8 @@ val navGraphsObjectTemplate = FileTemplate(
object $GENERATED_NAV_GRAPHS_OBJECT {

$NAV_GRAPHS_PLACEHOLDER

$NAV_GRAPHS_LIST_PLACEHOLDER
}
""".trimIndent()
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,35 @@ data class $GENERATED_NAV_GRAPH(
override val destinationsByRoute: Map<String, $typeAliasDestination> = destinations.associateBy { it.route }
}

/**
* Return the parent of this [Destination][DestinationSpec] by searching through the [NavGraph]s in
* the [NavGraphs] object.
*
* This variable should **NEVER** be null. If `first { ... }` throws a [NoSuchElementException], it
* means this [Destination][DestinationSpec] somehow isn't part of a [NavGraph].
*/
val $CORE_DESTINATION_SPEC<*>.parent: $GENERATED_NAV_GRAPH
get() = $GENERATED_NAV_GRAPHS_OBJECT.all.first {
it.destinations.contains(this)
}

/**
* Return the parent of this [NavGraphSpec] by searching through the [NavGraph]s'
* [NavGraph.nestedNavGraphs] in the [NavGraphs] object.
*/
val $CORE_NAV_GRAPH_SPEC.parent: $GENERATED_NAV_GRAPH?
get() = $GENERATED_NAV_GRAPHS_OBJECT.all.find {
it.nestedNavGraphs.contains(this)
}

/**
* Provides a sequence of the [DestinationSpec]'s hierarchy. The hierarchy starts with this
* [DestinationSpec]'s [parent][DestinationSpec.parent] [NavGraph], then that graph's parent, and up
* the hierarchy until you've reached the root navigation graph.
*/
val $CORE_DESTINATION_SPEC<*>.hierarchy: Sequence<$GENERATED_NAV_GRAPH>
get() = generateSequence(this.parent) { it.parent }

/**
* If this [Route] is a [$typeAliasDestination], returns it
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import com.ramcosta.composedestinations.codegen.model.GeneratedDestination
import com.ramcosta.composedestinations.codegen.model.Importable
import com.ramcosta.composedestinations.codegen.model.NavGraphGeneratingParams
import com.ramcosta.composedestinations.codegen.model.NavGraphInfo
import com.ramcosta.composedestinations.codegen.templates.NAV_GRAPHS_LIST_PLACEHOLDER
import com.ramcosta.composedestinations.codegen.templates.NAV_GRAPHS_PLACEHOLDER
import com.ramcosta.composedestinations.codegen.templates.navGraphsObjectTemplate
import com.ramcosta.composedestinations.codegen.writers.helpers.ImportableHelper
import com.ramcosta.composedestinations.codegen.writers.helpers.writeSourceFile
import java.util.Collections.addAll

class LegacyNavGraphsSingleObjectWriter(
private val codeGenerator: CodeOutputStreamMaker,
Expand All @@ -31,6 +33,7 @@ class LegacyNavGraphsSingleObjectWriter(
importableHelper = importableHelper,
sourceCode = navGraphsObjectTemplate.sourceCode
.replace(NAV_GRAPHS_PLACEHOLDER, navGraphsDeclaration(navGraphsParams))
.replace(NAV_GRAPHS_LIST_PLACEHOLDER, navGraphsListDeclaration(navGraphsParams))
)

return navGraphsParams
Expand Down Expand Up @@ -79,6 +82,19 @@ class LegacyNavGraphsSingleObjectWriter(

}

private fun navGraphsListDeclaration(navGraphsParams: List<NavGraphGeneratingParams>): String {
val navGraphsAnchor = "[NAV_GRAPHS]"
val navGraphFieldNames = navGraphsParams.joinToString(",\n\t\t") {
navGraphFieldName(it.route)
}
return """
| val all: List<$GENERATED_NAV_GRAPH> = listOf(
| $navGraphsAnchor
| )
""".trimMargin()
.replace(navGraphsAnchor, navGraphFieldNames)
}

private fun requireOptInAnnotations(navGraphRequireOptInImportables: Set<Importable>): String {
val code = StringBuilder()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.ramcosta.composedestinations.codegen.commons.*
import com.ramcosta.composedestinations.codegen.facades.CodeOutputStreamMaker
import com.ramcosta.composedestinations.codegen.facades.Logger
import com.ramcosta.composedestinations.codegen.model.*
import com.ramcosta.composedestinations.codegen.templates.NAV_GRAPHS_LIST_PLACEHOLDER
import com.ramcosta.composedestinations.codegen.templates.NAV_GRAPHS_PLACEHOLDER
import com.ramcosta.composedestinations.codegen.templates.navGraphsObjectTemplate
import com.ramcosta.composedestinations.codegen.writers.helpers.ImportableHelper
Expand Down Expand Up @@ -64,7 +65,7 @@ class NavGraphsSingleObjectWriter(
nestedNavGraphs.forEach {
addAll(destinationsByNavGraphParams[it]!!.requireOptInAnnotationClassTypes())
}
},
}
)
}

Expand All @@ -86,6 +87,7 @@ class NavGraphsSingleObjectWriter(
importableHelper = importableHelper,
sourceCode = navGraphsObjectTemplate.sourceCode
.replace(NAV_GRAPHS_PLACEHOLDER, navGraphsDeclaration(orderedNavGraphGenParams))
.replace(NAV_GRAPHS_LIST_PLACEHOLDER, navGraphsListDeclaration(orderedNavGraphGenParams))

)
}
Expand Down Expand Up @@ -130,6 +132,19 @@ class NavGraphsSingleObjectWriter(

}

private fun navGraphsListDeclaration(navGraphsParams: List<NavGraphGeneratingParams>): String {
val navGraphsAnchor = "[NAV_GRAPHS]"
val navGraphFieldNames = navGraphsParams.joinToString(",\n\t\t") {
navGraphFieldName(it.route)
}
return """
| val all: List<$GENERATED_NAV_GRAPH> = listOf(
| $navGraphsAnchor
| )
""".trimMargin()
.replace(navGraphsAnchor, navGraphFieldNames)
}

private fun destinationsInsideList(destinations: List<GeneratedDestination>): String {
val code = StringBuilder()
destinations.forEachIndexed { i, it ->
Expand Down