StatefulComposable

fun <T : Any> StatefulComposable(state: UiState<T>, onShowSnackbar: suspend (String, SnackbarAction, Throwable?) -> Boolean, content: @Composable (T) -> Unit)

A centralized composable wrapper that handles loading states, error handling, and content display.

This composable implements the standard UI state pattern used throughout the application:

  • Displays content based on the current state data

  • Shows an overlay loading indicator when UiState.loading is true

  • Automatically displays errors via snackbar when UiState.error contains an unhandled exception

Usage Example

@Composable
fun FeatureRoute(
onShowSnackbar: suspend (String, SnackbarAction, Throwable?) -> Boolean,
viewModel: FeatureViewModel = hiltViewModel()
) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()

StatefulComposable(
state = uiState,
onShowSnackbar = onShowSnackbar
) { screenData ->
FeatureScreen(
screenData = screenData,
onAction = viewModel::handleAction
)
}
}

Pattern

This composable enforces a clean separation of concerns:

  1. Route composable: Manages ViewModel and state collection (uses StatefulComposable)

  2. Screen composable: Pure UI that receives data and event callbacks

Parameters

state

The current UI state containing data, loading flag, and error events.

onShowSnackbar

Callback to display error messages. Returns true if the error was handled.

content

The screen content to display. Receives the current data from state.

Type Parameters

T

The type of the screen data. Must be a non-nullable type.

See also