From 700393edf2847ba9caa383ed8eeb775b8eb0c7c4 Mon Sep 17 00:00:00 2001 From: Chanjung Kim Date: Wed, 20 Mar 2024 23:52:04 +0900 Subject: [PATCH] Rename QuantityStringResource to PluralStringResource for consistency --- .../compose/resources/StringResources.kt | 32 +++++++++---------- .../compose/resources/ComposeResourceTest.kt | 26 +++++++-------- .../jetbrains/compose/resources/TestUtils.kt | 2 +- .../compose/resources/GenerateResClassTask.kt | 8 ++--- .../compose/resources/ResourcesSpec.kt | 6 ++-- 5 files changed, 37 insertions(+), 37 deletions(-) diff --git a/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringResources.kt b/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringResources.kt index f84fa67a39..f7f52567b8 100644 --- a/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringResources.kt +++ b/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringResources.kt @@ -38,7 +38,7 @@ class StringResource @OptIn(InternalResourceApi::class) @ExperimentalResourceApi @Immutable -class QuantityStringResource +class PluralStringResource @InternalResourceApi constructor(id: String, val key: String, items: Set) : Resource(id, items) private sealed interface StringItem { @@ -197,10 +197,10 @@ private suspend fun loadString( */ @ExperimentalResourceApi @Composable -fun pluralStringResource(resource: QuantityStringResource, quantity: Int): String { +fun pluralStringResource(resource: PluralStringResource, quantity: Int): String { val resourceReader = LocalResourceReader.current val pluralStr by rememberResourceState(resource, quantity, { "" }) { env -> - loadQuantityString(resource, quantity, resourceReader, env) + loadPluralString(resource, quantity, resourceReader, env) } return pluralStr } @@ -215,12 +215,12 @@ fun pluralStringResource(resource: QuantityStringResource, quantity: Int): Strin * @throws IllegalArgumentException If the provided ID or the pluralization is not found in the resource file. */ @ExperimentalResourceApi -suspend fun getQuantityString(resource: QuantityStringResource, quantity: Int): String = - loadQuantityString(resource, quantity, DefaultResourceReader, getResourceEnvironment()) +suspend fun getPluralString(resource: PluralStringResource, quantity: Int): String = + loadPluralString(resource, quantity, DefaultResourceReader, getResourceEnvironment()) @OptIn(InternalResourceApi::class, ExperimentalResourceApi::class) -private suspend fun loadQuantityString( - resource: QuantityStringResource, +private suspend fun loadPluralString( + resource: PluralStringResource, quantity: Int, resourceReader: ResourceReader, environment: ResourceEnvironment @@ -251,13 +251,13 @@ private suspend fun loadQuantityString( */ @ExperimentalResourceApi @Composable -fun pluralStringResource(resource: QuantityStringResource, quantity: Int, vararg formatArgs: Any): String { +fun pluralStringResource(resource: PluralStringResource, quantity: Int, vararg formatArgs: Any): String { val resourceReader = LocalResourceReader.current val args = formatArgs.map { it.toString() } - val quantityStr by rememberResourceState(resource, quantity, args, { "" }) { env -> - loadQuantityString(resource, quantity, args, resourceReader, env) + val pluralStr by rememberResourceState(resource, quantity, args, { "" }) { env -> + loadPluralString(resource, quantity, args, resourceReader, env) } - return quantityStr + return pluralStr } /** @@ -271,8 +271,8 @@ fun pluralStringResource(resource: QuantityStringResource, quantity: Int, vararg * @throws IllegalArgumentException If the provided ID or the pluralization is not found in the resource file. */ @ExperimentalResourceApi -suspend fun getQuantityString(resource: QuantityStringResource, quantity: Int, vararg formatArgs: Any): String = - loadQuantityString( +suspend fun getPluralString(resource: PluralStringResource, quantity: Int, vararg formatArgs: Any): String = + loadPluralString( resource, quantity, formatArgs.map { it.toString() }, DefaultResourceReader, @@ -280,14 +280,14 @@ suspend fun getQuantityString(resource: QuantityStringResource, quantity: Int, v ) @OptIn(ExperimentalResourceApi::class) -private suspend fun loadQuantityString( - resource: QuantityStringResource, +private suspend fun loadPluralString( + resource: PluralStringResource, quantity: Int, args: List, resourceReader: ResourceReader, environment: ResourceEnvironment ): String { - val str = loadQuantityString(resource, quantity, resourceReader, environment) + val str = loadPluralString(resource, quantity, resourceReader, environment) return str.replaceWithArgs(args) } diff --git a/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.kt b/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.kt index 3d5a8152cf..212d9d81e9 100644 --- a/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.kt +++ b/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.kt @@ -145,9 +145,9 @@ class ComposeResourceTest { } @Test - fun testQuantityStringResourceCache() = runComposeUiTest { + fun testPluralStringResourceCache() = runComposeUiTest { val testResourceReader = TestResourceReader() - var res by mutableStateOf(TestQuantityStringResource("plurals")) + var res by mutableStateOf(TestPluralStringResource("plurals")) var quantity by mutableStateOf(0) var str = "" setContent { @@ -177,7 +177,7 @@ class ComposeResourceTest { assertEquals("other", str) assertEquals(3, quantity) - res = TestQuantityStringResource("another_plurals") + res = TestPluralStringResource("another_plurals") quantity = 0 waitForIdle() assertEquals("another other", str) @@ -188,13 +188,13 @@ class ComposeResourceTest { } @Test - fun testReadQuantityStringResource() = runComposeUiTest { + fun testReadPluralStringResource() = runComposeUiTest { var plurals = "" var another_plurals = "" setContent { CompositionLocalProvider(LocalComposeEnvironment provides TestComposeEnvironment) { - plurals = pluralStringResource(TestQuantityStringResource("plurals"), 1) - another_plurals = pluralStringResource(TestQuantityStringResource("another_plurals"), 1) + plurals = pluralStringResource(TestPluralStringResource("plurals"), 1) + another_plurals = pluralStringResource(TestPluralStringResource("another_plurals"), 1) } } waitForIdle() @@ -213,8 +213,8 @@ class ComposeResourceTest { var str2 = "" setContent { CompositionLocalProvider(LocalComposeEnvironment provides TestComposeEnvironment) { - str1 = pluralStringResource(TestQuantityStringResource("messages"), quantity, 3, arg) - str2 = pluralStringResource(TestQuantityStringResource("messages"), quantity, 5, arg) + str1 = pluralStringResource(TestPluralStringResource("messages"), quantity, 3, arg) + str2 = pluralStringResource(TestPluralStringResource("messages"), quantity, 5, arg) } } waitForIdle() @@ -233,11 +233,11 @@ class ComposeResourceTest { } @Test - fun testLoadQuantityStringResource() = runTest { - assertEquals("one", getQuantityString(TestQuantityStringResource("plurals"), 1)) - assertEquals("other", getQuantityString(TestQuantityStringResource("plurals"), 5)) - assertEquals("another one", getQuantityString(TestQuantityStringResource("another_plurals"), 1)) - assertEquals("another other", getQuantityString(TestQuantityStringResource("another_plurals"), 5)) + fun testLoadPluralStringResource() = runTest { + assertEquals("one", getPluralString(TestPluralStringResource("plurals"), 1)) + assertEquals("other", getPluralString(TestPluralStringResource("plurals"), 5)) + assertEquals("another one", getPluralString(TestPluralStringResource("another_plurals"), 1)) + assertEquals("another other", getPluralString(TestPluralStringResource("another_plurals"), 5)) } @Test diff --git a/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/TestUtils.kt b/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/TestUtils.kt index f8b2b4e116..666a0fb8c6 100644 --- a/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/TestUtils.kt +++ b/components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/TestUtils.kt @@ -12,7 +12,7 @@ internal fun TestStringResource(key: String) = StringResource( ) @OptIn(InternalResourceApi::class, ExperimentalResourceApi::class) -internal fun TestQuantityStringResource(key: String) = QuantityStringResource( +internal fun TestPluralStringResource(key: String) = PluralStringResource( "PLURALS:$key", key, setOf(ResourceItem(emptySet(), "strings.xml")) diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt index 3151ccccee..40a981bc25 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/GenerateResClassTask.kt @@ -102,11 +102,11 @@ internal abstract class GenerateResClassTask : DefaultTask() { if (typeString == "values" && file.name.equals("strings.xml", true)) { val stringIds = getStringIds(file) - val quantityStringIds = getQuantityStringIds(file) + val pluralStringIds = getPluralStringIds(file) return stringIds.map { strId -> ResourceItem(ResourceType.STRING, qualifiers, strId.asUnderscoredIdentifier(), path) - } + quantityStringIds.map { quantityStrId -> - ResourceItem(ResourceType.QUANTITY_STRING, qualifiers, quantityStrId.asUnderscoredIdentifier(), path) + } + pluralStringIds.map { pluralStrId -> + ResourceItem(ResourceType.PLURAL_STRING, qualifiers, pluralStrId.asUnderscoredIdentifier(), path) } } @@ -124,7 +124,7 @@ internal abstract class GenerateResClassTask : DefaultTask() { return ids.toSet() } - private fun getQuantityStringIds(stringsXml: File): Set { + private fun getPluralStringIds(stringsXml: File): Set { val doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(stringsXml) val items = doc.getElementsByTagName("resources").item(0).childNodes val ids = List(items.length) { items.item(it) } diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesSpec.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesSpec.kt index 2edfb76c95..87e2d38dbd 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesSpec.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesSpec.kt @@ -9,7 +9,7 @@ import kotlin.io.path.invariantSeparatorsPathString internal enum class ResourceType(val typeName: String) { DRAWABLE("drawable"), STRING("string"), - QUANTITY_STRING("plurals"), + PLURAL_STRING("plurals"), FONT("font"); override fun toString(): String = typeName @@ -32,7 +32,7 @@ internal data class ResourceItem( private fun ResourceType.getClassName(): ClassName = when (this) { ResourceType.DRAWABLE -> ClassName("org.jetbrains.compose.resources", "DrawableResource") ResourceType.STRING -> ClassName("org.jetbrains.compose.resources", "StringResource") - ResourceType.QUANTITY_STRING -> ClassName("org.jetbrains.compose.resources", "QuantityStringResource") + ResourceType.PLURAL_STRING -> ClassName("org.jetbrains.compose.resources", "PluralStringResource") ResourceType.FONT -> ClassName("org.jetbrains.compose.resources", "FontResource") } @@ -227,7 +227,7 @@ private fun getChunkFileSpec( CodeBlock.builder() .add("return %T(\n", type.getClassName()).withIndent { add("\"${type}:${resName}\",") - if (type == ResourceType.STRING || type == ResourceType.QUANTITY_STRING) add(" \"$resName\",") + if (type == ResourceType.STRING || type == ResourceType.PLURAL_STRING) add(" \"$resName\",") withIndent { add("\nsetOf(\n").withIndent { items.forEach { item ->