Skip to content

Commit

Permalink
Merge branch 'v2-compose-1.6'
Browse files Browse the repository at this point in the history
# Conflicts:
#	compose-destinations-codegen/src/main/java/com/ramcosta/composedestinations/codegen/writers/CustomNavTypesWriter.kt
  • Loading branch information
raamcosta committed Mar 12, 2024
2 parents bde87ac + aefaa6c commit 6ac9066
Showing 1 changed file with 39 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.ramcosta.composedestinations.codegen.commons.isArrayOrArrayList
import com.ramcosta.composedestinations.codegen.commons.isCustomArrayOrArrayListTypeNavArg
import com.ramcosta.composedestinations.codegen.commons.isCustomTypeNavArg
import com.ramcosta.composedestinations.codegen.commons.plusAssign
import com.ramcosta.composedestinations.codegen.commons.sanitizePackageName
import com.ramcosta.composedestinations.codegen.facades.CodeOutputStreamMaker
import com.ramcosta.composedestinations.codegen.model.ClassKind
import com.ramcosta.composedestinations.codegen.model.CodeGenProcessedDestination
Expand All @@ -19,6 +18,7 @@ import com.ramcosta.composedestinations.codegen.model.NavTypeSerializer
import com.ramcosta.composedestinations.codegen.model.Type
import com.ramcosta.composedestinations.codegen.model.TypeArgument
import com.ramcosta.composedestinations.codegen.templates.core.FileTemplate
import com.ramcosta.composedestinations.codegen.templates.core.setOfImportable
import com.ramcosta.composedestinations.codegen.templates.navtype.CLASS_SIMPLE_NAME_CAMEL_CASE
import com.ramcosta.composedestinations.codegen.templates.navtype.DESTINATIONS_NAV_TYPE_SERIALIZER_TYPE
import com.ramcosta.composedestinations.codegen.templates.navtype.NAV_TYPE_CLASS_SIMPLE_NAME
Expand Down Expand Up @@ -120,51 +120,49 @@ internal class CustomNavTypesWriter(
return
}

codeGenerator.makeFile(
"EnumCustomNavTypes",
"$codeGenBasePackageName.navtype",
).use { out ->
val allEnums = StringBuilder()
val typeImports = StringBuilder()
allEnums += """
package $codeGenBasePackageName.navtype
import $CORE_PACKAGE_NAME.navargs.primitives.DestinationsEnumNavType
import $CORE_PACKAGE_NAME.navargs.primitives.array.DestinationsEnumArrayNavType
import $CORE_PACKAGE_NAME.navargs.primitives.arraylist.DestinationsEnumArrayListNavType
import $CORE_PACKAGE_NAME.navargs.primitives.valueOfIgnoreCase%s1
""".trimIndent()

typesToGenerate.forEach {
val navTypeName = it.getNavTypeName()

val importable = if (it.isArrayOrArrayList()) {
it.firstTypeArg.importable
} else it.importable

if (!typeImports.contains(importable.qualifiedName)) {
typeImports += "\nimport ${importable.qualifiedName.sanitizePackageName()}"
}
val (instantiateNavType, navType) = when {
it.isArrayList() -> {
"DestinationsEnumArrayListNavType(${importable.simpleName}::class.java)" to "DestinationsEnumArrayListNavType<${importable.simpleName}>"
}
it.isArray() -> {
"DestinationsEnumArrayNavType { Array<${importable.simpleName}>(it.size) { idx -> ${importable.simpleName}::class.java.valueOfIgnoreCase(it[idx]) } }" to "DestinationsEnumArrayNavType<${importable.simpleName}>"
}
else -> {
"DestinationsEnumNavType(${importable.simpleName}::class.java)" to "DestinationsEnumNavType<${importable.simpleName}>"
}
}
val importableHelper = ImportableHelper(
setOfImportable(
"$CORE_PACKAGE_NAME.navargs.primitives.DestinationsEnumNavType",
"$CORE_PACKAGE_NAME.navargs.primitives.array.DestinationsEnumArrayNavType",
"$CORE_PACKAGE_NAME.navargs.primitives.arraylist.DestinationsEnumArrayListNavType",
"$CORE_PACKAGE_NAME.navargs.primitives.valueOfIgnoreCase",
)
)

allEnums += "\n\npublic val $navTypeName: $navType = $instantiateNavType"
val allEnums = StringBuilder()
typesToGenerate.forEach {
val navTypeName = it.getNavTypeName()

typesForNavTypeName[it] = CustomNavType(navTypeName, null)
val importable = if (it.isArrayOrArrayList()) {
it.firstTypeArg.importable
} else it.importable

val typePlaceHolder = importableHelper.addAndGetPlaceholder(importable)
val (instantiateNavType, navType) = when {
it.isArrayList() -> {
"DestinationsEnumArrayListNavType(${typePlaceHolder}::class.java)" to "DestinationsEnumArrayListNavType<${typePlaceHolder}>"
}
it.isArray() -> {
"DestinationsEnumArrayNavType { Array<${typePlaceHolder}>(it.size) { idx -> ${typePlaceHolder}::class.java.valueOfIgnoreCase(it[idx]) } }" to "DestinationsEnumArrayNavType<${typePlaceHolder}>"
}
else -> {
"DestinationsEnumNavType(${typePlaceHolder}::class.java)" to "DestinationsEnumNavType<${typePlaceHolder}>"
}
}

out += allEnums.toString()
.replace("%s1", typeImports.toString())
allEnums += "\n\npublic val $navTypeName: $navType = $instantiateNavType"

typesForNavTypeName[it] = CustomNavType(navTypeName, null)
}

codeGenerator.makeFile(
"EnumCustomNavTypes",
"$codeGenBasePackageName.navtype",
).writeSourceFile(
"package $codeGenBasePackageName.navtype",
importableHelper,
allEnums.toString()
)
}

private fun Type.generateCustomNavType(navTypeSerializer: NavTypeSerializer?) {
Expand Down

0 comments on commit 6ac9066

Please sign in to comment.