Skip to content

Commit

Permalink
[resources] Add optional environment parameter to non-compose resourc…
Browse files Browse the repository at this point in the history
…e loading functions and function to get current compose environment
  • Loading branch information
terrakok committed Apr 19, 2024
1 parent 70c00f6 commit 4c1bb1e
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 resource The font resource.
* @param environment The optional resource environment.
* @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(
resource: FontResource,
environment: ResourceEnvironment = getResourceEnvironment()
): ByteArray {
val resourceItem = resource.getResourceItemByEnvironment(environment)
return DefaultResourceReader.read(resourceItem.path)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 resource The drawable resource.
* @param environment The optional resource environment.
* @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(
resource: DrawableResource,
environment: ResourceEnvironment = getResourceEnvironment()
): ByteArray {
val resourceItem = resource.getResourceItemByEnvironment(environment)
return DefaultResourceReader.read(resourceItem.path)
}

internal expect fun ByteArray.toImageBitmap(): ImageBitmap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ fun pluralStringResource(resource: PluralStringResource, quantity: Int): String
*
* @param resource The string resource to be used.
* @param quantity The quantity of the pluralization to use.
* @param environment The optional resource environment.
* @return The loaded string resource.
*
* @throws IllegalArgumentException If the provided ID or the pluralization is not found in the resource file.
*/
suspend fun getPluralString(resource: PluralStringResource, quantity: Int): String =
loadPluralString(resource, quantity, DefaultResourceReader, getResourceEnvironment())
suspend fun getPluralString(
resource: PluralStringResource,
quantity: Int,
environment: ResourceEnvironment = getResourceEnvironment()
): String = loadPluralString(resource, quantity, DefaultResourceReader, environment)

private suspend fun loadPluralString(
resource: PluralStringResource,
Expand Down Expand Up @@ -90,17 +94,22 @@ fun pluralStringResource(resource: PluralStringResource, quantity: Int, vararg f
* @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.
* @param environment The optional resource environment.
* @return The loaded string resource.
*
* @throws IllegalArgumentException If the provided ID or the pluralization is not found in the resource file.
*/
suspend fun getPluralString(resource: PluralStringResource, quantity: Int, vararg formatArgs: Any): String =
loadPluralString(
resource, quantity,
formatArgs.map { it.toString() },
DefaultResourceReader,
getResourceEnvironment(),
)
suspend fun getPluralString(
resource: PluralStringResource,
quantity: Int,
vararg formatArgs: Any,
environment: ResourceEnvironment = getResourceEnvironment()
): String = loadPluralString(
resource, quantity,
formatArgs.map { it.toString() },
DefaultResourceReader,
environment
)

private suspend fun loadPluralString(
resource: PluralStringResource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ fun stringArrayResource(resource: StringArrayResource): List<String> {
* Loads a list of strings using the specified string array resource.
*
* @param resource The string array resource to be used.
* @param environment The optional resource environment.
* @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(resource: StringArrayResource): List<String> =
loadStringArray(resource, DefaultResourceReader, getResourceEnvironment())
suspend fun getStringArray(
resource: StringArrayResource,
environment: ResourceEnvironment = getResourceEnvironment()
): List<String> = loadStringArray(resource, DefaultResourceReader, environment)

private suspend fun loadStringArray(
resource: StringArrayResource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ fun stringResource(resource: StringResource): String {
* Loads a string using the specified string resource.
*
* @param resource The string resource to be used.
* @param environment The optional resource environment.
* @return The loaded string resource.
*
* @throws IllegalArgumentException If the provided ID is not found in the resource file.
*/
suspend fun getString(resource: StringResource): String =
loadString(resource, DefaultResourceReader, getResourceEnvironment())
suspend fun getString(
resource: StringResource,
environment: ResourceEnvironment = getResourceEnvironment()
): String = loadString(resource, DefaultResourceReader, environment)

private suspend fun loadString(
resource: StringResource,
Expand Down Expand Up @@ -75,15 +78,20 @@ fun stringResource(resource: StringResource, vararg formatArgs: Any): String {
*
* @param resource The string resource to be used.
* @param formatArgs The arguments to be inserted into the formatted string.
* @param environment The optional resource environment.
* @return The formatted string resource.
*
* @throws IllegalArgumentException If the provided ID is not found in the resource file.
*/
suspend fun getString(resource: StringResource, vararg formatArgs: Any): String = loadString(
suspend fun getString(
resource: StringResource,
vararg formatArgs: Any,
environment: ResourceEnvironment = getResourceEnvironment()
): String = loadString(
resource,
formatArgs.map { it.toString() },
DefaultResourceReader,
getResourceEnvironment()
environment
)

private suspend fun loadString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(TestDrawableResource("1.png"))
assertEquals(946, imageBytes.size)
val fontBytes = getFontResourceBytes(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)
}
}

0 comments on commit 4c1bb1e

Please sign in to comment.