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

Get environment and select resource by qualifiers #4018

Merged
merged 12 commits into from
Dec 20, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,20 @@ private fun CodeBlock.Builder.addQualifiers(resourceItem: ResourceItem): CodeBlo
val languageRegex = Regex("[a-z][a-z]")
terrakok marked this conversation as resolved.
Show resolved Hide resolved
val regionRegex = Regex("r[A-Z][A-Z]")

var alreadyHasLanguage: String? = null
var alreadyHasRegion: String? = null
var alreadyHasTheme: String? = null
var alreadyHasDensity: String? = null
val qualifiersMap = mutableMapOf<ClassName, String>()

fun repetitiveQualifiers(first: String, second: String) {
error("${resourceItem.path} contains repetitive qualifiers: $first and $second")
fun saveQualifier(className: ClassName, qualifier: String) {
qualifiersMap[className]?.let {
error("${resourceItem.path} contains repetitive qualifiers: $it and $qualifier")
}
qualifiersMap[className] = qualifier
}

resourceItem.qualifiers.sorted().forEach { q ->
resourceItem.qualifiers.forEach { q ->
when (q) {
"light",
"dark" -> {
alreadyHasTheme?.let { repetitiveQualifiers(it, q) }
alreadyHasTheme = q
add("%T.${q.uppercase()}, ", themeQualifier)
saveQualifier(themeQualifier, q)
}

"mdpi",
Expand All @@ -65,28 +63,37 @@ private fun CodeBlock.Builder.addQualifiers(resourceItem: ResourceItem): CodeBlo
"xxhdpi",
"xxxhdpi",
"ldpi" -> {
alreadyHasDensity?.let { repetitiveQualifiers(it, q) }
alreadyHasDensity = q
add("%T.${q.uppercase()}, ", densityQualifier)
saveQualifier(densityQualifier, q)
}

else -> when {
q.matches(languageRegex) -> {
alreadyHasLanguage?.let { repetitiveQualifiers(it, q) }
alreadyHasLanguage = q
add("%T(\"$q\"), ", languageQualifier)
saveQualifier(languageQualifier, q)
}

q.matches(regionRegex) -> {
alreadyHasRegion?.let { repetitiveQualifiers(it, q) }
alreadyHasRegion = q
add("%T(\"${q.takeLast(2)}\"), ", regionQualifier)
saveQualifier(regionQualifier, q)
}

else -> error("${resourceItem.path} contains unknown qualifier: $q")
}
}
}
qualifiersMap[themeQualifier]?.let { q -> add("%T.${q.uppercase()}, ", themeQualifier) }
qualifiersMap[densityQualifier]?.let { q -> add("%T.${q.uppercase()}, ", densityQualifier) }
qualifiersMap[languageQualifier]?.let { q -> add("%T(\"$q\"), ", languageQualifier) }
qualifiersMap[regionQualifier]?.let { q ->
val lang = qualifiersMap[languageQualifier]
if (lang == null) {
error("Region qualifier must be used only with language")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May we provide a context here for users to understand where they did mistake?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add dot to the end of the sentence

}
val langAndRegion = "$lang-$q"
if(!resourceItem.path.toString().contains(langAndRegion)) {
error("Region qualifier must be declared after language: '$langAndRegion'")
}
add("%T(\"${q.takeLast(2)}\"), ", regionQualifier)
}

return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ class ResourcesTest : GradlePluginTestBase() {
}

file("src/commonMain/resources/composeRes/drawable-ren").renameTo(
file("src/commonMain/resources/composeRes/drawable-rUS-en")
)

gradle("generateComposeResClass").checks {
check.logContains("""
Region qualifier must be declared after language: 'en-rUS'
""".trimIndent())
}

file("src/commonMain/resources/composeRes/drawable-rUS-en").renameTo(
file("src/commonMain/resources/composeRes/drawable-rUS")
)

gradle("generateComposeResClass").checks {
check.logContains("""
Region qualifier must be used only with language
""".trimIndent())
}

file("src/commonMain/resources/composeRes/drawable-rUS").renameTo(
file("src/commonMain/resources/composeRes/drawable-en-fr")
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ internal object Res {
public val vector: DrawableResource = DrawableResource(
"drawable:vector",
setOf(
ResourceItem(
setOf(LanguageQualifier("au"), RegionQualifier("US"), ),
"composeRes/drawable-au-rUS/vector.xml"
),
ResourceItem(
setOf(ThemeQualifier.DARK, LanguageQualifier("ge"), ),
"composeRes/drawable-dark-ge/vector.xml"
Expand All @@ -31,10 +35,6 @@ internal object Res {
setOf(LanguageQualifier("en"), ),
"composeRes/drawable-en/vector.xml"
),
ResourceItem(
setOf(RegionQualifier("US"), ),
"composeRes/drawable-rUS/vector.xml"
),
ResourceItem(
setOf(),
"composeRes/drawable/vector.xml"
Expand Down