diff --git a/composeApp/src/androidMain/kotlin/org/incido/compose/util/ImageDecodeAndroid.kt b/composeApp/src/androidMain/kotlin/org/incido/compose/util/ImageDecodeAndroid.kt new file mode 100644 index 0000000..ec99386 --- /dev/null +++ b/composeApp/src/androidMain/kotlin/org/incido/compose/util/ImageDecodeAndroid.kt @@ -0,0 +1,13 @@ +package org.incido.compose.util + +import android.graphics.BitmapFactory +import androidx.compose.ui.graphics.ImageBitmap +import androidx.compose.ui.graphics.asImageBitmap + +/** + * M4.4b Android actual — the verbatim pre-M4 decode + * (`BitmapFactory.decodeByteArray` returns null on undecodable bytes, + * exactly the contract [decodeImageBytes] keeps). + */ +actual fun decodeImageBytes(bytes: ByteArray): ImageBitmap? = + BitmapFactory.decodeByteArray(bytes, 0, bytes.size)?.asImageBitmap() \ No newline at end of file diff --git a/composeApp/src/androidMain/kotlin/org/incido/compose/ui/editor/ImageViewerScreen.kt b/composeApp/src/commonMain/kotlin/org/incido/compose/ui/editor/ImageViewerScreen.kt similarity index 93% rename from composeApp/src/androidMain/kotlin/org/incido/compose/ui/editor/ImageViewerScreen.kt rename to composeApp/src/commonMain/kotlin/org/incido/compose/ui/editor/ImageViewerScreen.kt index d265c6e..5bfb97c 100644 --- a/composeApp/src/androidMain/kotlin/org/incido/compose/ui/editor/ImageViewerScreen.kt +++ b/composeApp/src/commonMain/kotlin/org/incido/compose/ui/editor/ImageViewerScreen.kt @@ -26,7 +26,7 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color -import androidx.compose.ui.graphics.asImageBitmap +import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.layout.ContentScale @@ -34,9 +34,9 @@ import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.window.Dialog import androidx.compose.ui.window.DialogProperties -import android.graphics.BitmapFactory -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext +import org.incido.compose.di.ioDispatcher +import org.incido.compose.util.decodeImageBytes import org.incido.domain.repository.NoteRepository /** @@ -91,10 +91,10 @@ fun ImageViewerScreen( // Decode the image (or display an error message when the // bytes can't be loaded / decoded). var loadFailed by remember { mutableStateOf(false) } - val bitmap by produceState(initialValue = null, key1 = attachmentId) { + val bitmap by produceState(initialValue = null, key1 = attachmentId) { loadFailed = false val bytes: ByteArray? = when (attachmentId) { - is ViewerAttachmentId.Persisted -> withContext(Dispatchers.IO) { + is ViewerAttachmentId.Persisted -> withContext(ioDispatcher) { repository.getAttachmentBytes(attachmentId.id) } is ViewerAttachmentId.Pending -> pendingBytes @@ -103,8 +103,8 @@ fun ImageViewerScreen( loadFailed = true return@produceState } - val decoded = withContext(Dispatchers.IO) { - BitmapFactory.decodeByteArray(bytes, 0, bytes.size) + val decoded = withContext(ioDispatcher) { + decodeImageBytes(bytes) } value = decoded if (decoded == null) loadFailed = true @@ -112,7 +112,7 @@ fun ImageViewerScreen( if (bitmap != null) { Image( - bitmap = bitmap!!.asImageBitmap(), + bitmap = bitmap!!, contentDescription = null, contentScale = ContentScale.Fit, modifier = Modifier diff --git a/composeApp/src/androidMain/kotlin/org/incido/compose/ui/notelist/NoteListScreen.kt b/composeApp/src/commonMain/kotlin/org/incido/compose/ui/notelist/NoteListScreen.kt similarity index 98% rename from composeApp/src/androidMain/kotlin/org/incido/compose/ui/notelist/NoteListScreen.kt rename to composeApp/src/commonMain/kotlin/org/incido/compose/ui/notelist/NoteListScreen.kt index f29daf8..85d2f14 100644 --- a/composeApp/src/androidMain/kotlin/org/incido/compose/ui/notelist/NoteListScreen.kt +++ b/composeApp/src/commonMain/kotlin/org/incido/compose/ui/notelist/NoteListScreen.kt @@ -1,6 +1,5 @@ package org.incido.compose.ui.notelist -import android.graphics.BitmapFactory import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.combinedClickable @@ -68,15 +67,17 @@ import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.Color -import androidx.compose.ui.graphics.asImageBitmap +import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.lerp import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.viewmodel.compose.viewModel +import org.incido.compose.di.ioDispatcher import org.incido.compose.di.requireAppGraph import org.incido.compose.ui.theme.toComposeColor +import org.incido.compose.util.decodeImageBytes import org.incido.compose.util.rememberRelativeTimestamp import org.incido.domain.model.Note import org.incido.domain.model.NoteAttachment @@ -471,13 +472,13 @@ private fun NoteCard( // a downsampled bitmap for the card background. The produceState // scopes the work to the card's lifetime, so off-screen cards // don't keep their bitmaps alive. - val thumbnailBitmap by produceState(initialValue = null, key1 = firstImage?.id) { + val thumbnailBitmap by produceState(initialValue = null, key1 = firstImage?.id) { if (firstImage == null || repository == null) { value = null } else { - value = withContext(kotlinx.coroutines.Dispatchers.IO) { + value = withContext(ioDispatcher) { val bytes = repository.getAttachmentBytes(firstImage.id) ?: return@withContext null - BitmapFactory.decodeByteArray(bytes, 0, bytes.size) + decodeImageBytes(bytes) } } } @@ -537,7 +538,7 @@ private fun NoteCard( // Card's opaque containerColor was drawn on top of them). if (thumbnailBitmap != null) { Image( - bitmap = thumbnailBitmap!!.asImageBitmap(), + bitmap = thumbnailBitmap!!, contentDescription = null, contentScale = ContentScale.Crop, modifier = Modifier.matchParentSize(), diff --git a/composeApp/src/commonMain/kotlin/org/incido/compose/util/ImageDecode.kt b/composeApp/src/commonMain/kotlin/org/incido/compose/util/ImageDecode.kt new file mode 100644 index 0000000..8ad228a --- /dev/null +++ b/composeApp/src/commonMain/kotlin/org/incido/compose/util/ImageDecode.kt @@ -0,0 +1,15 @@ +package org.incido.compose.util + +import androidx.compose.ui.graphics.ImageBitmap + +/** + * Phase 1 (M4.4b) — the §6.3 image-decode seam. + * + * The single decode call the UI layer needs (attachment thumbnails + + * the full-screen viewer). Android actual = the verbatim + * `BitmapFactory.decodeByteArray` (+ `asImageBitmap`); JVM actual = + * `org.jetbrains.skia.Image.makeFromEncoded(bytes).toComposeImageBitmap()` + * — both wrapped so undecodable bytes return null, which is how the + * pre-M4 screens already treat a failed `BitmapFactory` decode. + */ +expect fun decodeImageBytes(bytes: ByteArray): ImageBitmap? \ No newline at end of file diff --git a/composeApp/src/jvmMain/kotlin/org/incido/compose/util/ImageDecodeJvm.kt b/composeApp/src/jvmMain/kotlin/org/incido/compose/util/ImageDecodeJvm.kt new file mode 100644 index 0000000..156c2a7 --- /dev/null +++ b/composeApp/src/jvmMain/kotlin/org/incido/compose/util/ImageDecodeJvm.kt @@ -0,0 +1,15 @@ +package org.incido.compose.util + +import androidx.compose.ui.graphics.ImageBitmap +import androidx.compose.ui.graphics.toComposeImageBitmap +import org.jetbrains.skia.Image + +/** + * M4.4b JVM actual — the §6.3 recipe: + * `Image.makeFromEncoded(bytes).toComposeImageBitmap()`. skia THROWS on + * undecodable bytes where BitmapFactory returns null, so the + * runCatching keeps the seam's null-on-failure contract (empty/partial + * attachment bytes must show the placeholder, not crash the strip). + */ +actual fun decodeImageBytes(bytes: ByteArray): ImageBitmap? = + runCatching { Image.makeFromEncoded(bytes).toComposeImageBitmap() }.getOrNull() \ No newline at end of file