Skip to content

Commit

Permalink
Mark all resources public API as experimental. (#4146)
Browse files Browse the repository at this point in the history
And all public but only for generated code API as @InternalResourceApi
  • Loading branch information
terrakok committed Jan 19, 2024
1 parent 0e8a92a commit c31c761
Show file tree
Hide file tree
Showing 32 changed files with 81 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

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

@Before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.res.Configuration
import android.content.res.Resources
import java.util.*

@OptIn(InternalResourceApi::class)
internal actual fun getSystemEnvironment(): ResourceEnvironment {
val locale = Locale.getDefault()
val configuration = Resources.getSystem().configuration
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ import androidx.compose.ui.text.font.*
*
* @see Resource
*/
@OptIn(InternalResourceApi::class)
@ExperimentalResourceApi
@Immutable
class FontResource(id: String, items: Set<ResourceItem>): Resource(id, items)
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.
*/
@OptIn(InternalResourceApi::class)
@ExperimentalResourceApi
fun FontResource(path: String): FontResource = FontResource(
id = "FontResource:$path",
Expand All @@ -39,6 +43,7 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ 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.
*/
@OptIn(InternalResourceApi::class)
@ExperimentalResourceApi
@Immutable
class DrawableResource(id: String, items: Set<ResourceItem>) : Resource(id, items)
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.
*/
@OptIn(InternalResourceApi::class)
@ExperimentalResourceApi
fun DrawableResource(path: String): DrawableResource = DrawableResource(
id = "DrawableResource:$path",
Expand All @@ -42,6 +46,7 @@ fun DrawableResource(path: String): DrawableResource = DrawableResource(
* @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 @@ -62,6 +67,7 @@ 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 @@ -85,6 +91,7 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package org.jetbrains.compose.resources

interface Qualifier

@InternalResourceApi
data class LanguageQualifier(
val language: String
) : Qualifier

@InternalResourceApi
data class RegionQualifier(
val region: String
) : Qualifier

@InternalResourceApi
enum class ThemeQualifier : Qualifier {
LIGHT,
DARK;
Expand All @@ -21,6 +24,7 @@ enum class ThemeQualifier : Qualifier {
}

//https://developer.android.com/guide/topics/resources/providing-resources
@InternalResourceApi
enum class DensityQualifier(val dpi: Int) : Qualifier {
LDPI(120),
MDPI(160),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ 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(
sealed class Resource
@InternalResourceApi constructor(
internal val id: String,
internal val items: Set<ResourceItem>
) {
Expand All @@ -39,6 +41,7 @@ sealed class Resource(
* @property qualifiers The qualifiers of the resource item.
* @property path The path of the resource item.
*/
@InternalResourceApi
@Immutable
data class ResourceItem(
internal val qualifiers: Set<Qualifier>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.runtime.*
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.intl.Locale

@OptIn(InternalResourceApi::class)
internal data class ResourceEnvironment(
val language: LanguageQualifier,
val region: RegionQualifier,
Expand All @@ -17,6 +18,7 @@ internal interface ComposeEnvironment {
fun rememberEnvironment(): ResourceEnvironment
}

@OptIn(InternalResourceApi::class)
internal val DefaultComposeEnvironment = object : ComposeEnvironment {
@Composable
override fun rememberEnvironment(): ResourceEnvironment {
Expand Down Expand Up @@ -48,6 +50,7 @@ internal expect fun getSystemEnvironment(): ResourceEnvironment
*/
internal var getResourceEnvironment = ::getSystemEnvironment

@OptIn(InternalResourceApi::class, ExperimentalResourceApi::class)
internal fun Resource.getPathByEnvironment(environment: ResourceEnvironment): String {
//Priority of environments: https://developer.android.com/guide/topics/resources/providing-resources#table2
items.toList()
Expand All @@ -68,6 +71,7 @@ internal fun Resource.getPathByEnvironment(environment: ResourceEnvironment): St
}
}

@OptIn(InternalResourceApi::class)
private fun List<ResourceItem>.filterBy(qualifier: Qualifier): List<ResourceItem> {
//Android has a slightly different algorithm,
//but it provides the same result: https://developer.android.com/guide/topics/resources/providing-resources#BestMatch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.compose.resources

import androidx.compose.runtime.staticCompositionLocalOf

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ private val SimpleStringFormatRegex = Regex("""%(\d)\$[ds]""")
* @param key The key used to retrieve the string resource.
* @param items The set of resource items associated with the string resource.
*/
@OptIn(InternalResourceApi::class)
@ExperimentalResourceApi
@Immutable
class StringResource(id: String, val key: String, items: Set<ResourceItem>): Resource(id, items)
class StringResource
@InternalResourceApi constructor(id: String, val key: String, items: Set<ResourceItem>): Resource(id, items)

private sealed interface StringItem {
data class Value(val text: String) : StringItem
Expand Down Expand Up @@ -69,6 +72,7 @@ private suspend fun parseStringXml(path: String, resourceReader: ResourceReader)
*
* @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 @@ -86,9 +90,11 @@ 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)
private suspend fun loadString(
resource: StringResource,
resourceReader: ResourceReader,
Expand All @@ -110,6 +116,7 @@ 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 @@ -129,13 +136,15 @@ 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 All @@ -156,6 +165,7 @@ private suspend fun loadString(
*
* @throws IllegalStateException if the string array with the given ID is not found.
*/
@ExperimentalResourceApi
@Composable
fun stringArrayResource(resource: StringResource): List<String> {
val resourceReader = LocalResourceReader.current
Expand All @@ -173,9 +183,11 @@ fun stringArrayResource(resource: StringResource): List<String> {
*
* @throws IllegalStateException if the string array with the given ID is not found.
*/
@ExperimentalResourceApi
suspend fun getStringArray(resource: StringResource): List<String> =
loadStringArray(resource, DefaultResourceReader, getResourceEnvironment())

@OptIn(ExperimentalResourceApi::class)
private suspend fun loadStringArray(
resource: StringResource,
resourceReader: ResourceReader,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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() = runBlockingTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.compose.resources

import androidx.compose.runtime.Composable

@OptIn(InternalResourceApi::class)
internal fun getTestEnvironment() = ResourceEnvironment(
language = LanguageQualifier("en"),
region = RegionQualifier("US"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ expect class TestReturnType

expect fun runBlockingTest(block: suspend CoroutineScope.() -> Unit): TestReturnType

@OptIn(InternalResourceApi::class, ExperimentalResourceApi::class)
internal fun TestStringResource(key: String) = StringResource(
"STRING:$key",
key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.jetbrains.skiko.currentSystemTheme
import java.awt.Toolkit
import java.util.*

@OptIn(InternalResourceApi::class)
internal actual fun getSystemEnvironment(): ResourceEnvironment {
val locale = Locale.getDefault()
//FIXME: don't use skiko internals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

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

@Before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import platform.Foundation.*
import platform.UIKit.UIScreen
import platform.UIKit.UIUserInterfaceStyle

@OptIn(InternalResourceApi::class)
internal actual fun getSystemEnvironment(): ResourceEnvironment {
val locale = NSLocale.currentLocale()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import platform.Foundation.NSBundle
import platform.Foundation.NSFileManager
import platform.posix.memcpy

@OptIn(ExperimentalResourceApi::class)
@InternalResourceApi
actual suspend fun readResourceBytes(path: String): ByteArray {
val fileManager = NSFileManager.defaultManager()
// todo: support fallback path at bundle root?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.jetbrains.compose.resources
import org.jetbrains.compose.resources.vector.xmldom.*
import org.w3c.dom.parsing.DOMParser

@OptIn(ExperimentalResourceApi::class)
internal actual fun ByteArray.toXmlElement(): Element {
val xmlString = decodeToString()
val xmlDom = DOMParser().parseFromString(xmlString, "application/xml")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ private external class Intl {
}
}

@OptIn(InternalResourceApi::class)
internal actual fun getSystemEnvironment(): ResourceEnvironment {
val locale = Intl.Locale(window.navigator.language)
val isDarkTheme = window.matchMedia("(prefers-color-scheme: dark)").matches
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ private fun ArrayBuffer.toByteArray(): ByteArray =
Int8Array(this, 0, byteLength).unsafeCast<ByteArray>()

@OptIn(ExperimentalResourceApi::class)
@InternalResourceApi
actual suspend fun readResourceBytes(path: String): ByteArray {
val resPath = WebResourcesConfiguration.getResourcePath(path)
val response = window.fetch(resPath).await()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.jetbrains.compose.resources

private object JvmResourceReader

@OptIn(ExperimentalResourceApi::class)
@InternalResourceApi
actual suspend fun readResourceBytes(path: String): ByteArray {
val classLoader = Thread.currentThread().contextClassLoader ?: JvmResourceReader.javaClass.classLoader
val resource = classLoader.getResourceAsStream(path) ?: throw MissingResourceException(path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import platform.CoreGraphics.CGDisplayPixelsWide
import platform.CoreGraphics.CGDisplayScreenSize
import platform.Foundation.*

@OptIn(InternalResourceApi::class)
internal actual fun getSystemEnvironment(): ResourceEnvironment {
val locale = NSLocale.currentLocale()
val isDarkTheme = NSUserDefaults.standardUserDefaults.stringForKey("AppleInterfaceStyle") == "Dark"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import kotlinx.cinterop.usePinned
import platform.Foundation.NSFileManager
import platform.posix.memcpy

@OptIn(ExperimentalResourceApi::class)
@InternalResourceApi
actual suspend fun readResourceBytes(path: String): ByteArray {
val currentDirectoryPath = NSFileManager.defaultManager().currentDirectoryPath
val contentsAtPath = NSFileManager.defaultManager().run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package org.jetbrains.compose.resources.vector.xmldom

import org.jetbrains.compose.resources.ExperimentalResourceApi
import platform.Foundation.NSError
import platform.Foundation.NSString
import platform.Foundation.NSUTF8StringEncoding
Expand Down Expand Up @@ -57,6 +58,7 @@ private class ElementImpl(
override fun lookupPrefix(namespaceURI: String): String = prefixMap[namespaceURI] ?: ""
}

@OptIn(ExperimentalResourceApi::class)
@Suppress("CONFLICTING_OVERLOADS", "PARAMETER_NAME_CHANGED_ON_OVERRIDE")
private class DomXmlParser : NSObject(), NSXMLParserDelegateProtocol {

Expand Down

0 comments on commit c31c761

Please sign in to comment.