Skip to content

Commit

Permalink
Fix DesktopImageStorage to store images by id, rather than by the `…
Browse files Browse the repository at this point in the history
…PictureData.Camera` itself (#4963)
  • Loading branch information
m-sasha authored Jun 13, 2024
1 parent f8cf966 commit a1b88db
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ abstract class Dependencies {
}

override fun saveImage(picture: PictureData.Camera, image: PlatformStorableImage) {
pictures.add(0, picture)
imageStorage.saveImage(picture, image)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package example.imageviewer

import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.toAwtImage
import androidx.compose.ui.graphics.toComposeImageBitmap
Expand All @@ -13,37 +12,36 @@ private const val maxStorableImageSizePx = 2000
private const val storableThumbnailSizePx = 200

class DesktopImageStorage(
private val pictures: SnapshotStateList<PictureData>,
private val ioScope: CoroutineScope
) : ImageStorage {
private val largeImages = mutableMapOf<PictureData.Camera, ImageBitmap>()
private val thumbnails = mutableMapOf<PictureData.Camera, ImageBitmap>()
private val largeImages = mutableMapOf<String, ImageBitmap>()
private val thumbnails = mutableMapOf<String, ImageBitmap>()

override fun saveImage(picture: PictureData.Camera, image: PlatformStorableImage) {
if (image.imageBitmap.width == 0 || image.imageBitmap.height == 0) {
return
}
ioScope.launch {
largeImages[picture] = image.imageBitmap.fitInto(maxStorableImageSizePx)
thumbnails[picture] = image.imageBitmap.fitInto(storableThumbnailSizePx)
pictures.add(0, picture)
largeImages[picture.id] = image.imageBitmap.fitInto(maxStorableImageSizePx)
thumbnails[picture.id] = image.imageBitmap.fitInto(storableThumbnailSizePx)
}
}

override fun delete(picture: PictureData.Camera) {
// For now, on Desktop pictures saving in memory. We don't need additional delete logic.
largeImages.remove(picture.id)
thumbnails.remove(picture.id)
}

override fun rewrite(picture: PictureData.Camera) {
// For now, on Desktop pictures saving in memory. We don't need additional rewrite logic.
}

override suspend fun getThumbnail(picture: PictureData.Camera): ImageBitmap {
return thumbnails[picture]!!
return thumbnails[picture.id]!!
}

override suspend fun getImage(picture: PictureData.Camera): ImageBitmap {
return largeImages[picture]!!
return largeImages[picture.id]!!
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private fun getDependencies(
toastState.value = ToastState.Shown(text)
}
}
override val imageStorage: DesktopImageStorage = DesktopImageStorage(pictures, ioScope)
override val imageStorage: DesktopImageStorage = DesktopImageStorage(ioScope)
override val sharePicture: SharePicture = object : SharePicture {
override fun share(context: PlatformContext, picture: PictureData) {
// On Desktop share feature not supported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import platform.CoreGraphics.CGRectMake
Expand All @@ -36,7 +35,7 @@ private const val storableThumbnailSizePx = 180
private const val jpegCompressionQuality = 60

class IosImageStorage(
private val pictures: SnapshotStateList<PictureData>,
pictures: SnapshotStateList<PictureData>,
private val ioScope: CoroutineScope
) : ImageStorage {

Expand Down Expand Up @@ -77,7 +76,6 @@ class IosImageStorage(
picture.jpgFile.writeJpeg(fitInto(maxStorableImageSizePx))
picture.thumbnailJpgFile.writeJpeg(fitInto(storableThumbnailSizePx))
}
pictures.add(0, picture)
picture.jsonFile.writeText(picture.toJson())
}
}
Expand Down

0 comments on commit a1b88db

Please sign in to comment.