UiState

sealed interface UiState<out T>

A generic sealed interface representing the different states of an asynchronous operation or data loading process. Annotated with @Immutable for Jetpack Compose stability and optimal recomposition behavior.

Type Parameters

T

The type of data held by the Success state.

Inheritors

Types

Link copied to clipboard
data class Failure(val throwable: Throwable) : UiState<Nothing>

Indicates an operation failure holding the thrown throwable.

Link copied to clipboard
data object Idle : UiState<Nothing>

Indicates the initial state before any operation has started.

Link copied to clipboard
data object Loading : UiState<Nothing>

Indicates that an operation is currently in progress.

Link copied to clipboard
data class Success<out T>(val data: T) : UiState<T>

Indicates successful completion holding the resulting data.

Properties

Link copied to clipboard
open val isFailure: Boolean
Link copied to clipboard
open val isIdle: Boolean
Link copied to clipboard
open val isLoading: Boolean
Link copied to clipboard
open val isSuccess: Boolean

Functions

Link copied to clipboard
inline fun <T> UiState<T>.Content(crossinline onSuccess: @Composable (data: T) -> Unit, noinline onLoading: @Composable () -> Unit? = null, noinline onIdle: @Composable () -> Unit? = null, noinline onFailure: @Composable (throwable: Throwable) -> Unit? = null)

Exhaustively renders UI content based on the current UiState.

Link copied to clipboard

Returns the encapsulated throwable if this state is UiState.Failure, or null otherwise.

Link copied to clipboard
inline fun <T, R> UiState<T>.flatMap(transform: (T) -> UiState<R>): UiState<R>

Transforms the UiState.Success data using the given transform function that returns a new UiState.

Link copied to clipboard
inline fun <T, R> UiState<T>.fold(onSuccess: (data: T) -> R, onFailure: (throwable: Throwable) -> R, onLoading: () -> R, onIdle: () -> R): R
Link copied to clipboard
fun <T> UiState<T>.getOrDefault(default: T): T

Returns the encapsulated data if this state is UiState.Success, or default otherwise.

Link copied to clipboard
inline fun <T> UiState<T>.getOrElse(onFailure: (throwable: Throwable?) -> T): T

Returns the encapsulated data if this state is UiState.Success, or the result of onFailure function for non-success states.

Link copied to clipboard
fun <T> UiState<T>.getOrNull(): T?

Returns the encapsulated data if this state is UiState.Success, or null otherwise.

Link copied to clipboard
fun <T> UiState<T>.getOrThrow(): T

Returns the encapsulated data if this state is UiState.Success, or throws the encapsulated Throwable if this state is UiState.Failure, or an IllegalStateException if UiState.Idle or UiState.Loading.

Link copied to clipboard
inline fun <T, R> UiState<T>.map(transform: (T) -> R): UiState<R>

Transforms the UiState.Success data using the given transform function. Non-success states (UiState.Idle, UiState.Loading, UiState.Failure) are passed through unchanged.

Link copied to clipboard
inline fun <T> UiState<T>.onFailure(block: (throwable: Throwable) -> Unit): UiState<T>

Executes block with UiState.Failure.throwable if the UiState is UiState.Failure. Returns this to allow chaining.

Link copied to clipboard
inline fun <T> UiState<T>.onIdle(block: () -> Unit): UiState<T>

Executes block if the UiState is UiState.Idle. Returns this to allow chaining.

Link copied to clipboard
inline fun <T> UiState<T>.onLoading(block: () -> Unit): UiState<T>

Executes block if the UiState is UiState.Loading. Returns this to allow chaining.

Link copied to clipboard
inline fun <T> UiState<T>.onSuccess(block: (data: T) -> Unit): UiState<T>

Executes block with UiState.Success.data if the UiState is UiState.Success. Returns this to allow chaining.