feat(phase-1/m4.4b): image-decode seam; viewer + list move to commonMain

expect/actual decodeImageBytes (§6.3 image-decode): Android actual is
the verbatim BitmapFactory.decodeByteArray + asImageBitmap; JVM actual
is skia Image.makeFromEncoded().toComposeImageBitmap() with
runCatching to keep the null-on-failure contract BitmapFactory gave
the screens. Screens switch produceState<Bitmap?> ->
produceState<ImageBitmap?> and the three BitmapFactory call sites
(list thumbnail, viewer, editor thumbnail stays with the screen next
step). Dispatchers.IO -> the existing M4.3c ioDispatcher seam
(identical actuals).

Verified: ./gradlew :composeApp:compileKotlinJvm
:composeApp:compileDebugKotlinAndroid (BUILD SUCCESSFUL); full gate +:desktopApp:run --self-test + :core:data-jvm:smoke run at the end
of the step chain.
This commit is contained in:
Incido Developer committed 2026-10-06 18:31:08 +13:00
1 parent bdb3d7157d
commit 56949c485f
5 files changed
+58 -14

No files matched your search

@@ -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()
@@ -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<android.graphics.Bitmap?>(initialValue = null, key1 = attachmentId) {
val bitmap by produceState<ImageBitmap?>(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
@@ -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<android.graphics.Bitmap?>(initialValue = null, key1 = firstImage?.id) {
val thumbnailBitmap by produceState<ImageBitmap?>(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(),
@@ -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?
@@ -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()