BaseViewModelWithEffect

abstract class BaseViewModelWithEffect<S : UiState, A : UiAction, E>(initialState: S) : BaseViewModel<S, A> (source)

Base class for ViewModels that manages UI state, handles user actions, and emits side effects.

This class extends BaseViewModel by adding support for emitting side effects via a SharedFlow. Side effects are typically used for actions that should not directly mutate the UI state but trigger external events, such as navigation or showing a dialog.

Parameters

S

The type of UI state that this ViewModel manages. It should implement UiState.

A

The type of user actions (intents) that this ViewModel responds to. It should implement UiAction.

E

The type of side effects that this ViewModel emits.

Constructors

Link copied to clipboard
constructor(initialState: S)

Properties

Link copied to clipboard
val sideEffects: SharedFlow<E>
Link copied to clipboard
val state: StateFlow<S>

Functions

Link copied to clipboard
expect open fun addCloseable(closeable: AutoCloseable)
expect fun addCloseable(key: String, closeable: AutoCloseable)
Link copied to clipboard
expect fun <T : AutoCloseable> getCloseable(key: String): T?
Link copied to clipboard
fun handleAction(action: A)

Handles a user action and updates the state accordingly using the provided onAction method.

Link copied to clipboard
abstract suspend fun onAction(action: A)

Abstract method to be implemented by subclasses to handle the specific action logic.