BaseViewModel

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

Base class for ViewModels that manages UI state and handles user actions.

This class holds the common logic for state management using StateFlow and provides a mechanism to handle actions asynchronously using Kotlin coroutines.

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.

Inheritors

Constructors

Link copied to clipboard
constructor(initialState: S)

Properties

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.