Skip to content

Commit

Permalink
Delete the experimental mark from the stable resources library API (#…
Browse files Browse the repository at this point in the history
…4623)

And remove internal testing methods
  • Loading branch information
terrakok committed Apr 18, 2024
1 parent 6b9b473 commit 2d225f3
Show file tree
Hide file tree
Showing 44 changed files with 27 additions and 185 deletions.
Expand Up @@ -5,13 +5,12 @@

import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.window.CanvasBasedWindow
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.configureWebResources
import org.jetbrains.compose.resources.demo.shared.UseResources
import org.jetbrains.skiko.wasm.onWasmReady


@OptIn(ExperimentalComposeUiApi::class, ExperimentalResourceApi::class)
@OptIn(ExperimentalComposeUiApi::class)
fun main() {
configureWebResources {
// Not necessary - It's the same as the default. We add it here just to present this feature.
Expand Down
@@ -1,10 +1,9 @@
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.window.CanvasBasedWindow
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.configureWebResources
import org.jetbrains.compose.resources.demo.shared.UseResources

@OptIn(ExperimentalComposeUiApi::class, ExperimentalResourceApi::class)
@OptIn(ExperimentalComposeUiApi::class)
fun main() {
configureWebResources {
// Not necessary - It's the same as the default. We add it here just to present this feature.
Expand Down
Expand Up @@ -5,7 +5,6 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.*

@ExperimentalResourceApi
@Composable
actual fun Font(resource: FontResource, weight: FontWeight, style: FontStyle): Font {
val environment = LocalComposeEnvironment.current.rememberEnvironment()
Expand Down
Expand Up @@ -19,7 +19,6 @@ internal actual fun getPlatformResourceReader(): ResourceReader = object : Resou
return result
}

@OptIn(ExperimentalResourceApi::class)
override fun getUri(path: String): String {
val classLoader = getClassLoader()
val resource = classLoader.getResource(path) ?: run {
Expand All @@ -31,7 +30,6 @@ internal actual fun getPlatformResourceReader(): ResourceReader = object : Resou
return resource.toURI().toString()
}

@OptIn(ExperimentalResourceApi::class)
private fun getResourceAsStream(path: String): InputStream {
val classLoader = getClassLoader()
val resource = classLoader.getResourceAsStream(path) ?: run {
Expand Down
Expand Up @@ -12,23 +12,10 @@ import androidx.compose.ui.text.font.*
*
* @see Resource
*/
@ExperimentalResourceApi
@Immutable
class FontResource
@InternalResourceApi constructor(id: String, items: Set<ResourceItem>): Resource(id, items)

/**
* Creates an [FontResource] object with the specified path.
*
* @param path The path to the font resource file.
* @return A new [FontResource] object.
*/
@ExperimentalResourceApi
fun FontResource(path: String): FontResource = FontResource(
id = "FontResource:$path",
items = setOf(ResourceItem(emptySet(), path, -1, -1))
)

/**
* Creates a font using the specified font resource, weight, and style.
*
Expand All @@ -41,7 +28,6 @@ fun FontResource(path: String): FontResource = FontResource(
* @throws NotFoundException if the specified resource ID is not found.
*/
@Composable
@ExperimentalResourceApi
expect fun Font(
resource: FontResource,
weight: FontWeight = FontWeight.Normal,
Expand Down
Expand Up @@ -21,31 +21,17 @@ import org.jetbrains.compose.resources.vector.xmldom.Element
* @param id The unique identifier of the drawable resource.
* @param items The set of resource items associated with the image resource.
*/
@ExperimentalResourceApi
@Immutable
class DrawableResource
@InternalResourceApi constructor(id: String, items: Set<ResourceItem>) : Resource(id, items)

/**
* Creates an [DrawableResource] object with the specified path.
*
* @param path The path of the drawable resource.
* @return An [DrawableResource] object.
*/
@ExperimentalResourceApi
fun DrawableResource(path: String): DrawableResource = DrawableResource(
id = "DrawableResource:$path",
items = setOf(ResourceItem(emptySet(), path, -1, -1))
)

/**
* Retrieves a [Painter] using the specified drawable resource.
* Automatically select a type of the Painter depending on the file extension.
*
* @param resource The drawable resource to be used.
* @return The [Painter] loaded from the resource.
*/
@ExperimentalResourceApi
@Composable
fun painterResource(resource: DrawableResource): Painter {
val environment = LocalComposeEnvironment.current.rememberEnvironment()
Expand All @@ -67,7 +53,6 @@ private val emptyImageBitmap: ImageBitmap by lazy { ImageBitmap(1, 1) }
* @param resource The drawable resource to be used.
* @return The ImageBitmap loaded from the resource.
*/
@ExperimentalResourceApi
@Composable
fun imageResource(resource: DrawableResource): ImageBitmap {
val resourceReader = LocalResourceReader.current
Expand All @@ -91,7 +76,6 @@ private val emptyImageVector: ImageVector by lazy {
* @param resource The drawable resource to be used.
* @return The ImageVector loaded from the resource.
*/
@ExperimentalResourceApi
@Composable
fun vectorResource(resource: DrawableResource): ImageVector {
val resourceReader = LocalResourceReader.current
Expand Down
Expand Up @@ -11,7 +11,6 @@ import org.jetbrains.compose.resources.plural.PluralRuleList
* @param key The key used to retrieve the string resource.
* @param items The set of resource items associated with the string resource.
*/
@ExperimentalResourceApi
@Immutable
class PluralStringResource
@InternalResourceApi constructor(id: String, val key: String, items: Set<ResourceItem>) : Resource(id, items)
Expand All @@ -25,7 +24,6 @@ class PluralStringResource
*
* @throws IllegalArgumentException If the provided ID or the pluralization is not found in the resource file.
*/
@ExperimentalResourceApi
@Composable
fun pluralStringResource(resource: PluralStringResource, quantity: Int): String {
val resourceReader = LocalResourceReader.current
Expand All @@ -44,11 +42,9 @@ fun pluralStringResource(resource: PluralStringResource, quantity: Int): String
*
* @throws IllegalArgumentException If the provided ID or the pluralization is not found in the resource file.
*/
@ExperimentalResourceApi
suspend fun getPluralString(resource: PluralStringResource, quantity: Int): String =
loadPluralString(resource, quantity, DefaultResourceReader, getResourceEnvironment())

@OptIn(InternalResourceApi::class, ExperimentalResourceApi::class)
private suspend fun loadPluralString(
resource: PluralStringResource,
quantity: Int,
Expand Down Expand Up @@ -78,7 +74,6 @@ private suspend fun loadPluralString(
*
* @throws IllegalArgumentException If the provided ID or the pluralization is not found in the resource file.
*/
@ExperimentalResourceApi
@Composable
fun pluralStringResource(resource: PluralStringResource, quantity: Int, vararg formatArgs: Any): String {
val resourceReader = LocalResourceReader.current
Expand All @@ -99,7 +94,6 @@ fun pluralStringResource(resource: PluralStringResource, quantity: Int, vararg f
*
* @throws IllegalArgumentException If the provided ID or the pluralization is not found in the resource file.
*/
@ExperimentalResourceApi
suspend fun getPluralString(resource: PluralStringResource, quantity: Int, vararg formatArgs: Any): String =
loadPluralString(
resource, quantity,
Expand All @@ -108,7 +102,6 @@ suspend fun getPluralString(resource: PluralStringResource, quantity: Int, varar
getResourceEnvironment(),
)

@OptIn(ExperimentalResourceApi::class)
private suspend fun loadPluralString(
resource: PluralStringResource,
quantity: Int,
Expand Down
Expand Up @@ -14,7 +14,6 @@ annotation class InternalResourceApi
* @property id The ID of the resource.
* @property items The set of resource items associated with the resource.
*/
@ExperimentalResourceApi
@Immutable
sealed class Resource
@InternalResourceApi constructor(
Expand Down
Expand Up @@ -48,7 +48,7 @@ internal expect fun getSystemEnvironment(): ResourceEnvironment
*/
internal var getResourceEnvironment = ::getSystemEnvironment

@OptIn(InternalResourceApi::class, ExperimentalResourceApi::class)
@OptIn(InternalResourceApi::class)
internal fun Resource.getResourceItemByEnvironment(environment: ResourceEnvironment): ResourceItem {
//Priority of environments: https://developer.android.com/guide/topics/resources/providing-resources#table2
items.toList()
Expand Down
Expand Up @@ -2,7 +2,6 @@ package org.jetbrains.compose.resources

import androidx.compose.runtime.staticCompositionLocalOf

@ExperimentalResourceApi
class MissingResourceException(path: String) : Exception("Missing resource with path: $path")

/**
Expand Down
Expand Up @@ -16,7 +16,6 @@ import org.jetbrains.compose.resources.vector.xmldom.NodeList
* @param key The key used to retrieve the string array resource.
* @param items The set of resource items associated with the string array resource.
*/
@ExperimentalResourceApi
@Immutable
class StringArrayResource
@InternalResourceApi constructor(id: String, val key: String, items: Set<ResourceItem>) : Resource(id, items)
Expand All @@ -29,7 +28,6 @@ class StringArrayResource
*
* @throws IllegalStateException if the string array with the given ID is not found.
*/
@ExperimentalResourceApi
@Composable
fun stringArrayResource(resource: StringArrayResource): List<String> {
val resourceReader = LocalResourceReader.current
Expand All @@ -47,11 +45,9 @@ fun stringArrayResource(resource: StringArrayResource): List<String> {
*
* @throws IllegalStateException if the string array with the given ID is not found.
*/
@ExperimentalResourceApi
suspend fun getStringArray(resource: StringArrayResource): List<String> =
loadStringArray(resource, DefaultResourceReader, getResourceEnvironment())

@OptIn(ExperimentalResourceApi::class, InternalResourceApi::class)
private suspend fun loadStringArray(
resource: StringArrayResource,
resourceReader: ResourceReader,
Expand Down
Expand Up @@ -9,7 +9,6 @@ import androidx.compose.runtime.*
* @param key The key used to retrieve the string resource.
* @param items The set of resource items associated with the string resource.
*/
@ExperimentalResourceApi
@Immutable
class StringResource
@InternalResourceApi constructor(id: String, val key: String, items: Set<ResourceItem>) : Resource(id, items)
Expand All @@ -22,7 +21,6 @@ class StringResource
*
* @throws IllegalArgumentException If the provided ID is not found in the resource file.
*/
@ExperimentalResourceApi
@Composable
fun stringResource(resource: StringResource): String {
val resourceReader = LocalResourceReader.current
Expand All @@ -40,11 +38,9 @@ fun stringResource(resource: StringResource): String {
*
* @throws IllegalArgumentException If the provided ID is not found in the resource file.
*/
@ExperimentalResourceApi
suspend fun getString(resource: StringResource): String =
loadString(resource, DefaultResourceReader, getResourceEnvironment())

@OptIn(ExperimentalResourceApi::class, InternalResourceApi::class)
private suspend fun loadString(
resource: StringResource,
resourceReader: ResourceReader,
Expand All @@ -64,7 +60,6 @@ private suspend fun loadString(
*
* @throws IllegalArgumentException If the provided ID is not found in the resource file.
*/
@ExperimentalResourceApi
@Composable
fun stringResource(resource: StringResource, vararg formatArgs: Any): String {
val resourceReader = LocalResourceReader.current
Expand All @@ -84,15 +79,13 @@ fun stringResource(resource: StringResource, vararg formatArgs: Any): String {
*
* @throws IllegalArgumentException If the provided ID is not found in the resource file.
*/
@ExperimentalResourceApi
suspend fun getString(resource: StringResource, vararg formatArgs: Any): String = loadString(
resource,
formatArgs.map { it.toString() },
DefaultResourceReader,
getResourceEnvironment()
)

@OptIn(ExperimentalResourceApi::class)
private suspend fun loadString(
resource: StringResource,
args: List<String>,
Expand Down
@@ -1,9 +1,6 @@
package org.jetbrains.compose.resources.vector.xmldom

import org.jetbrains.compose.resources.ExperimentalResourceApi

/**
* Error throw when parsed XML is malformed
*/
@ExperimentalResourceApi
class MalformedXMLException(message: String?) : Exception(message)
Expand Up @@ -9,7 +9,7 @@ import kotlinx.coroutines.test.runTest
import org.jetbrains.skiko.URIManager
import kotlin.test.*

@OptIn(ExperimentalTestApi::class, ExperimentalResourceApi::class, InternalResourceApi::class)
@OptIn(ExperimentalTestApi::class, InternalResourceApi::class)
class ComposeResourceTest {

init {
Expand All @@ -20,7 +20,7 @@ class ComposeResourceTest {

@Test
fun testCountRecompositions() = runComposeUiTest {
var res by mutableStateOf(DrawableResource("1.png"))
var res by mutableStateOf(TestDrawableResource("1.png"))
val recompositionsCounter = RecompositionsCounter()
setContent {
CompositionLocalProvider(LocalComposeEnvironment provides TestComposeEnvironment) {
Expand All @@ -31,15 +31,15 @@ class ComposeResourceTest {
}
}
waitForIdle()
res = DrawableResource("2.png")
res = TestDrawableResource("2.png")
waitForIdle()
assertEquals(2, recompositionsCounter.count)
}

@Test
fun testImageResourceCache() = runComposeUiTest {
val testResourceReader = TestResourceReader()
var res by mutableStateOf(DrawableResource("1.png"))
var res by mutableStateOf(TestDrawableResource("1.png"))
setContent {
CompositionLocalProvider(
LocalResourceReader provides testResourceReader,
Expand All @@ -49,9 +49,9 @@ class ComposeResourceTest {
}
}
waitForIdle()
res = DrawableResource("2.png")
res = TestDrawableResource("2.png")
waitForIdle()
res = DrawableResource("1.png")
res = TestDrawableResource("1.png")
waitForIdle()

assertEquals(
Expand Down
Expand Up @@ -10,16 +10,15 @@ import org.jetbrains.compose.resources.ThemeQualifier.DARK
import org.jetbrains.compose.resources.ThemeQualifier.LIGHT
import kotlin.test.*

@OptIn(ExperimentalResourceApi::class, InternalResourceApi::class)
class ResourceTest {
@Test
fun testResourceEquals() {
assertEquals(DrawableResource("a"), DrawableResource("a"))
assertEquals(TestDrawableResource("a"), TestDrawableResource("a"))
}

@Test
fun testResourceNotEquals() {
assertNotEquals(DrawableResource("a"), DrawableResource("b"))
assertNotEquals(TestDrawableResource("a"), TestDrawableResource("b"))
}

@Test
Expand Down
Expand Up @@ -17,27 +17,29 @@ private val cvrMap: Map<String, ResourceItem> = mapOf(
"str_arr" to ResourceItem(setOf(), "strings.cvr", 211, 47),
)

@OptIn(ExperimentalResourceApi::class)
internal fun TestStringResource(key: String) = StringResource(
"STRING:$key",
key,
setOf(cvrMap[key] ?: error("String ID=`$key` is not found!"))
)

@OptIn(ExperimentalResourceApi::class)
internal fun TestStringArrayResource(key: String) = StringArrayResource(
"STRING:$key",
key,
setOf(cvrMap[key] ?: error("String ID=`$key` is not found!"))
)

@OptIn(ExperimentalResourceApi::class)
internal fun TestPluralStringResource(key: String) = PluralStringResource(
"PLURALS:$key",
key,
setOf(cvrMap[key] ?: error("String ID=`$key` is not found!"))
)

internal fun TestDrawableResource(path: String) = DrawableResource(
path,
setOf(ResourceItem(emptySet(), path, -1, -1))
)

internal fun parsePluralSamples(samples: String): List<Int> {
return samples.split(',').flatMap {
val range = it.trim()
Expand Down

0 comments on commit 2d225f3

Please sign in to comment.