Render artifact thumbnails with an honest loading/failure state
The artifact grid could show failure placeholders that were never earned: a server-relative artifact url that failed to resolve against the configured server was fetched anyway and blamed with the "!" failure mark, and the loading state was a bare "…" text. - Pass the resolved url (nullable) into ArtifactImage; a null short-circuits to a new distinct Unavailable state (LinkOff glyph) and issues no request, so a fetch that was never attempted is never drawn as a failure. - Reserve Error/"!" for a fetch or decode that was actually attempted. - Replace the "…" text with a real Material 3 CircularProgressIndicator. - Resolve gallery artifact urls in one shared helper (resolvedArtifactUrl) so the image tile and the non-image row cannot disagree; non-image artifacts keep their file-type icon. - Add unit tests covering resolution against the configured server and the null (unconfigured) case.
This commit is contained in:
@@ -6,6 +6,10 @@ import android.util.LruCache
|
|||||||
import androidx.compose.foundation.Image
|
import androidx.compose.foundation.Image
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.layout.size
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.filled.LinkOff
|
||||||
|
import androidx.compose.material3.CircularProgressIndicator
|
||||||
|
import androidx.compose.material3.Icon
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
@@ -52,21 +56,31 @@ private val artifactBitmapCache = object : LruCache<String, Bitmap>(MAX_CACHED_B
|
|||||||
|
|
||||||
private const val MAX_CACHED_BITMAPS = 24
|
private const val MAX_CACHED_BITMAPS = 24
|
||||||
|
|
||||||
/** Nothing is shown until the bytes arrive, and a failure is shown, not crashed. */
|
/**
|
||||||
|
* One inline preview's state. [Unavailable] is deliberately separate from
|
||||||
|
* [Error]: it means no fetch was attempted because the artifact url could not be
|
||||||
|
* resolved against a configured server, so it must never be drawn with the
|
||||||
|
* failure mark (that would blame a load that never happened). [Error] is reserved
|
||||||
|
* for a fetch or decode that was actually attempted and failed.
|
||||||
|
*/
|
||||||
private sealed interface ArtifactImageState {
|
private sealed interface ArtifactImageState {
|
||||||
data object Loading : ArtifactImageState
|
data object Loading : ArtifactImageState
|
||||||
|
data object Unavailable : ArtifactImageState
|
||||||
data object Error : ArtifactImageState
|
data object Error : ArtifactImageState
|
||||||
data class Ready(val bitmap: Bitmap) : ArtifactImageState
|
data class Ready(val bitmap: Bitmap) : ArtifactImageState
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An inline artifact preview. Fetching and decoding happen off the main thread
|
* An inline artifact preview. [url] is the resolved, absolute http(s) url, or
|
||||||
* and are cached; [maxDim] requests a thumbnail-sized decode (a full-resolution
|
* null when the artifact could not be resolved (no configured server): a null url
|
||||||
* bitmap is only used for the explicit full-size view, [maxDim] <= 0).
|
* shows [ArtifactImageState.Unavailable] and issues no request at all. Fetching
|
||||||
|
* and decoding happen off the main thread and are cached; [maxDim] requests a
|
||||||
|
* thumbnail-sized decode (a full-resolution bitmap is only used for the explicit
|
||||||
|
* full-size view, [maxDim] <= 0).
|
||||||
*/
|
*/
|
||||||
@Composable
|
@Composable
|
||||||
internal fun ArtifactImage(
|
internal fun ArtifactImage(
|
||||||
url: String,
|
url: String?,
|
||||||
contentDescription: String?,
|
contentDescription: String?,
|
||||||
maxDim: Int,
|
maxDim: Int,
|
||||||
fetchBytes: suspend (String) -> ByteArray?,
|
fetchBytes: suspend (String) -> ByteArray?,
|
||||||
@@ -82,7 +96,20 @@ internal fun ArtifactImage(
|
|||||||
contentScale = ContentScale.Fit,
|
contentScale = ContentScale.Fit,
|
||||||
modifier = Modifier.size(size),
|
modifier = Modifier.size(size),
|
||||||
)
|
)
|
||||||
ArtifactImageState.Loading -> Text("…", style = MaterialTheme.typography.bodySmall)
|
// A real, visible loading state: distinct from both the failure mark
|
||||||
|
// and the non-image file glyph.
|
||||||
|
ArtifactImageState.Loading -> CircularProgressIndicator(
|
||||||
|
modifier = Modifier.size(ARTIFACT_PROGRESS_DP),
|
||||||
|
strokeWidth = ARTIFACT_PROGRESS_STROKE_DP,
|
||||||
|
)
|
||||||
|
// No fetch was attempted (unresolved url): a link-off glyph, never
|
||||||
|
// the failure mark, and never the file-type icon of a non-image.
|
||||||
|
ArtifactImageState.Unavailable -> Icon(
|
||||||
|
imageVector = Icons.Filled.LinkOff,
|
||||||
|
contentDescription = contentDescription?.let { "$it unavailable" }
|
||||||
|
?: "Preview unavailable",
|
||||||
|
tint = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||||
|
)
|
||||||
ArtifactImageState.Error -> Text(
|
ArtifactImageState.Error -> Text(
|
||||||
"!",
|
"!",
|
||||||
style = MaterialTheme.typography.bodySmall,
|
style = MaterialTheme.typography.bodySmall,
|
||||||
@@ -94,10 +121,11 @@ internal fun ArtifactImage(
|
|||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
private fun rememberArtifactBitmap(
|
private fun rememberArtifactBitmap(
|
||||||
url: String,
|
url: String?,
|
||||||
maxDim: Int,
|
maxDim: Int,
|
||||||
fetchBytes: suspend (String) -> ByteArray?,
|
fetchBytes: suspend (String) -> ByteArray?,
|
||||||
): ArtifactImageState {
|
): ArtifactImageState {
|
||||||
|
if (url == null) return ArtifactImageState.Unavailable
|
||||||
val key = "$url@$maxDim"
|
val key = "$url@$maxDim"
|
||||||
var state by remember(key) { mutableStateOf<ArtifactImageState>(ArtifactImageState.Loading) }
|
var state by remember(key) { mutableStateOf<ArtifactImageState>(ArtifactImageState.Loading) }
|
||||||
LaunchedEffect(key) {
|
LaunchedEffect(key) {
|
||||||
@@ -145,3 +173,7 @@ private fun decodeSampled(bytes: ByteArray, maxDim: Int): Bitmap? = try {
|
|||||||
} catch (_: Throwable) {
|
} catch (_: Throwable) {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The loading indicator's size and stroke, a small inline spinner. */
|
||||||
|
private val ARTIFACT_PROGRESS_DP = 24.dp
|
||||||
|
private val ARTIFACT_PROGRESS_STROKE_DP = 2.dp
|
||||||
|
|||||||
@@ -163,6 +163,17 @@ internal fun artifactTileDp(availableWidth: Dp, columns: Int, spacing: Dp, prefe
|
|||||||
return fit.coerceIn(0.dp, preferred)
|
return fit.coerceIn(0.dp, preferred)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The absolute url a gallery artifact is previewed/opened at, or null when it
|
||||||
|
* cannot be resolved against the configured server (blank or invalid). The one
|
||||||
|
* place a gallery artifact url is resolved, shared by the image tile and the
|
||||||
|
* non-image row so the two can never disagree. A null must never reach a fetch:
|
||||||
|
* a server-relative url has no scheme and can never load, so the tile renders the
|
||||||
|
* distinct "unavailable" state instead of the failure mark.
|
||||||
|
*/
|
||||||
|
internal fun resolvedArtifactUrl(artifact: StageArtifact, configuredServerUrl: String): String? =
|
||||||
|
ServerUrl.resolveArtifactUrl(configuredServerUrl, artifact.url)
|
||||||
|
|
||||||
/** One image artifact as a square thumbnail that opens the full-size view. */
|
/** One image artifact as a square thumbnail that opens the full-size view. */
|
||||||
@Composable
|
@Composable
|
||||||
private fun ArtifactTile(
|
private fun ArtifactTile(
|
||||||
@@ -173,7 +184,7 @@ private fun ArtifactTile(
|
|||||||
) {
|
) {
|
||||||
// Resolve once per (url, configuration), not on every recomposition.
|
// Resolve once per (url, configuration), not on every recomposition.
|
||||||
val resolved = remember(artifact.url, configuredServerUrl) {
|
val resolved = remember(artifact.url, configuredServerUrl) {
|
||||||
ServerUrl.resolveArtifactUrl(configuredServerUrl, artifact.url)
|
resolvedArtifactUrl(artifact, configuredServerUrl)
|
||||||
}
|
}
|
||||||
var showFull by remember(artifact.url) { mutableStateOf(false) }
|
var showFull by remember(artifact.url) { mutableStateOf(false) }
|
||||||
|
|
||||||
@@ -183,8 +194,11 @@ private fun ArtifactTile(
|
|||||||
.clip(RoundedCornerShape(12.dp))
|
.clip(RoundedCornerShape(12.dp))
|
||||||
.clickable(enabled = resolved != null) { showFull = true },
|
.clickable(enabled = resolved != null) { showFull = true },
|
||||||
) {
|
) {
|
||||||
|
// Pass the resolved url (null when there is no configured server): a
|
||||||
|
// null never triggers a relative-url fetch, it shows the distinct
|
||||||
|
// "unavailable" state instead of the failure mark.
|
||||||
ArtifactImage(
|
ArtifactImage(
|
||||||
url = resolved ?: artifact.url,
|
url = resolved,
|
||||||
contentDescription = "Preview of ${artifact.name}",
|
contentDescription = "Preview of ${artifact.name}",
|
||||||
maxDim = THUMBNAIL_MAX_DIM,
|
maxDim = THUMBNAIL_MAX_DIM,
|
||||||
fetchBytes = fetchArtifactBytes,
|
fetchBytes = fetchArtifactBytes,
|
||||||
@@ -213,7 +227,7 @@ private fun ArtifactRow(
|
|||||||
val clipboard = LocalClipboardManager.current
|
val clipboard = LocalClipboardManager.current
|
||||||
// Resolve/format once per (url, configuration), not on every recomposition.
|
// Resolve/format once per (url, configuration), not on every recomposition.
|
||||||
val resolved = remember(artifact.url, configuredServerUrl) {
|
val resolved = remember(artifact.url, configuredServerUrl) {
|
||||||
ServerUrl.resolveArtifactUrl(configuredServerUrl, artifact.url)
|
resolvedArtifactUrl(artifact, configuredServerUrl)
|
||||||
}
|
}
|
||||||
val metaLine = remember(artifact) { artifactMetaLine(artifact) }
|
val metaLine = remember(artifact) { artifactMetaLine(artifact) }
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package net.awan.pipelines.ui.graph
|
package net.awan.pipelines.ui.graph
|
||||||
|
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
import net.awan.pipelines.data.protocol.StageArtifact
|
||||||
import org.junit.Assert.assertEquals
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertNull
|
||||||
import org.junit.Assert.assertTrue
|
import org.junit.Assert.assertTrue
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
@@ -10,6 +12,27 @@ class ArtifactGalleryTest {
|
|||||||
private val spacing = 8.dp
|
private val spacing = 8.dp
|
||||||
private val preferred = 96.dp
|
private val preferred = 96.dp
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun resolvedArtifactUrlUsesTheConfiguredServer() {
|
||||||
|
val shot = StageArtifact(
|
||||||
|
name = "00_start.png",
|
||||||
|
sizeBytes = 239275,
|
||||||
|
contentType = "image/png",
|
||||||
|
url = "/artifacts/60/4/00_start.png",
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
"http://192.168.1.20:8081/artifacts/60/4/00_start.png",
|
||||||
|
resolvedArtifactUrl(shot, "ws://192.168.1.20:8081/connect"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun resolvedArtifactUrlIsNullWhenUnconfiguredSoNoFetchIsAttempted() {
|
||||||
|
val shot = StageArtifact(url = "/artifacts/60/4/00_start.png")
|
||||||
|
assertNull(resolvedArtifactUrl(shot, ""))
|
||||||
|
assertNull(resolvedArtifactUrl(shot, "http://host:8080/connect"))
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun narrowWidthShrinksTilesSoTheRowNeverOverflows() {
|
fun narrowWidthShrinksTilesSoTheRowNeverOverflows() {
|
||||||
// A depth-2 phone stage pane is ~360dp less two 56dp peeks and the 16dp
|
// A depth-2 phone stage pane is ~360dp less two 56dp peeks and the 16dp
|
||||||
|
|||||||
Reference in New Issue
Block a user