Browse Source

Add demo code using Res.getAsFlow()

pull/4500/head
Chanjung Kim 9 months ago
parent
commit
5d85765e89
  1. 2
      components/gradle/libs.versions.toml
  2. 1
      components/resources/demo/shared/build.gradle.kts
  3. 50
      components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/FileRes.kt

2
components/gradle/libs.versions.toml

@ -1,5 +1,6 @@
[versions] [versions]
kotlinx-coroutines = "1.7.3" kotlinx-coroutines = "1.7.3"
kotlinx-io = "0.3.1"
androidx-appcompat = "1.6.1" androidx-appcompat = "1.6.1"
androidx-activity-compose = "1.8.2" androidx-activity-compose = "1.8.2"
androidx-test = "1.5.0" androidx-test = "1.5.0"
@ -8,6 +9,7 @@ androidx-compose = "1.6.0"
[libraries] [libraries]
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" } kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" } kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }
kotlinx-io-core = { module = "org.jetbrains.kotlinx:kotlinx-io-core", version.ref = "kotlinx-io" }
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidx-appcompat" } androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidx-appcompat" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activity-compose" } androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activity-compose" }
androidx-test-core = { module = "androidx.test:core", version.ref = "androidx-test" } androidx-test-core = { module = "androidx.test:core", version.ref = "androidx-test" }

1
components/resources/demo/shared/build.gradle.kts

@ -59,6 +59,7 @@ kotlin {
commonMain.dependencies { commonMain.dependencies {
implementation(compose.runtime) implementation(compose.runtime)
implementation(compose.material3) implementation(compose.material3)
implementation(libs.kotlinx.io.core)
implementation(project(":resources:library")) implementation(project(":resources:library"))
} }
val desktopMain by getting val desktopMain by getting

50
components/resources/demo/shared/src/commonMain/kotlin/org/jetbrains/compose/resources/demo/shared/FileRes.kt

@ -9,11 +9,12 @@ import androidx.compose.runtime.*
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import components.resources.demo.shared.generated.resources.Res import components.resources.demo.shared.generated.resources.Res
import kotlinx.io.*
@Composable @Composable
fun FileRes(paddingValues: PaddingValues) { fun FileRes(paddingValues: PaddingValues) {
Column( Column(
modifier = Modifier.padding(paddingValues) modifier = Modifier.padding(paddingValues).verticalScroll(rememberScrollState())
) { ) {
Text( Text(
modifier = Modifier.padding(16.dp), modifier = Modifier.padding(16.dp),
@ -50,7 +51,7 @@ fun FileRes(paddingValues: PaddingValues) {
) )
Text( Text(
modifier = Modifier.padding(16.dp), modifier = Modifier.padding(16.dp),
text = "File: 'files/platform-text.txt'", text = "File: 'files/icon.xml'",
style = MaterialTheme.typography.titleLarge style = MaterialTheme.typography.titleLarge
) )
OutlinedCard( OutlinedCard(
@ -80,5 +81,50 @@ fun FileRes(paddingValues: PaddingValues) {
Text(bytes.decodeToString()) Text(bytes.decodeToString())
""".trimIndent() """.trimIndent()
) )
Text(
modifier = Modifier.padding(16.dp),
text = "File: 'drawable/compose.png'",
style = MaterialTheme.typography.titleLarge
)
OutlinedCard(
modifier = Modifier.padding(horizontal = 16.dp),
shape = RoundedCornerShape(4.dp),
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.primaryContainer)
) {
var content by remember { mutableStateOf("") }
LaunchedEffect(Unit) {
@OptIn(ExperimentalStdlibApi::class)
Buffer().use { buffer ->
Res.getAsFlow("drawable/compose.png").collect { chunk ->
buffer.write(chunk)
}
content = buffer.readByteArray().asList().toString()
}
}
Text(
modifier = Modifier.padding(8.dp).height(200.dp).verticalScroll(rememberScrollState()),
text = content,
color = MaterialTheme.colorScheme.onPrimaryContainer,
)
}
Text(
modifier = Modifier.padding(16.dp),
text = """
import kotlinx.io.*
var content by remember {
mutableStateOf("")
}
LaunchedEffect(Unit) {
Buffer().use { buffer ->
Res.getAsFlow("drawable/compose.png").collect { chunk ->
buffer.write(chunk)
}
content = buffer.readByteArray().asList().toString()
}
}
Text(content)
""".trimIndent()
)
} }
} }
Loading…
Cancel
Save