diff --git a/gradle.properties b/gradle.properties index 1238c435..056937b5 100755 --- a/gradle.properties +++ b/gradle.properties @@ -7,6 +7,7 @@ kotlin.mpp.stability.nowarn=true kotlin.native.enableDependencyPropagation=false kotlin.mpp.enableGranularSourceSetsMetadata=true kotlin.mpp.enableCompatibilityMetadataVariant=true +org.jetbrains.compose.experimental.jscanvas.enabled=true android.useAndroidX=true diff --git a/resources-compose/build.gradle.kts b/resources-compose/build.gradle.kts index a168b31b..ae4f8893 100644 --- a/resources-compose/build.gradle.kts +++ b/resources-compose/build.gradle.kts @@ -27,12 +27,21 @@ android { } kotlin { + android() jvm() + js(IR) { + browser() + } sourceSets { commonMain { dependencies { api(projects.resources) implementation(compose.runtime) + } + } + + named("androidMain") { + dependencies { implementation(compose.foundation) } } @@ -40,6 +49,7 @@ kotlin { named("jvmMain") { dependencies { api(compose.desktop.common) + implementation(compose.foundation) implementation(compose.desktop.currentOs) } } diff --git a/resources-compose/src/androidMain/kotlin/dev/icerock/moko/resources/compose/AssetResource.kt b/resources-compose/src/androidMain/kotlin/dev/icerock/moko/resources/compose/AssetResource.kt new file mode 100644 index 00000000..2cb1195c --- /dev/null +++ b/resources-compose/src/androidMain/kotlin/dev/icerock/moko/resources/compose/AssetResource.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2023 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +package dev.icerock.moko.resources.compose + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.State +import androidx.compose.runtime.produceState +import androidx.compose.ui.platform.LocalContext +import dev.icerock.moko.resources.AssetResource + +@Composable +actual fun AssetResource.readTextAsState(): State { + val context = LocalContext.current + return produceState("") { + value = readText(context) + } +} diff --git a/resources-compose/src/commonMain/kotlin/dev/icerock/moko/resources/compose/AssetResource.kt b/resources-compose/src/commonMain/kotlin/dev/icerock/moko/resources/compose/AssetResource.kt new file mode 100644 index 00000000..a25ad619 --- /dev/null +++ b/resources-compose/src/commonMain/kotlin/dev/icerock/moko/resources/compose/AssetResource.kt @@ -0,0 +1,12 @@ +/* + * Copyright 2023 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +package dev.icerock.moko.resources.compose + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.State +import dev.icerock.moko.resources.AssetResource + +@Composable +expect fun AssetResource.readTextAsState(): State diff --git a/resources-compose/src/jsMain/kotlin/dev/icerock/moko/resources/compose/AssetResource.kt b/resources-compose/src/jsMain/kotlin/dev/icerock/moko/resources/compose/AssetResource.kt new file mode 100644 index 00000000..80b21131 --- /dev/null +++ b/resources-compose/src/jsMain/kotlin/dev/icerock/moko/resources/compose/AssetResource.kt @@ -0,0 +1,17 @@ +/* + * Copyright 2023 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +package dev.icerock.moko.resources.compose + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.State +import androidx.compose.runtime.produceState +import dev.icerock.moko.resources.AssetResource + +@Composable +actual fun AssetResource.readTextAsState(): State { + return produceState("") { + value = getText() + } +} diff --git a/resources-compose/src/jsMain/kotlin/dev/icerock/moko/resources/compose/StringDescExt.kt b/resources-compose/src/jsMain/kotlin/dev/icerock/moko/resources/compose/StringDescExt.kt new file mode 100644 index 00000000..73fae8c4 --- /dev/null +++ b/resources-compose/src/jsMain/kotlin/dev/icerock/moko/resources/compose/StringDescExt.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2023 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +package dev.icerock.moko.resources.compose + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.produceState +import dev.icerock.moko.resources.desc.StringDesc + +@Composable +actual fun StringDesc.localized(): String { + return produceState("") { + value = localized() + }.value +} diff --git a/resources-compose/src/jsMain/kotlin/dev/icerock/moko/resources/compose/StringResource.kt b/resources-compose/src/jsMain/kotlin/dev/icerock/moko/resources/compose/StringResource.kt new file mode 100644 index 00000000..5466b0b9 --- /dev/null +++ b/resources-compose/src/jsMain/kotlin/dev/icerock/moko/resources/compose/StringResource.kt @@ -0,0 +1,33 @@ +/* + * Copyright 2023 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +package dev.icerock.moko.resources.compose + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.produceState +import dev.icerock.moko.resources.PluralsResource +import dev.icerock.moko.resources.StringResource +import dev.icerock.moko.resources.desc.Plural +import dev.icerock.moko.resources.desc.PluralFormatted +import dev.icerock.moko.resources.desc.Resource +import dev.icerock.moko.resources.desc.ResourceFormatted +import dev.icerock.moko.resources.desc.StringDesc + +@Composable +actual fun stringResource(resource: StringResource): String = + produceState("") { value = StringDesc.Resource(resource).localized() }.value + +@Composable +actual fun stringResource(resource: StringResource, vararg args: Any): String = + produceState("") { value = StringDesc.ResourceFormatted(resource, *args).localized() }.value + +@Composable +actual fun stringResource(resource: PluralsResource, quantity: Int): String = + produceState("") { value = StringDesc.Plural(resource, quantity).localized() }.value + +@Composable +actual fun stringResource(resource: PluralsResource, quantity: Int, vararg args: Any): String = + produceState("") { + value = StringDesc.PluralFormatted(resource, quantity, *args).localized() + }.value diff --git a/resources-compose/src/jvmMain/kotlin/dev/icerock/moko/resources/compose/AssetResource.kt b/resources-compose/src/jvmMain/kotlin/dev/icerock/moko/resources/compose/AssetResource.kt new file mode 100644 index 00000000..73b8785e --- /dev/null +++ b/resources-compose/src/jvmMain/kotlin/dev/icerock/moko/resources/compose/AssetResource.kt @@ -0,0 +1,17 @@ +/* + * Copyright 2023 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +package dev.icerock.moko.resources.compose + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.State +import androidx.compose.runtime.produceState +import dev.icerock.moko.resources.AssetResource + +@Composable +actual fun AssetResource.readTextAsState(): State { + return produceState("") { + value = readText() + } +} \ No newline at end of file