Browse Source

Update resources library in imageViewer sample (#3931)

compiler/test-cases-with-kotlin2 v1.6.0-dev1276
Konstantin 1 year ago committed by GitHub
parent
commit
89f21eb99e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      examples/imageviewer/shared/build.gradle.kts
  2. 4
      examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/storage/AndroidImageStorage.kt
  3. 4
      examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/view/CameraView.android.kt
  4. 8
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/Dependencies.kt
  5. 9
      examples/imageviewer/shared/src/desktopMain/kotlin/example/imageviewer/view/CameraView.desktop.kt

3
examples/imageviewer/shared/build.gradle.kts

@ -32,8 +32,7 @@ kotlin {
implementation(compose.foundation) implementation(compose.foundation)
implementation(compose.material) implementation(compose.material)
//implementation(compose.materialIconsExtended) // TODO not working on iOS for now //implementation(compose.materialIconsExtended) // TODO not working on iOS for now
@OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class) implementation("org.jetbrains.compose.components:components-resources:1.6.0-dev1275")
implementation(compose.components.resources)
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0") implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")

4
examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/storage/AndroidImageStorage.kt

@ -21,7 +21,7 @@ import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import org.jetbrains.compose.resources.ExperimentalResourceApi import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.resource import org.jetbrains.compose.resources.readResourceBytes
import java.io.File import java.io.File
private const val maxStorableImageSizePx = 2000 private const val maxStorableImageSizePx = 2000
@ -117,7 +117,7 @@ class AndroidImageStorage(
if (!tempFileToShare.exists()) { if (!tempFileToShare.exists()) {
tempFileToShare.createNewFile() tempFileToShare.createNewFile()
} }
tempFileToShare.writeBytes(resource(picture.resource).readBytes()) tempFileToShare.writeBytes(readResourceBytes(picture.resource))
} }
} }
FileProvider.getUriForFile( FileProvider.getUriForFile(

4
examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/view/CameraView.android.kt

@ -37,7 +37,7 @@ import example.imageviewer.model.createCameraPictureData
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.jetbrains.compose.resources.ExperimentalResourceApi import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.resource import org.jetbrains.compose.resources.readResourceBytes
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.util.* import java.util.*
import java.util.concurrent.Executors import java.util.concurrent.Executors
@ -176,7 +176,7 @@ private fun CameraWithGrantedPermission(
delay(5000) delay(5000)
if (capturePhotoStarted) { if (capturePhotoStarted) {
addLocationInfoAndReturnResult( addLocationInfoAndReturnResult(
resource("android-emulator-photo.jpg").readBytes().toImageBitmap() readResourceBytes("android-emulator-photo.jpg").toImageBitmap()
) )
} }
} }

8
examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/Dependencies.kt

@ -5,11 +5,11 @@ import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.runtime.staticCompositionLocalOf import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.ImageBitmap
import example.imageviewer.filter.PlatformContext import example.imageviewer.filter.PlatformContext
import example.imageviewer.model.* import example.imageviewer.model.PictureData
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow import kotlinx.coroutines.flow.emptyFlow
import org.jetbrains.compose.resources.ExperimentalResourceApi import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.resource import org.jetbrains.compose.resources.readResourceBytes
@OptIn(ExperimentalResourceApi::class) @OptIn(ExperimentalResourceApi::class)
abstract class Dependencies { abstract class Dependencies {
@ -22,7 +22,7 @@ abstract class Dependencies {
val imageProvider: ImageProvider = object : ImageProvider { val imageProvider: ImageProvider = object : ImageProvider {
override suspend fun getImage(picture: PictureData): ImageBitmap = when (picture) { override suspend fun getImage(picture: PictureData): ImageBitmap = when (picture) {
is PictureData.Resource -> { is PictureData.Resource -> {
resource(picture.resource).readBytes().toImageBitmap() readResourceBytes(picture.resource).toImageBitmap()
} }
is PictureData.Camera -> { is PictureData.Camera -> {
@ -32,7 +32,7 @@ abstract class Dependencies {
override suspend fun getThumbnail(picture: PictureData): ImageBitmap = when (picture) { override suspend fun getThumbnail(picture: PictureData): ImageBitmap = when (picture) {
is PictureData.Resource -> { is PictureData.Resource -> {
resource(picture.thumbnailResource).readBytes().toImageBitmap() readResourceBytes(picture.thumbnailResource).toImageBitmap()
} }
is PictureData.Camera -> { is PictureData.Camera -> {

9
examples/imageviewer/shared/src/desktopMain/kotlin/example/imageviewer/view/CameraView.desktop.kt

@ -18,10 +18,7 @@ import example.imageviewer.icon.IconPhotoCamera
import example.imageviewer.model.PictureData import example.imageviewer.model.PictureData
import example.imageviewer.model.createCameraPictureData import example.imageviewer.model.createCameraPictureData
import org.jetbrains.compose.resources.ExperimentalResourceApi import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.orEmpty import org.jetbrains.compose.resources.imageResource
import org.jetbrains.compose.resources.rememberImageBitmap
import org.jetbrains.compose.resources.resource
import java.util.*
@OptIn(ExperimentalResourceApi::class) @OptIn(ExperimentalResourceApi::class)
@Composable @Composable
@ -30,10 +27,10 @@ actual fun CameraView(
onCapture: (picture: PictureData.Camera, image: PlatformStorableImage) -> Unit onCapture: (picture: PictureData.Camera, image: PlatformStorableImage) -> Unit
) { ) {
val randomPicture = remember { resourcePictures.random() } val randomPicture = remember { resourcePictures.random() }
val imageBitmap = resource(randomPicture.resource).rememberImageBitmap().orEmpty() val imageBitmap = imageResource(randomPicture.resource)
Box(Modifier.fillMaxSize().background(Color.Black)) { Box(Modifier.fillMaxSize().background(Color.Black)) {
Image( Image(
bitmap = imageBitmap, bitmap = imageResource(randomPicture.resource),
contentDescription = "Camera stub", contentDescription = "Camera stub",
Modifier.fillMaxSize() Modifier.fillMaxSize()
) )

Loading…
Cancel
Save