Browse Source

[resources] Add optional environment parameter to non-compose resource loading functions and function to get current compose environment

pull/4651/head
Konstantin Tskhovrebov 4 weeks ago
parent
commit
cd2646029a
  1. 21
      components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/FontResources.kt
  2. 21
      components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ImageResources.kt
  3. 39
      components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/PluralStringResources.kt
  4. 24
      components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.kt
  5. 14
      components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringArrayResources.kt
  6. 33
      components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringResources.kt
  7. 25
      components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.kt

21
components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/FontResources.kt

@ -36,18 +36,17 @@ expect fun Font(
): Font
/**
* Retrieves an ByteArray using the specified font resource.
* Retrieves the byte array of the font resource.
*
* @param resource The font resource to be used.
* @return The ByteArray loaded from the resource.
* @param environment The optional resource environment.
* @param resource The font resource.
* @return The byte array representing the font resource.
*/
@ExperimentalResourceApi
@Composable
fun getFontResourceBytes(resource: FontResource): ByteArray {
val resourceReader = LocalResourceReader.current
val bytes by rememberResourceState(resource, resourceReader, { ByteArray(0) }) { env ->
val item = resource.getResourceItemByEnvironment(env)
resourceReader.read(item.path)
}
return bytes
suspend fun getFontResourceBytes(
environment: ResourceEnvironment = getResourceEnvironment(),
resource: FontResource
): ByteArray {
val resourceItem = resource.getResourceItemByEnvironment(environment)
return DefaultResourceReader.read(resourceItem.path)
}

21
components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ImageResources.kt

@ -110,20 +110,19 @@ private fun svgPainter(resource: DrawableResource): Painter {
}
/**
* Retrieves an ByteArray using the specified drawable resource.
* Retrieves the byte array of the drawable resource.
*
* @param resource The drawable resource to be used.
* @return The ByteArray loaded from the resource.
* @param environment The optional resource environment.
* @param resource The drawable resource.
* @return The byte array representing the drawable resource.
*/
@ExperimentalResourceApi
@Composable
fun getDrawableResourceBytes(resource: DrawableResource): ByteArray {
val resourceReader = LocalResourceReader.current
val bytes by rememberResourceState(resource, resourceReader, { ByteArray(0) }) { env ->
val item = resource.getResourceItemByEnvironment(env)
resourceReader.read(item.path)
}
return bytes
suspend fun getDrawableResourceBytes(
environment: ResourceEnvironment = getResourceEnvironment(),
resource: DrawableResource
): ByteArray {
val resourceItem = resource.getResourceItemByEnvironment(environment)
return DefaultResourceReader.read(resourceItem.path)
}
internal expect fun ByteArray.toImageBitmap(): ImageBitmap

39
components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/PluralStringResources.kt

@ -45,6 +45,22 @@ fun pluralStringResource(resource: PluralStringResource, quantity: Int): String
suspend fun getPluralString(resource: PluralStringResource, quantity: Int): String =
loadPluralString(resource, quantity, DefaultResourceReader, getResourceEnvironment())
/**
* Loads a string using the specified string resource.
*
* @param environment The resource environment.
* @param resource The string resource to be used.
* @param quantity The quantity of the pluralization to use.
* @return The loaded string resource.
*
* @throws IllegalArgumentException If the provided ID or the pluralization is not found in the resource file.
*/
suspend fun getPluralString(
environment: ResourceEnvironment,
resource: PluralStringResource,
quantity: Int
): String = loadPluralString(resource, quantity, DefaultResourceReader, environment)
private suspend fun loadPluralString(
resource: PluralStringResource,
quantity: Int,
@ -102,6 +118,29 @@ suspend fun getPluralString(resource: PluralStringResource, quantity: Int, varar
getResourceEnvironment(),
)
/**
* Loads a string using the specified string resource.
*
* @param environment The resource environment.
* @param resource The string resource to be used.
* @param quantity The quantity of the pluralization to use.
* @param formatArgs The arguments to be inserted into the formatted string.
* @return The loaded string resource.
*
* @throws IllegalArgumentException If the provided ID or the pluralization is not found in the resource file.
*/
suspend fun getPluralString(
environment: ResourceEnvironment,
resource: PluralStringResource,
quantity: Int,
vararg formatArgs: Any
): String = loadPluralString(
resource, quantity,
formatArgs.map { it.toString() },
DefaultResourceReader,
environment
)
private suspend fun loadPluralString(
resource: PluralStringResource,
quantity: Int,

24
components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.kt

@ -5,11 +5,11 @@ import androidx.compose.runtime.*
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.intl.Locale
internal data class ResourceEnvironment(
val language: LanguageQualifier,
val region: RegionQualifier,
val theme: ThemeQualifier,
val density: DensityQualifier
data class ResourceEnvironment internal constructor(
internal val language: LanguageQualifier,
internal val region: RegionQualifier,
internal val theme: ThemeQualifier,
internal val density: DensityQualifier
)
internal interface ComposeEnvironment {
@ -39,6 +39,20 @@ internal val DefaultComposeEnvironment = object : ComposeEnvironment {
//ComposeEnvironment provider will be overridden for tests
internal val LocalComposeEnvironment = staticCompositionLocalOf { DefaultComposeEnvironment }
/**
* Returns an instance of [ResourceEnvironment].
*
* The [ResourceEnvironment] class represents the environment for resources.
*
* @return An instance of [ResourceEnvironment] representing the current environment.
*/
@ExperimentalResourceApi
@Composable
fun rememberResourceEnvironment(): ResourceEnvironment {
val composeEnvironment = LocalComposeEnvironment.current
return composeEnvironment.rememberEnvironment()
}
internal expect fun getSystemEnvironment(): ResourceEnvironment
//the function reference will be overridden for tests

14
components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringArrayResources.kt

@ -48,6 +48,20 @@ fun stringArrayResource(resource: StringArrayResource): List<String> {
suspend fun getStringArray(resource: StringArrayResource): List<String> =
loadStringArray(resource, DefaultResourceReader, getResourceEnvironment())
/**
* Loads a list of strings using the specified string array resource.
*
* @param environment The resource environment.
* @param resource The string array resource to be used.
* @return A list of strings representing the items in the string array.
*
* @throws IllegalStateException if the string array with the given ID is not found.
*/
suspend fun getStringArray(
environment: ResourceEnvironment,
resource: StringArrayResource
): List<String> = loadStringArray(resource, DefaultResourceReader, environment)
private suspend fun loadStringArray(
resource: StringArrayResource,
resourceReader: ResourceReader,

33
components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringResources.kt

@ -41,6 +41,18 @@ fun stringResource(resource: StringResource): String {
suspend fun getString(resource: StringResource): String =
loadString(resource, DefaultResourceReader, getResourceEnvironment())
/**
* Loads a string using the specified string resource.
*
* @param environment The resource environment.
* @param resource The string resource to be used.
* @return The loaded string resource.
*
* @throws IllegalArgumentException If the provided ID is not found in the resource file.
*/
suspend fun getString(environment: ResourceEnvironment, resource: StringResource): String =
loadString(resource, DefaultResourceReader, environment)
private suspend fun loadString(
resource: StringResource,
resourceReader: ResourceReader,
@ -86,6 +98,27 @@ suspend fun getString(resource: StringResource, vararg formatArgs: Any): String
getResourceEnvironment()
)
/**
* Loads a formatted string using the specified string resource and arguments.
*
* @param environment The resource environment.
* @param resource The string resource to be used.
* @param formatArgs The arguments to be inserted into the formatted string.
* @return The formatted string resource.
*
* @throws IllegalArgumentException If the provided ID is not found in the resource file.
*/
suspend fun getString(
environment: ResourceEnvironment,
resource: StringResource,
vararg formatArgs: Any
): String = loadString(
resource,
formatArgs.map { it.toString() },
DefaultResourceReader,
environment
)
private suspend fun loadString(
resource: StringResource,
args: List<String>,

25
components/resources/library/src/commonTest/kotlin/org/jetbrains/compose/resources/ComposeResourceTest.kt

@ -6,7 +6,6 @@ import androidx.compose.runtime.*
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.runComposeUiTest
import kotlinx.coroutines.test.runTest
import org.jetbrains.skiko.URIManager
import kotlin.test.*
@OptIn(ExperimentalTestApi::class, InternalResourceApi::class)
@ -307,17 +306,23 @@ class ComposeResourceTest {
@OptIn(ExperimentalResourceApi::class)
@Test
fun testGetResourceBytes() = runComposeUiTest {
var imageBytes = ByteArray(0)
var fontBytes = ByteArray(0)
fun testGetResourceBytes() = runTest {
val imageBytes = getDrawableResourceBytes(resource = TestDrawableResource("1.png"))
assertEquals(946, imageBytes.size)
val fontBytes = getFontResourceBytes(resource = TestFontResource("font_awesome.otf"))
assertEquals(134808, fontBytes.size)
}
@OptIn(ExperimentalResourceApi::class)
@Test
fun testGetResourceEnvironment() = runComposeUiTest {
var environment: ResourceEnvironment? = null
setContent {
CompositionLocalProvider(LocalComposeEnvironment provides TestComposeEnvironment) {
imageBytes = getDrawableResourceBytes(TestDrawableResource("1.png"))
fontBytes = getFontResourceBytes(TestFontResource("font_awesome.otf"))
}
environment = rememberResourceEnvironment()
}
waitForIdle()
assertEquals(946, imageBytes.size)
assertEquals(134808, fontBytes.size)
val systemEnvironment = getSystemEnvironment()
assertEquals(systemEnvironment, environment)
}
}

Loading…
Cancel
Save