Skip to content

Commit

Permalink
Merge branch 'v2-compose-1.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
raamcosta committed Mar 11, 2024
2 parents f3a7238 + 1ae7124 commit 88d549c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ const val CORE_DIRECTION_DESTINATION_SPEC = "DirectionDestinationSpec"
const val CORE_ALIAS_NAV_GRAPH_SPEC = "NavGraphSpec"

val CORE_NAV_HOST_ANIMATED_DESTINATION_STYLE = Importable("NavHostAnimatedDestinationStyle", "$CORE_PACKAGE_NAME.animations.NavHostAnimatedDestinationStyle")
val CORE_NAV_HOST_GRAPH_SPEC = Importable("NavHostGraphSpec", "$CORE_PACKAGE_NAME.spec.NavHostGraphSpec")
val CORE_TYPED_NAV_HOST_GRAPH_SPEC = Importable("TypedNavHostGraphSpec", "$CORE_PACKAGE_NAME.spec.TypedNavHostGraphSpec")
val CORE_DIRECTION_NAV_HOST_GRAPH_SPEC = Importable("DirectionNavHostGraphSpec", "$CORE_PACKAGE_NAME.spec.DirectionNavHostGraphSpec")
val CORE_DIRECTION_NAV_GRAPH_SPEC = Importable("DirectionNavGraphSpec", "$CORE_PACKAGE_NAME.spec.DirectionNavGraphSpec")
val CORE_TYPED_NAV_GRAPH_SPEC = Importable("TypedNavGraphSpec", "$CORE_PACKAGE_NAME.spec.TypedNavGraphSpec")
val CORE_ACTIVITY_DESTINATION_SPEC = Importable("ActivityDestinationSpec", "$CORE_PACKAGE_NAME.spec.ActivityDestinationSpec")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal class NavGraphsSingleObjectWriter(

private fun navGraphWriter(rawNavGraphTree: RawNavGraphTree) = singleNavGraphWriter(
codeGenerator,
importableHelper,
ImportableHelper(navGraphsObjectTemplate.imports),
rawNavGraphTree,
NavArgResolver(customNavTypeByType, importableHelper)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import com.ramcosta.composedestinations.codegen.commons.CORE_ALIAS_NAV_GRAPH_SPE
import com.ramcosta.composedestinations.codegen.commons.CORE_DESTINATION_ANIMATION_STYLE
import com.ramcosta.composedestinations.codegen.commons.CORE_DIRECTION
import com.ramcosta.composedestinations.codegen.commons.CORE_DIRECTION_NAV_GRAPH_SPEC
import com.ramcosta.composedestinations.codegen.commons.CORE_DIRECTION_NAV_HOST_GRAPH_SPEC
import com.ramcosta.composedestinations.codegen.commons.CORE_NAV_HOST_ANIMATED_DESTINATION_STYLE
import com.ramcosta.composedestinations.codegen.commons.CORE_NAV_HOST_GRAPH_SPEC
import com.ramcosta.composedestinations.codegen.commons.CORE_PACKAGE_NAME
import com.ramcosta.composedestinations.codegen.commons.CORE_TYPED_NAV_GRAPH_SPEC
import com.ramcosta.composedestinations.codegen.commons.CORE_TYPED_NAV_HOST_GRAPH_SPEC
import com.ramcosta.composedestinations.codegen.commons.RawNavGraphTree
import com.ramcosta.composedestinations.codegen.commons.bundleImportable
import com.ramcosta.composedestinations.codegen.commons.plusAssign
Expand Down Expand Up @@ -59,7 +60,8 @@ internal class SingleNavGraphWriter(
private val navArgResolver: NavArgResolver
) {
private val navGraphType = CORE_TYPED_NAV_GRAPH_SPEC
private val navHostNavGraphType = CORE_NAV_HOST_GRAPH_SPEC
private val typedNavHostNavGraphType = CORE_TYPED_NAV_HOST_GRAPH_SPEC
private val directionNavHostNavGraphType = CORE_DIRECTION_NAV_HOST_GRAPH_SPEC
private val directionNavGraphType = CORE_DIRECTION_NAV_GRAPH_SPEC

private val navArgumentBridgeCodeBuilder = NavArgumentBridgeCodeBuilder(
Expand Down Expand Up @@ -160,6 +162,10 @@ internal class SingleNavGraphWriter(
}

private fun RawNavGraphTree.graphInvokeFunction(): String {
if (isNavHostGraph || hasNoArgs()) {
return "\n"
}

if (usesSameArgsAsStartRoute()) {
return """
|
Expand All @@ -172,10 +178,6 @@ internal class SingleNavGraphWriter(
""".trimMargin()
}

if (isNavHostGraph || hasNoArgs()) {
return ""
}

val navArgsType = navArgTypes.first?.let { importableHelper.addAndGetPlaceholder(it) } ?: "Unit"
val directionRouteSuffix = if (navArgTypes.second != null) {
" + \n\t\t\t\t\t\"\${startRoute(startRouteArgs).route.removePrefix(startRoute.baseRoute)}\""
Expand Down Expand Up @@ -280,7 +282,11 @@ internal class SingleNavGraphWriter(

private fun RawNavGraphTree.graphSuperType(): String {
if (isNavHostGraph) {
return importableHelper.addAndGetPlaceholder(navHostNavGraphType)
return if (startRouteArgs != null) {
"${importableHelper.addAndGetPlaceholder(typedNavHostNavGraphType)}<${importableHelper.addAndGetPlaceholder(startRouteArgs.type)}>"
} else {
importableHelper.addAndGetPlaceholder(directionNavHostNavGraphType)
}
}

val (graphArgs, startRouteNavArgsName) = navArgTypes.let { argTypes ->
Expand All @@ -298,7 +304,11 @@ internal class SingleNavGraphWriter(

private fun RawNavGraphTree.startRouteType(isDestination: Boolean): String {
if (isNavHostGraph) {
return "TypedRoute<Unit>"
return if (startRouteArgs != null) {
"TypedRoute<${importableHelper.addAndGetPlaceholder(startRouteArgs.type)}>"
} else {
"TypedRoute<Unit>"
}
}

return if (startRouteArgs != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
package com.ramcosta.composedestinations.spec

import android.os.Bundle
import androidx.lifecycle.SavedStateHandle
import com.ramcosta.composedestinations.animations.NavHostAnimatedDestinationStyle

typealias NavHostGraphSpec = TypedNavHostGraphSpec<*>

/**
* Like [DirectionNavGraphSpec] but used specifically for top level navigation graphs (i.e they
* have no parent graph) that are meant to pass to [com.ramcosta.composedestinations.DestinationsNavHost] call.
*/
interface NavHostGraphSpec : DirectionNavGraphSpec {
interface TypedNavHostGraphSpec<START_ROUTE_NAV_ARGS>: TypedNavGraphSpec<START_ROUTE_NAV_ARGS, START_ROUTE_NAV_ARGS> {

override val baseRoute: String get() = route

override fun invoke(navArgs: START_ROUTE_NAV_ARGS): Direction {
//args cannot have mandatory args on start routes of NavHostGraphs, so this is ok
return Direction(route)
}

/**
* Like [TypedNavGraphSpec.defaultTransitions] but not nullable since NavHost level
* graphs must have animations defined (even if they are defined as "No animations")
*/
override val defaultTransitions: NavHostAnimatedDestinationStyle
}

interface DirectionNavHostGraphSpec : TypedNavHostGraphSpec<Unit>, Direction {

override val baseRoute: String get() = route

override fun invoke(navArgs: Unit): Direction = this

operator fun invoke(): Direction = this

override fun argsFrom(bundle: Bundle?) = Unit

override fun argsFrom(savedStateHandle: SavedStateHandle) = Unit
}

0 comments on commit 88d549c

Please sign in to comment.