ScreenViewModel

abstract class ScreenViewModel<S, E : Any>(initialState: S) : ViewModel(source)

Base ViewModel for a screen: owns its state S, handles its events E, and queues the messages it shows.

S is whatever the feature declares, usually a data class. The status of each piece of content that loads is a field of it, often a Loadable, so a screen that loads several things gives each its own status, and a screen that starts with its content needs none. Every change goes through setState, one atomic update, so coroutines writing at the same time never drop each other's change.

Messages are kept apart from S because every screen shows them the same way: showMessage queues one and MessageHost shows the queue in order, calling messageShown as each leaves. Being state rather than a one-off event, a message survives a configuration change.

Subclasses implement handleEvent; onEvent is the entry point the UI calls.

Inheritors

Constructors

Link copied to clipboard
constructor(initialState: S)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
val messages: StateFlow<ImmutableList<UiMessage>>

Messages waiting to be shown, oldest first.

Link copied to clipboard
val state: StateFlow<S>

Functions

Link copied to clipboard
open fun addCloseable(closeable: AutoCloseable)
fun addCloseable(key: String, closeable: AutoCloseable)
Link copied to clipboard
Link copied to clipboard

Removes the message with id from the queue, once it has been shown.

Link copied to clipboard
fun onEvent(event: E)