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 ee54fda + fe11150 commit f3a7238
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ const val CORE_PACKAGE_NAME = "com.ramcosta.composedestinations"

const val DESTINATION_ANNOTATION = "Destination"
const val ACTIVITY_DESTINATION_ANNOTATION = "ActivityDestination"
const val JAVA_ACTIVITY_DESTINATION_ANNOTATION = "JavaActivityDestination"
const val NAV_GRAPH_ANNOTATION = "NavGraph"
const val NAV_HOST_GRAPH_ANNOTATION = "NavHostGraph"
const val NAV_TYPE_SERIALIZER_ANNOTATION = "NavTypeSerializer"
const val DESTINATION_ANNOTATION_QUALIFIED = "$CORE_PACKAGE_NAME.annotation.$DESTINATION_ANNOTATION"
const val NAV_GRAPH_ANNOTATION_QUALIFIED = "$CORE_PACKAGE_NAME.annotation.$NAV_GRAPH_ANNOTATION"
const val NAV_HOST_GRAPH_ANNOTATION_QUALIFIED = "$CORE_PACKAGE_NAME.annotation.$NAV_HOST_GRAPH_ANNOTATION"
const val ACTIVITY_DESTINATION_ANNOTATION_QUALIFIED = "$CORE_PACKAGE_NAME.annotation.$ACTIVITY_DESTINATION_ANNOTATION"
const val JAVA_ACTIVITY_DESTINATION_ANNOTATION_QUALIFIED = "$CORE_PACKAGE_NAME.annotation.$JAVA_ACTIVITY_DESTINATION_ANNOTATION"
const val NAV_HOST_PARAM_ANNOTATION_QUALIFIED = "$CORE_PACKAGE_NAME.annotation.parameters.NavHostParam"
const val NAV_TYPE_SERIALIZER_ANNOTATION_QUALIFIED = "$CORE_PACKAGE_NAME.navargs.$NAV_TYPE_SERIALIZER_ANNOTATION"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ fun KSAnnotated.findAnnotation(name: String): KSAnnotation {
return annotations.find { it.shortName.asString() == name }!!
}

fun KSAnnotated.findAnnotationPathRecursively(name: String, path: List<KSAnnotation> = emptyList()): List<KSAnnotation>? {
fun KSAnnotated.findAnnotationPathRecursively(names: List<String>, path: List<KSAnnotation> = emptyList()): List<KSAnnotation>? {
val relevantAnnotations = annotations.filter { it.shortName.asString() !in ignoreAnnotations}
val foundAnnotation = relevantAnnotations.find { it.shortName.asString() == name }
val foundAnnotation = relevantAnnotations.find { it.shortName.asString() in names }
if (foundAnnotation != null) {
return path + foundAnnotation
}

relevantAnnotations.forEach { annotation ->
val found = annotation.annotationType.resolve().declaration.findAnnotationPathRecursively(name, path + annotation)
val found = annotation.annotationType.resolve().declaration.findAnnotationPathRecursively(names, path + annotation)
if (found != null) {
return found
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.ramcosta.composedestinations.codegen.commons.DESTINATION_ANNOTATION_D
import com.ramcosta.composedestinations.codegen.commons.DESTINATION_ANNOTATION_ROUTE_ARGUMENT
import com.ramcosta.composedestinations.codegen.commons.GENERATED_DESTINATION_SUFFIX
import com.ramcosta.composedestinations.codegen.commons.IllegalDestinationsSetup
import com.ramcosta.composedestinations.codegen.commons.JAVA_ACTIVITY_DESTINATION_ANNOTATION
import com.ramcosta.composedestinations.codegen.commons.toSnakeCase
import com.ramcosta.composedestinations.codegen.commons.toValidClassName
import com.ramcosta.composedestinations.codegen.model.ActivityDestinationParams
Expand Down Expand Up @@ -42,7 +43,7 @@ internal class KspToCodeGenDestinationsMapper(

fun map(
composableDestinationPaths: Sequence<DestinationAnnotationsPath>,
activityDestinations: Sequence<KSClassDeclaration> //TODO RACOSTA do we need to have a path as well for activity? 🤔
activityDestinations: List<KSClassDeclaration> //TODO RACOSTA do we need to have a path as well for activity? 🤔
): List<RawDestinationGenParams> {
val composableDestinations = composableDestinationPaths.groupBy { it.function.qualifiedName?.asString() }
.values.flatMap { destinationsWithSameFunction ->
Expand Down Expand Up @@ -94,15 +95,19 @@ internal class KspToCodeGenDestinationsMapper(
errorLocationHint: String
): NavGraphInfo? {
return findOverridingArgumentValue {
annotationType.resolve().arguments.firstOrNull()?.type?.resolve()
if (shortName.asString() == JAVA_ACTIVITY_DESTINATION_ANNOTATION) {
findArgumentValue<KSType>("navGraph")
} else {
annotationType.resolve().arguments.firstOrNull()?.type?.resolve()
}
}!!.toNavGraphParentInfo(
errorLocationHint = errorLocationHint,
annotationType = "@Destination"
)
}

private fun KSClassDeclaration.toActivityDestination(): RawDestinationGenParams {
val activityDestinationAnnotations = findAnnotationPathRecursively(ACTIVITY_DESTINATION_ANNOTATION)!!.reversed()
val activityDestinationAnnotations = findAnnotationPathRecursively(listOf(ACTIVITY_DESTINATION_ANNOTATION, JAVA_ACTIVITY_DESTINATION_ANNOTATION))!!.reversed()
val deepLinksAnnotations = activityDestinationAnnotations.findCumulativeArgumentValue { findArgumentValue<ArrayList<KSAnnotation>>(DESTINATION_ANNOTATION_DEEP_LINKS_ARGUMENT) }
val explicitActivityClass = activityDestinationAnnotations.findOverridingArgumentValue { findArgumentValue<KSType>("activityClass") }!!
.declaration as KSClassDeclaration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.ramcosta.composedestinations.codegen.commons.CORE_BOTTOM_SHEET_DESTIN
import com.ramcosta.composedestinations.codegen.commons.CORE_PACKAGE_NAME
import com.ramcosta.composedestinations.codegen.commons.DESTINATION_ANNOTATION_QUALIFIED
import com.ramcosta.composedestinations.codegen.commons.IllegalDestinationsSetup
import com.ramcosta.composedestinations.codegen.commons.JAVA_ACTIVITY_DESTINATION_ANNOTATION_QUALIFIED
import com.ramcosta.composedestinations.codegen.commons.NAV_GRAPH_ANNOTATION_QUALIFIED
import com.ramcosta.composedestinations.codegen.commons.NAV_HOST_GRAPH_ANNOTATION_QUALIFIED
import com.ramcosta.composedestinations.codegen.commons.NAV_TYPE_SERIALIZER_ANNOTATION_QUALIFIED
Expand Down Expand Up @@ -217,19 +218,24 @@ class Processor(
}
}

private fun Resolver.getActivityDestinations(name: String = ACTIVITY_DESTINATION_ANNOTATION_QUALIFIED): Sequence<KSClassDeclaration> {
val symbolsWithAnnotation = getSymbolsWithAnnotation(name)
private fun Resolver.getActivityDestinations(
names: List<String> = listOf(
JAVA_ACTIVITY_DESTINATION_ANNOTATION_QUALIFIED,
ACTIVITY_DESTINATION_ANNOTATION_QUALIFIED
)
): List<KSClassDeclaration> {
val symbolsWithAnnotation = names.flatMap { getSymbolsWithAnnotation(it) }

return symbolsWithAnnotation
.filterIsInstance<KSClassDeclaration>()
.filter { Modifier.ANNOTATION !in it.modifiers } + getAnnotationActivityDestinations(symbolsWithAnnotation)
}

private fun Resolver.getAnnotationActivityDestinations(symbolsWithAnnotation: Sequence<KSAnnotated>): Sequence<KSClassDeclaration> {
private fun Resolver.getAnnotationActivityDestinations(symbolsWithAnnotation: List<KSAnnotated>): List<KSClassDeclaration> {
return symbolsWithAnnotation.filterIsInstance<KSClassDeclaration>()
.filter { Modifier.ANNOTATION in it.modifiers && it.qualifiedName != null }
.flatMap {
getActivityDestinations(it.qualifiedName!!.asString())
getActivityDestinations(listOf(it.qualifiedName!!.asString()))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,26 @@ annotation class ActivityDestination<T: Annotation>(
const val DEFAULT_NULL = "@ramcosta.destinations.activity-null-default@"
}
}

/**
* Like [ActivityDestination] but specifically for java files, since
* java doesn't have annotation with type arguments, so nav graph can't be
* set in that manner.
* Use [navGraph] instead to pass that same annotation class.
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
annotation class JavaActivityDestination(
/* keep in sync with ActivityDestination fields */
val navGraph: KClass<out Annotation>,
val route: String = Destination.COMPOSABLE_NAME,
val start: Boolean = false,
val navArgs: KClass<*> = Nothing::class,
val deepLinks: Array<DeepLink> = [],
val activityClass: KClass<out Activity> = Nothing::class,
val targetPackage: String = ActivityDestination.DEFAULT_NULL,
val action: String = ActivityDestination.DEFAULT_NULL,
val dataUri: String = ActivityDestination.DEFAULT_NULL,
val dataPattern: String = ActivityDestination.DEFAULT_NULL,
val visibility: CodeGenVisibility = CodeGenVisibility.PUBLIC
)
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ annotation class ProfileSettingsGraph {
}

@Repeatable
@Destination<RootGraph>(
@Destination<Nothing>(
visibility = CodeGenVisibility.INTERNAL
)
annotation class InternalDestination<T: Annotation>(
Expand Down

0 comments on commit f3a7238

Please sign in to comment.