Skip to content

Commit

Permalink
Merge pull request #438 from icerockdev/compose-web-target-with-asset…
Browse files Browse the repository at this point in the history
…-res

#422 Compose web target with asset res
  • Loading branch information
Alex009 committed Mar 10, 2023
2 parents cce55f5 + c037ae1 commit 4ece9be
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions resources-compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,29 @@ android {
}

kotlin {
android()
jvm()
js(IR) {
browser()
}
sourceSets {
commonMain {
dependencies {
api(projects.resources)
implementation(compose.runtime)
}
}

named("androidMain") {
dependencies {
implementation(compose.foundation)
}
}

named("jvmMain") {
dependencies {
api(compose.desktop.common)
implementation(compose.foundation)
implementation(compose.desktop.currentOs)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> {
val context = LocalContext.current
return produceState("") {
value = readText(context)
}
}
Original file line number Diff line number Diff line change
@@ -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<String>
Original file line number Diff line number Diff line change
@@ -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<String> {
return produceState("") {
value = getText()
}
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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<String> {
return produceState("") {
value = readText()
}
}

0 comments on commit 4ece9be

Please sign in to comment.