Browse Source

feat: generate Res.getAsFlow

pull/4500/head
Chanjung Kim 9 months ago
parent
commit
4be96281b1
  1. 34
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesSpec.kt

34
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesSpec.kt

@ -1,6 +1,7 @@
package org.jetbrains.compose.resources
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.plusParameter
import org.jetbrains.compose.internal.utils.uppercaseFirstChar
import java.nio.file.Path
import java.util.*
@ -151,6 +152,39 @@ internal fun getResFileSpecs(
.addStatement("""return %M("$moduleDir" + path)""", readResourceBytes)
.build()
)
val getResourceAsFlow = MemberName("org.jetbrains.compose.resources", "getResourceAsFlow")
resObject.addFunction(
FunSpec.builder("getAsFlow")
.addKdoc(
"""
Returns a flow which emits the content of the resource file as byte array chunks. The length of each
chunk is not empty and has the length of [byteCount] or smaller. The flow will throw
[MissingResourceException] when the resource file is missing or [ResourceIOException] if any IO
error occurs. You can catch those with the [catch][kotlinx.coroutines.flow.catch] operator. This
function is useful when the resource is too big to be contained in a single [ByteArray].
Example: `val bytes = Res.getAsFlow("files/key.bin").collectToList().flatten()`
@param path The path of the file to read in the resource's directory.
@param byteCount The maximum length of the emitted byte arrays. The flow can emit an array smaller
than this length.
@return A flow that emits the content of the file as byte sub-arrays.
@throws IllegalArgumentException - when [byteCount] is not positive.
""".trimIndent()
)
.addParameter("path", String::class)
.addParameter("byteCount", Int::class)
.returns(
ClassName("kotlinx.coroutines.flow", "Flow")
.plusParameter(ByteArray::class.asTypeName())
)
.addStatement("""return %M("$moduleDir" + path, byteCount)""", getResourceAsFlow)
.build()
)
ResourceType.values().forEach { type ->
resObject.addType(TypeSpec.objectBuilder(type.typeName).build())
}

Loading…
Cancel
Save