Cross-fade loading, empty and error states in data-backed panes

A data-backed pane swapped its loading spinner, empty message, error
message and content with a plain `when`, so content flashed from empty to
full and empty/error states popped.

Introduce a shared presentation model in ui/components/States.kt:
- `LoadPhase` (Loading, Error(message), Empty(message), Content), with
  message-carrying phases capturing their text at classification so a
  fading-out branch never recomposes against cleared error/filter state.
- `listLoadPhase(loading, error, hasData, emptyMessage)` and
  `detailLoadPhase(error, hasData)` pure classifiers preserving the
  existing precedence (failed load with no data, then loading, then
  empty, else content; stale data keeps the pane on content).
- `StateCrossfade(phase)` wraps the branch in a `Crossfade` using the
  existing `MotionStandard` token; both branches share one box so the
  resting layout is unchanged and the outgoing phase fades under the
  incoming one.

Apply to PipelineListScreen, RunHistoryScreen, PipelineDetailScreen and
RunDetailScreen. Detail panes render the fading-out branch from the last
non-null item. The Runs pane keeps its inline empty/error line inside the
list, now fading through animateListItem().

Add LoadPhaseTest covering the classifiers and captured messages, and
document the change and evidence in android/README.md.
This commit is contained in:
2026-10-16 10:45:27 +01:00
parent 934994d7f7
commit b89ad29c41
7 changed files with 454 additions and 123 deletions
@@ -1,5 +1,6 @@
package net.awan.pipelines.ui.components
import androidx.compose.animation.Crossfade
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
@@ -65,6 +66,88 @@ fun EmptyState(message: String, modifier: Modifier = Modifier) {
}
}
/**
* The mutually exclusive presentation states of a data-backed pane: a load with
* no data yet ([Loading]), a failed load with no data ([Error]), loaded-but-
* nothing-to-show ([Empty]) and loaded content ([Content]).
*
* Both message-carrying states and [Empty] capture the text they are rendered
* with when the state is classified and never read a live value, so the branch
* that is fading out cannot be recomposed against a since-cleared error or a
* since-changed filter. [StateCrossfade] animates between these and a pane is in
* exactly one at a time.
*/
sealed interface LoadPhase {
/** A load is in flight and there is nothing to show yet. */
data object Loading : LoadPhase
/** The load failed and there is no data to fall back to. */
data class Error(val message: String) : LoadPhase
/** The load succeeded but there is nothing to show; [message] explains it. */
data class Empty(val message: String) : LoadPhase
/** The loaded content. */
data object Content : LoadPhase
}
/**
* Classifies a list pane's [LoadPhase] from its load signals. A failed load with
* no data wins, then a load with no data, then an empty result; anything else is
* [LoadPhase.Content]. [emptyMessage] is non-null exactly when the pane is empty
* and is the text to show, captured so a fading-out empty branch never reads a
* changed query.
*
* When stale data is present neither loading nor an error replaces it: the
* screen keeps showing the data and surfaces the error inline, so [hasData]
* suppresses both.
*/
fun listLoadPhase(
loading: Boolean,
error: String?,
hasData: Boolean,
emptyMessage: String?,
): LoadPhase = when {
error != null && !hasData -> LoadPhase.Error(error)
loading && !hasData -> LoadPhase.Loading
emptyMessage != null -> LoadPhase.Empty(emptyMessage)
else -> LoadPhase.Content
}
/**
* Classifies a detail pane's [LoadPhase]. There is no empty state — a detail with
* no data is either an error or still loading — so data present means [Content],
* then a reported error, then [LoadPhase.Loading].
*/
fun detailLoadPhase(error: String?, hasData: Boolean): LoadPhase = when {
hasData -> LoadPhase.Content
error != null -> LoadPhase.Error(error)
else -> LoadPhase.Loading
}
/**
* Cross-fades a pane between its [LoadPhase] branches with the shared
* [MotionStandard] token. Both branches are laid out in the same box, so the
* resting layout is exactly the branch's own layout while the outgoing phase
* fades out under the incoming one: loading never flashes into content and an
* empty or error state fades in and out rather than popping. With system
* animations disabled Compose maps the platform animator duration scale onto the
* spec, so the swap is instant.
*/
@Composable
fun StateCrossfade(
phase: LoadPhase,
modifier: Modifier = Modifier,
content: @Composable (LoadPhase) -> Unit,
) {
Crossfade(
targetState = phase,
animationSpec = MotionStandard.spec(),
modifier = modifier,
label = "load-phase",
) { content(it) }
}
/** Renders a labelled key/value row used across detail screens. */
@Composable
fun KeyValueRow(
@@ -53,13 +53,17 @@ import net.awan.pipelines.ui.components.DetailTabBody
import net.awan.pipelines.ui.components.DetailTabs
import net.awan.pipelines.ui.components.ErrorState
import net.awan.pipelines.ui.components.KeyValueRow
import net.awan.pipelines.ui.components.LoadPhase
import net.awan.pipelines.ui.components.LoadingState
import net.awan.pipelines.ui.components.PaneIdentity
import net.awan.pipelines.ui.components.StateCrossfade
import net.awan.pipelines.ui.components.animateListItem
import net.awan.pipelines.ui.components.paneSafeArea
import net.awan.pipelines.ui.components.collapsibleSection
import net.awan.pipelines.ui.components.copyText
import net.awan.pipelines.ui.components.detailLoadPhase
import net.awan.pipelines.ui.components.pipelineDetailTabs
import net.awan.pipelines.ui.components.rememberPaneContent
import net.awan.pipelines.ui.components.rememberSectionExpansion
import net.awan.pipelines.ui.graph.StageFieldRow
import net.awan.pipelines.ui.graph.WorkflowGraph
@@ -89,6 +93,11 @@ fun PipelineDetailScreen(
val detail = state.data?.takeIf { it.id == pipelineId }
// The cross-fading content branch renders the last non-null detail, so
// switching to another pipeline (which nulls the id-filtered detail) fades
// the old content out instead of blanking it.
val shownDetail = rememberPaneContent(detail)
// A covered pane shows its compact identity only; the tab rail and section
// body must never be drawn squeezed into the peek strip.
if (covered) {
@@ -123,29 +132,36 @@ fun PipelineDetailScreen(
)
},
) { padding ->
when {
detail == null && state.error != null -> ErrorState(
state.error!!,
onRetry = { vm.loadPipeline(pipelineId, version) },
modifier = Modifier.padding(padding),
)
detail == null -> LoadingState("Loading pipeline…", Modifier.padding(padding))
else -> DetailTabs(
tabs = pipelineDetailTabs,
selectedIndex = selectedSection,
onSelect = onSelectSection,
wide = wide,
modifier = Modifier.padding(padding).fillMaxSize(),
) { tab, contentModifier ->
// Only the selected section's content is composed at rest.
when (tab.key) {
"summary" -> PipelineSummaryTab(detail, wide, contentModifier)
"graph" -> PipelineGraphTab(
detail = detail,
modifier = contentModifier,
onStageTap = onOpenStage,
)
else -> PipelineSourceTab(detail, contentModifier)
StateCrossfade(
phase = detailLoadPhase(state.error, hasData = detail != null),
modifier = Modifier.fillMaxSize(),
) { phase ->
when (phase) {
is LoadPhase.Error -> ErrorState(
phase.message,
onRetry = { vm.loadPipeline(pipelineId, version) },
modifier = Modifier.padding(padding),
)
LoadPhase.Loading -> LoadingState("Loading pipeline…", Modifier.padding(padding))
else -> if (shownDetail != null) {
DetailTabs(
tabs = pipelineDetailTabs,
selectedIndex = selectedSection,
onSelect = onSelectSection,
wide = wide,
modifier = Modifier.padding(padding).fillMaxSize(),
) { tab, contentModifier ->
// Only the selected section's content is composed at rest.
when (tab.key) {
"summary" -> PipelineSummaryTab(shownDetail, wide, contentModifier)
"graph" -> PipelineGraphTab(
detail = shownDetail,
modifier = contentModifier,
onStageTap = onOpenStage,
)
else -> PipelineSourceTab(shownDetail, contentModifier)
}
}
}
}
}
@@ -36,9 +36,12 @@ import net.awan.pipelines.ui.AppViewModel
import net.awan.pipelines.ui.components.EmptyState
import net.awan.pipelines.ui.components.ErrorState
import net.awan.pipelines.ui.components.ListRow
import net.awan.pipelines.ui.components.LoadPhase
import net.awan.pipelines.ui.components.LoadingState
import net.awan.pipelines.ui.components.PaneIdentity
import net.awan.pipelines.ui.components.StateCrossfade
import net.awan.pipelines.ui.components.animateListItem
import net.awan.pipelines.ui.components.listLoadPhase
import net.awan.pipelines.ui.components.paneSafeArea
import net.awan.pipelines.ui.theme.pipelinesText
@@ -92,30 +95,36 @@ fun PipelineListScreen(
singleLine = true,
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp),
)
when {
state.loading && state.data == null -> LoadingState("Loading pipelines…")
state.error != null && state.data == null -> ErrorState(state.error!!, onRetry = vm::refreshPipelines)
else -> {
val items = (state.data ?: emptyList())
.filter { it.name.contains(query, ignoreCase = true) }
if (items.isEmpty()) {
EmptyState(
if (query.isBlank()) "No pipelines are defined on this server."
else "No pipelines match \"$query\".",
)
val pipelines = (state.data ?: emptyList())
.filter { it.name.contains(query, ignoreCase = true) }
StateCrossfade(
phase = listLoadPhase(
loading = state.loading,
error = state.error,
hasData = state.data != null,
emptyMessage = if (pipelines.isEmpty()) {
if (query.isBlank()) "No pipelines are defined on this server."
else "No pipelines match \"$query\"."
} else {
LazyColumn(
modifier = Modifier.fillMaxSize(),
contentPadding = PaddingValues(vertical = 8.dp),
) {
items(items, key = { it.id }) { pipeline ->
PipelineRow(
pipeline = pipeline,
selected = pipeline.id == selectedId,
onClick = { onOpen(pipeline.id, pipeline.version) },
modifier = animateListItem(),
)
}
null
},
),
) { phase ->
when (phase) {
LoadPhase.Loading -> LoadingState("Loading pipelines…")
is LoadPhase.Error -> ErrorState(phase.message, onRetry = vm::refreshPipelines)
is LoadPhase.Empty -> EmptyState(phase.message)
LoadPhase.Content -> LazyColumn(
modifier = Modifier.fillMaxSize(),
contentPadding = PaddingValues(vertical = 8.dp),
) {
items(pipelines, key = { it.id }) { pipeline ->
PipelineRow(
pipeline = pipeline,
selected = pipeline.id == selectedId,
onClick = { onOpen(pipeline.id, pipeline.version) },
modifier = animateListItem(),
)
}
}
}
@@ -57,14 +57,18 @@ import net.awan.pipelines.ui.components.DetailTabBody
import net.awan.pipelines.ui.components.DetailTabs
import net.awan.pipelines.ui.components.ErrorState
import net.awan.pipelines.ui.components.KeyValueRow
import net.awan.pipelines.ui.components.LoadPhase
import net.awan.pipelines.ui.components.LoadingState
import net.awan.pipelines.ui.components.PaneIdentity
import net.awan.pipelines.ui.components.StateCrossfade
import net.awan.pipelines.ui.components.animateListItem
import net.awan.pipelines.ui.components.paneSafeArea
import net.awan.pipelines.ui.components.RunStatusIcon
import net.awan.pipelines.ui.components.StatusChip
import net.awan.pipelines.ui.components.collapsibleSection
import net.awan.pipelines.ui.components.clampTabIndex
import net.awan.pipelines.ui.components.detailLoadPhase
import net.awan.pipelines.ui.components.rememberPaneContent
import net.awan.pipelines.ui.components.rememberSectionExpansion
import net.awan.pipelines.ui.components.runDetailTabs
import net.awan.pipelines.ui.components.runStateOf
@@ -116,6 +120,13 @@ fun RunDetailScreen(
val run = state.data?.takeIf { it.id == runId }
val executing = run?.executing == true
// The cross-fading content branch renders the last non-null run, so
// switching to another run (which nulls the id-filtered run) fades the old
// content out instead of blanking it. The artifact groups are derived from
// the same snapshot, so the outgoing grid keeps its cells; the selection is
// reseeded per run id, so a stale candidate set is never selectable.
val shownRun = rememberPaneContent(run)
// Bounded polling: only while the run is executing, stopped on terminal status.
LaunchedEffect(runId, executing) {
if (executing) {
@@ -128,7 +139,7 @@ fun RunDetailScreen(
// Aggregated once per step-list change; it drives the grid and the
// selection's copy/share actions, never recomputed per recomposition.
val artifactGroups = remember(run?.steps) { runArtifactGroups(run?.steps.orEmpty()) }
val artifactGroups = remember(shownRun?.steps) { runArtifactGroups(shownRun?.steps.orEmpty()) }
val candidateArtifacts = remember(artifactGroups) {
artifactGroups.flatMap { it.artifacts }.distinctBy { it.url }
}
@@ -182,65 +193,70 @@ fun RunDetailScreen(
)
},
) { selection, onSelectionChange, padding ->
when {
run == null && state.error != null -> ErrorState(
state.error!!,
onRetry = { vm.loadRun(runId) },
modifier = Modifier.padding(padding),
)
run == null -> LoadingState("Loading run…", Modifier.padding(padding))
else -> {
DetailTabs(
tabs = runDetailTabs,
selectedIndex = selectedSection,
onSelect = onSelectSection,
wide = wide,
modifier = Modifier.padding(padding).fillMaxSize(),
) { tab, contentModifier ->
// Only the selected section's content is composed at rest.
when (tab.key) {
"summary" -> RunSummaryTab(
run = run,
wide = wide,
modifier = contentModifier,
configuredServerUrl = configuredServerUrl,
fetchArtifactBytes = vm::fetchArtifactBytes,
artifactGroups = artifactGroups,
selection = selection,
onSelectionChange = onSelectionChange,
)
"graph" -> RunGraphTab(
run = run,
modifier = contentModifier,
onStageTap = onOpenStage,
)
"input" -> RunFieldSectionsTab(
emptyMessage = "No input recorded.",
keyPrefix = "input",
entries = run.input.orEmpty().toList(),
wide = wide,
modifier = contentModifier,
)
"output" -> RunFieldSectionsTab(
emptyMessage = "No output recorded.",
keyPrefix = "output",
entries = run.output.orEmpty().toList(),
wide = wide,
modifier = contentModifier,
)
"evidence" -> RunEvidenceTab(
evidenceState = evidenceState,
steps = run.steps,
onLoadEvidence = { vm.loadEvidence(runId) },
wide = wide,
modifier = contentModifier,
)
else -> RunTimelineTab(
run = run,
wide = wide,
modifier = contentModifier,
onStageTap = onOpenStage,
)
StateCrossfade(
phase = detailLoadPhase(state.error, hasData = run != null),
modifier = Modifier.fillMaxSize(),
) { phase ->
when (phase) {
is LoadPhase.Error -> ErrorState(
phase.message,
onRetry = { vm.loadRun(runId) },
modifier = Modifier.padding(padding),
)
LoadPhase.Loading -> LoadingState("Loading run…", Modifier.padding(padding))
else -> if (shownRun != null) {
DetailTabs(
tabs = runDetailTabs,
selectedIndex = selectedSection,
onSelect = onSelectSection,
wide = wide,
modifier = Modifier.padding(padding).fillMaxSize(),
) { tab, contentModifier ->
// Only the selected section's content is composed at rest.
when (tab.key) {
"summary" -> RunSummaryTab(
run = shownRun,
wide = wide,
modifier = contentModifier,
configuredServerUrl = configuredServerUrl,
fetchArtifactBytes = vm::fetchArtifactBytes,
artifactGroups = artifactGroups,
selection = selection,
onSelectionChange = onSelectionChange,
)
"graph" -> RunGraphTab(
run = shownRun,
modifier = contentModifier,
onStageTap = onOpenStage,
)
"input" -> RunFieldSectionsTab(
emptyMessage = "No input recorded.",
keyPrefix = "input",
entries = shownRun.input.orEmpty().toList(),
wide = wide,
modifier = contentModifier,
)
"output" -> RunFieldSectionsTab(
emptyMessage = "No output recorded.",
keyPrefix = "output",
entries = shownRun.output.orEmpty().toList(),
wide = wide,
modifier = contentModifier,
)
"evidence" -> RunEvidenceTab(
evidenceState = evidenceState,
steps = shownRun.steps,
onLoadEvidence = { vm.loadEvidence(runId) },
wide = wide,
modifier = contentModifier,
)
else -> RunTimelineTab(
run = shownRun,
wide = wide,
modifier = contentModifier,
onStageTap = onOpenStage,
)
}
}
}
}
@@ -51,9 +51,12 @@ import net.awan.pipelines.ui.components.RunStatusChip
import net.awan.pipelines.ui.components.RunStatusIcon
import net.awan.pipelines.ui.components.ErrorState
import net.awan.pipelines.ui.components.ListRow
import net.awan.pipelines.ui.components.LoadPhase
import net.awan.pipelines.ui.components.LoadingState
import net.awan.pipelines.ui.components.PaneIdentity
import net.awan.pipelines.ui.components.StateCrossfade
import net.awan.pipelines.ui.components.animateListItem
import net.awan.pipelines.ui.components.listLoadPhase
import net.awan.pipelines.ui.components.paneSafeArea
import net.awan.pipelines.ui.components.runStateColor
import net.awan.pipelines.ui.components.runStateOf
@@ -102,22 +105,35 @@ fun RunHistoryScreen(
)
},
) { padding ->
when {
state.loading && state.data == null -> LoadingState("Loading runs…", Modifier.padding(padding))
state.error != null && state.data == null -> ErrorState(
state.error!!,
onRetry = vm::refreshRuns,
modifier = Modifier.padding(padding),
)
else -> RunHistoryContent(
usage = state.data?.usage,
runs = state.data?.runs.orEmpty(),
filterPipelineId = filterPipelineId,
selectedRunId = selectedRunId,
StateCrossfade(
phase = listLoadPhase(
loading = state.loading,
error = state.error,
onOpenRun = onOpenRun,
modifier = Modifier.padding(padding),
)
hasData = state.data != null,
// Runs has no pane-level empty state: an empty list keeps its
// usage block and shows the "no runs" line inside the list, where
// the item animation fades it in and out.
emptyMessage = null,
),
modifier = Modifier.fillMaxSize(),
) { phase ->
when (phase) {
LoadPhase.Loading -> LoadingState("Loading runs…", Modifier.padding(padding))
is LoadPhase.Error -> ErrorState(
phase.message,
onRetry = vm::refreshRuns,
modifier = Modifier.padding(padding),
)
else -> RunHistoryContent(
usage = state.data?.usage,
runs = state.data?.runs.orEmpty(),
filterPipelineId = filterPipelineId,
selectedRunId = selectedRunId,
error = state.error,
onOpenRun = onOpenRun,
modifier = Modifier.padding(padding),
)
}
}
}
}
@@ -160,7 +176,7 @@ private fun RunHistoryContent(
"No runs have been recorded yet.",
style = pipelinesText().body,
color = MaterialTheme.colorScheme.onSurfaceVariant,
modifier = Modifier.padding(24.dp),
modifier = animateListItem(Modifier.padding(24.dp)),
)
}
} else {
@@ -182,7 +198,7 @@ private fun RunHistoryContent(
error,
color = MaterialTheme.colorScheme.error,
style = pipelinesText().body,
modifier = Modifier.padding(16.dp),
modifier = animateListItem(Modifier.padding(16.dp)),
)
}
}
@@ -0,0 +1,108 @@
package net.awan.pipelines.ui.components
import org.junit.Assert.assertEquals
import org.junit.Test
/**
* The load-phase classification is pure, so the rules the cross-fading screens
* rely on are checked here without a device: a failed load with no data wins over
* loading, a load with no data is loading, an empty result is empty, and stale
* data always keeps the pane on content (the screen shows the error inline
* instead). Detail panes have no empty state, so absent data is an error or a
* load. The message-carrying phases keep the text captured at classification.
*/
class LoadPhaseTest {
// ---- list panes ---------------------------------------------------------
@Test
fun listReportsAnErrorBeforeLoadingWhenThereIsNoData() {
assertEquals(
LoadPhase.Error("boom"),
listLoadPhase(loading = true, error = "boom", hasData = false, emptyMessage = null),
)
}
@Test
fun listReportsLoadingWhenEmptyAndFailedAreBothAbsent() {
assertEquals(
LoadPhase.Loading,
listLoadPhase(loading = true, error = null, hasData = false, emptyMessage = null),
)
}
@Test
fun listReportsEmptyWithItsCapturedMessage() {
assertEquals(
LoadPhase.Empty("No pipelines match \"x\"."),
listLoadPhase(loading = false, error = null, hasData = true, emptyMessage = "No pipelines match \"x\"."),
)
}
@Test
fun listReportsContentWhenThereAreRows() {
assertEquals(
LoadPhase.Content,
listLoadPhase(loading = false, error = null, hasData = true, emptyMessage = null),
)
}
@Test
fun listKeepsStaleDataOnContentDespiteLoadingOrError() {
// A refresh keeps the data on screen and surfaces the error inline.
assertEquals(
LoadPhase.Content,
listLoadPhase(loading = true, error = null, hasData = true, emptyMessage = null),
)
assertEquals(
LoadPhase.Content,
listLoadPhase(loading = false, error = "offline", hasData = true, emptyMessage = null),
)
}
@Test
fun listWithoutAnySignalStaysOnContent() {
// A runs pane deliberately has no pane-level empty state, so its initial
// no-data/no-error frame is content (the empty line lives in the list).
assertEquals(
LoadPhase.Content,
listLoadPhase(loading = false, error = null, hasData = false, emptyMessage = null),
)
}
// ---- detail panes -------------------------------------------------------
@Test
fun detailReportsContentWheneverDataIsPresent() {
assertEquals(
LoadPhase.Content,
detailLoadPhase(error = "offline", hasData = true),
)
}
@Test
fun detailReportsAnErrorWhenThereIsNoData() {
assertEquals(
LoadPhase.Error("boom"),
detailLoadPhase(error = "boom", hasData = false),
)
}
@Test
fun detailReportsLoadingWhenThereIsNoDataAndNoError() {
assertEquals(
LoadPhase.Loading,
detailLoadPhase(error = null, hasData = false),
)
}
// ---- the phases themselves ---------------------------------------------
@Test
fun messagePhasesCompareByTheirCapturedText() {
assertEquals(LoadPhase.Error("a"), LoadPhase.Error("a"))
assertEquals(LoadPhase.Empty("b"), LoadPhase.Empty("b"))
assertEquals(LoadPhase.Loading, LoadPhase.Loading)
assertEquals(LoadPhase.Content, LoadPhase.Content)
}
}