UiState

data class UiState<T : Any>(val data: T, val loading: Boolean = false, val error: OneTimeEvent<Throwable?> = OneTimeEvent(null))

Wrapper class representing the complete state of a UI screen.

This is the standard state container used across all ViewModels in the application. It combines screen data with loading and error states in a single, immutable structure.

Key Features

  • Type-safe data: Generic type parameter ensures compile-time safety

  • Loading state: Boolean flag for showing/hiding loading indicators

  • One-time errors: Errors are delivered as events that are handled only once

Usage in ViewModel

@HiltViewModel
class FeatureViewModel @Inject constructor(
private val repository: FeatureRepository
) : ViewModel() {
private val _uiState = MutableStateFlow(UiState(FeatureScreenData()))
val uiState = _uiState.asStateFlow()
}

State Updates

Use the provided extension functions to update state:

Parameters

T

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

See also

Constructors

Link copied to clipboard
constructor(data: T, loading: Boolean = false, error: OneTimeEvent<Throwable?> = OneTimeEvent(null))

Properties

Link copied to clipboard
val data: T

The current screen data. This is always present (never null).

Link copied to clipboard

A one-time event containing an error. Automatically consumed after being handled.

Link copied to clipboard

True when an async operation is in progress. Used to show loading indicators.