Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make codegen always use 'experimental' navigation #750

Merged
merged 3 commits into from
Apr 30, 2024
Merged
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Change Log
==========

## 0.24.0 **UNRELEASED**

### Codegen

- Removed `experimentalNavigation` option and `@UseExperimentalNavigation`. The generated
code will now always use Khonshu's built-in navigation instead of androidx.navigation.


## 0.23.1 *(2024-03-08)*

### Navigation
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public data class NavHostActivityData(

public val activityBaseClass: ClassName,

val experimentalNavigation: Boolean,
val navHostParameter: ComposableParameter,
override val stateParameter: ComposableParameter?,
override val sendActionParameter: ComposableParameter?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ package com.freeletics.khonshu.codegen.codegen

import com.freeletics.khonshu.codegen.BaseData
import com.freeletics.khonshu.codegen.NavHostActivityData
import com.freeletics.khonshu.codegen.util.androidxNavHost
import com.freeletics.khonshu.codegen.util.bundle
import com.freeletics.khonshu.codegen.util.compositionLocalProvider
import com.freeletics.khonshu.codegen.util.experimentalNavHost
import com.freeletics.khonshu.codegen.util.localActivityComponentProvider
import com.freeletics.khonshu.codegen.util.navHostTransitionAnimations
import com.freeletics.khonshu.codegen.util.navHost
import com.freeletics.khonshu.codegen.util.optInAnnotation
import com.freeletics.khonshu.codegen.util.remember
import com.freeletics.khonshu.codegen.util.setContent
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier.OVERRIDE
import com.squareup.kotlinpoet.TypeSpec
Expand Down Expand Up @@ -49,48 +46,18 @@ internal class ActivityGenerator(
compositionLocalProvider,
localActivityComponentProvider,
)
.apply {
if (data.experimentalNavigation) {
beginControlFlow("val useExperimentalNavigation = %M(component)", remember)
.addStatement("component.useExperimentalNavigation")
.endControlFlow()
.beginControlFlow("if (useExperimentalNavigation)")
.addCode(callNavHost(true))
.nextControlFlow("else")
.addCode(callNavHost(false))
.endControlFlow()
} else {
addCode(callNavHost(false))
}
}
.endControlFlow()
.endControlFlow()
.endControlFlow()
.build()
}

private fun callNavHost(experimental: Boolean): CodeBlock {
return CodeBlock.builder()
.apply {
if (experimental) {
addStatement("%M(", experimentalNavHost)
} else {
addStatement("%M(", androidxNavHost)
}
}
.addStatement("%M(", navHost)
.addStatement(" startRoute = startRoute,")
.addStatement(" destinations = %M(component) { component.destinations },", remember)
.addStatement(" modifier = modifier,")
.addStatement(" deepLinkHandlers = %M(component) { component.deepLinkHandlers },", remember)
.addStatement(" deepLinkPrefixes = %M(component) { component.deepLinkPrefixes },", remember)
.addStatement(" navEventNavigator = %M(component) { component.navEventNavigator },", remember)
.addStatement(" destinationChangedCallback = destinationChangedCallback,")
.apply {
if (!experimental) {
addStatement(" transitionAnimations = %T.noAnimations(),", navHostTransitionAnimations)
}
}
.addStatement(")")
.endControlFlow()
.endControlFlow()
.endControlFlow()
.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.freeletics.khonshu.codegen.codegen

import com.freeletics.khonshu.codegen.BaseData
import com.freeletics.khonshu.codegen.NavHostActivityData
import com.freeletics.khonshu.codegen.UseExperimentalNavigation
import com.freeletics.khonshu.codegen.util.asParameter
import com.freeletics.khonshu.codegen.util.bindsInstanceParameter
import com.freeletics.khonshu.codegen.util.contributesToAnnotation
Expand All @@ -19,9 +18,7 @@ import com.freeletics.khonshu.codegen.util.simplePropertySpec
import com.freeletics.khonshu.codegen.util.subcomponentAnnotation
import com.freeletics.khonshu.codegen.util.subcomponentFactoryAnnotation
import com.squareup.anvil.compiler.internal.decapitalize
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.AnnotationSpec.UseSiteTarget.GET
import com.squareup.kotlinpoet.BOOLEAN
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier.ABSTRACT
import com.squareup.kotlinpoet.KModifier.OVERRIDE
Expand Down Expand Up @@ -83,11 +80,6 @@ internal class ComponentGenerator(
PropertySpec.builder("deepLinkHandlers", immutableSet.parameterizedBy(deepLinkHandler)).build(),
PropertySpec.builder("deepLinkPrefixes", immutableSet.parameterizedBy(deepLinkPrefix)).build(),
)
if (data.experimentalNavigation) {
properties += PropertySpec.builder("useExperimentalNavigation", BOOLEAN)
.addAnnotation(AnnotationSpec.builder(UseExperimentalNavigation::class).useSiteTarget(GET).build())
.build()
}
}
properties += PropertySpec.builder(closeableSetPropertyName, SET.parameterizedBy(Closeable::class.asTypeName()))
.addAnnotation(forScope(data.scope, GET))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ internal fun TopLevelFunctionReference.toNavHostActivityData(annotation: Annotat
parentScope = annotation.parentScope,
stateMachine = stateMachine.asClassName(),
activityBaseClass = annotation.activityBaseClass,
experimentalNavigation = annotation.experimentalNavigation,
navHostParameter = navHostParameter,
composableParameter = getInjectedParameters(stateParameter, actionParameter, navHostParameter.typeName),
stateParameter = getParameterWithType(stateParameter),
Expand Down Expand Up @@ -124,9 +123,6 @@ private val AnnotationReference.destinationScope: ClassName
private val AnnotationReference.activityBaseClass: ClassName
get() = requireClassReferenceArgument("activityBaseClass", 3).asClassName()

internal val AnnotationReference.experimentalNavigation: Boolean
get() = argumentAt("experimentalNavigation", 4)?.value() ?: false

private fun TopLevelFunctionReference.getParameterWithType(expectedType: TypeName): ComposableParameter? {
return parameters.firstNotNullOfOrNull { parameter ->
parameter.toComposableParameter { it == expectedType }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ internal fun KSFunctionDeclaration.toNavHostActivityData(
parentScope = annotation.parentScope,
stateMachine = annotation.stateMachine,
activityBaseClass = annotation.activityBaseClass,
experimentalNavigation = annotation.experimentalNavigation,
navHostParameter = navHostParameter,
composableParameter = getInjectedParameters(stateParameter, actionParameter, navHostParameter.typeName),
stateParameter = getParameterWithType(stateParameter),
Expand All @@ -101,9 +100,6 @@ private val KSAnnotation.destinationScope: ClassName
private val KSAnnotation.activityBaseClass: ClassName
get() = (findArgument("activityBaseClass").value as KSType).toClassName()

internal val KSAnnotation.experimentalNavigation: Boolean
get() = findArgument("experimentalNavigation").value as Boolean

private fun KSAnnotation.findArgument(name: String): KSValueArgument {
return arguments.find { it.name?.asString() == name }
?: defaultArguments.find { it.name?.asString() == name }!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ internal val baseRoute = ClassName("com.freeletics.khonshu.navigation", "BaseRou
internal val baseRouteFqName = FqName(baseRoute.canonicalName)
internal val navEventNavigator = ClassName("com.freeletics.khonshu.navigation", "NavEventNavigator")
internal val navigationExecutor = ClassName("com.freeletics.khonshu.navigation.internal", "NavigationExecutor")
internal val androidxNavHost = MemberName("com.freeletics.khonshu.navigation.androidx", "NavHost")
internal val navHostTransitionAnimations =
ClassName("com.freeletics.khonshu.navigation.androidx", "NavHostTransitionAnimations")
internal val experimentalNavHost = MemberName("com.freeletics.khonshu.navigation", "NavHost")
internal val navHost = MemberName("com.freeletics.khonshu.navigation", "NavHost")
internal val navigationSetup = MemberName("com.freeletics.khonshu.navigation", "NavigationSetup")
internal val navigationDestination = ClassName("com.freeletics.khonshu.navigation", "NavDestination")
internal val screenDestination = MemberName("com.freeletics.khonshu.navigation", "ScreenDestination")
Expand Down
4 changes: 0 additions & 4 deletions codegen/api/android/codegen.api
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public abstract interface annotation class com/freeletics/khonshu/codegen/NavDes

public abstract interface annotation class com/freeletics/khonshu/codegen/NavHostActivity : java/lang/annotation/Annotation {
public abstract fun activityBaseClass ()Ljava/lang/Class;
public abstract fun experimentalNavigation ()Z
public abstract fun parentScope ()Ljava/lang/Class;
public abstract fun scope ()Ljava/lang/Class;
public abstract fun stateMachine ()Ljava/lang/Class;
Expand All @@ -22,9 +21,6 @@ public abstract interface annotation class com/freeletics/khonshu/codegen/NavHos
public abstract interface class com/freeletics/khonshu/codegen/Overlay {
}

public abstract interface annotation class com/freeletics/khonshu/codegen/UseExperimentalNavigation : java/lang/annotation/Annotation {
}

public final class com/freeletics/khonshu/codegen/internal/ActivityComponentLookupKt {
public static final fun findComponentByScope (Landroid/content/Context;Lkotlin/reflect/KClass;)Ljava/lang/Object;
}
Expand Down
4 changes: 0 additions & 4 deletions codegen/api/jvm/codegen.api
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public abstract interface annotation class com/freeletics/khonshu/codegen/NavDes

public abstract interface annotation class com/freeletics/khonshu/codegen/NavHostActivity : java/lang/annotation/Annotation {
public abstract fun activityBaseClass ()Ljava/lang/Class;
public abstract fun experimentalNavigation ()Z
public abstract fun parentScope ()Ljava/lang/Class;
public abstract fun scope ()Ljava/lang/Class;
public abstract fun stateMachine ()Ljava/lang/Class;
Expand All @@ -22,9 +21,6 @@ public abstract interface annotation class com/freeletics/khonshu/codegen/NavHos
public abstract interface class com/freeletics/khonshu/codegen/Overlay {
}

public abstract interface annotation class com/freeletics/khonshu/codegen/UseExperimentalNavigation : java/lang/annotation/Annotation {
}

public abstract interface annotation class com/freeletics/khonshu/codegen/internal/InternalCodegenApi : java/lang/annotation/Annotation {
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public annotation class NavHostActivity(
val parentScope: KClass<*> = AppScope::class,
val stateMachine: KClass<out StateMachine<*, *>>,
val activityBaseClass: KClass<*>,
val experimentalNavigation: Boolean = false,
)

/**
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.freeletics.khonshu.sample.feature.root.nav.RootRoute
@NavHostActivity(
stateMachine = MainStateMachine::class,
activityBaseClass = ComponentActivity::class,
experimentalNavigation = true,
)
@Composable
internal fun MainScreen(
Expand Down
Loading