Skip to content

Commit

Permalink
Update resources library in imageViewer sample (#3931)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrakok authored Nov 14, 2023
1 parent 5cc685c commit 89f21eb
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 16 deletions.
3 changes: 1 addition & 2 deletions examples/imageviewer/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ kotlin {
implementation(compose.foundation)
implementation(compose.material)
//implementation(compose.materialIconsExtended) // TODO not working on iOS for now
@OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
implementation(compose.components.resources)
implementation("org.jetbrains.compose.components:components-resources:1.6.0-dev1275")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.resource
import org.jetbrains.compose.resources.readResourceBytes
import java.io.File

private const val maxStorableImageSizePx = 2000
Expand Down Expand Up @@ -117,7 +117,7 @@ class AndroidImageStorage(
if (!tempFileToShare.exists()) {
tempFileToShare.createNewFile()
}
tempFileToShare.writeBytes(resource(picture.resource).readBytes())
tempFileToShare.writeBytes(readResourceBytes(picture.resource))
}
}
FileProvider.getUriForFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import example.imageviewer.model.createCameraPictureData
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.resource
import org.jetbrains.compose.resources.readResourceBytes
import java.nio.ByteBuffer
import java.util.*
import java.util.concurrent.Executors
Expand Down Expand Up @@ -176,7 +176,7 @@ private fun CameraWithGrantedPermission(
delay(5000)
if (capturePhotoStarted) {
addLocationInfoAndReturnResult(
resource("android-emulator-photo.jpg").readBytes().toImageBitmap()
readResourceBytes("android-emulator-photo.jpg").toImageBitmap()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.ImageBitmap
import example.imageviewer.filter.PlatformContext
import example.imageviewer.model.*
import example.imageviewer.model.PictureData
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.resource
import org.jetbrains.compose.resources.readResourceBytes

@OptIn(ExperimentalResourceApi::class)
abstract class Dependencies {
Expand All @@ -22,7 +22,7 @@ abstract class Dependencies {
val imageProvider: ImageProvider = object : ImageProvider {
override suspend fun getImage(picture: PictureData): ImageBitmap = when (picture) {
is PictureData.Resource -> {
resource(picture.resource).readBytes().toImageBitmap()
readResourceBytes(picture.resource).toImageBitmap()
}

is PictureData.Camera -> {
Expand All @@ -32,7 +32,7 @@ abstract class Dependencies {

override suspend fun getThumbnail(picture: PictureData): ImageBitmap = when (picture) {
is PictureData.Resource -> {
resource(picture.thumbnailResource).readBytes().toImageBitmap()
readResourceBytes(picture.thumbnailResource).toImageBitmap()
}

is PictureData.Camera -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ import example.imageviewer.icon.IconPhotoCamera
import example.imageviewer.model.PictureData
import example.imageviewer.model.createCameraPictureData
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.orEmpty
import org.jetbrains.compose.resources.rememberImageBitmap
import org.jetbrains.compose.resources.resource
import java.util.*
import org.jetbrains.compose.resources.imageResource

@OptIn(ExperimentalResourceApi::class)
@Composable
Expand All @@ -30,10 +27,10 @@ actual fun CameraView(
onCapture: (picture: PictureData.Camera, image: PlatformStorableImage) -> Unit
) {
val randomPicture = remember { resourcePictures.random() }
val imageBitmap = resource(randomPicture.resource).rememberImageBitmap().orEmpty()
val imageBitmap = imageResource(randomPicture.resource)
Box(Modifier.fillMaxSize().background(Color.Black)) {
Image(
bitmap = imageBitmap,
bitmap = imageResource(randomPicture.resource),
contentDescription = "Camera stub",
Modifier.fillMaxSize()
)
Expand Down

0 comments on commit 89f21eb

Please sign in to comment.