RouterViewModel

abstract class RouterViewModel(loader: TransformerSetLoader, config: RouterViewModelConfig = RouterViewModelConfig()) : ViewModel

An abstract ViewModel that simplifies the integration of TransmissionRouter with multiplatform ViewModel architecture.

This class provides a convenient wrapper around TransmissionRouter with automatic setup and lifecycle management. It handles the router initialization, stream collection, and cleanup automatically, allowing developers to focus on implementing their business logic through the provided hooks.

Key Features:

  • Automatic Setup: Router is configured and initialized automatically

  • Lifecycle Management: Router cleanup is handled in onCleared

  • Stream Collection: Data and effect streams are automatically collected in viewModelScope

  • Hook Methods: Override onData, onEffect, onProcessSignal, and onProcessEffect for custom logic

  • Query Support: Access to QueryHandler for querying transformer states

  • StateFlow Helpers: Built-in methods for converting streams to StateFlow

Usage Examples:

Simple Implementation with Transformer Set:

class MyViewModel : RouterViewModel(
setOf(
UserTransformer(),
AuthTransformer(),
DataTransformer()
)
) {
// Create StateFlow from data stream
val userState = streamDataAsState<UserData>(UserData.Empty)
val authState = streamDataAsState<AuthData>(AuthData.LoggedOut)

override fun onData(data: Transmission.Data) {
when (data) {
is UserData -> updateUserState(data)
is AuthData -> updateAuthState(data)
}
}

fun login(credentials: Credentials) {
processSignal(AuthSignal.Login(credentials))
}
}

Implementation with Custom Configuration:

class FeatureViewModel : RouterViewModel(
loader = MyTransformerSetLoader(),
config = RouterViewModelConfig(
capacity = Capacity.High,
dispatcher = Dispatchers.IO
)
) {
override fun onEffect(effect: Transmission.Effect) {
when (effect) {
is NavigationEffect -> handleNavigation(effect)
is ErrorEffect -> showError(effect)
}
}

override fun onError(throwable: Throwable) {
// Handle router errors
logError("Router error", throwable)
}
}

Parameters

loader

The TransformerSetLoader that provides the transformers for the router

config

Configuration options for the router setup

Constructors

Link copied to clipboard
constructor(transformerSet: Set<Transformer>, config: RouterViewModelConfig = RouterViewModelConfig())

Convenience constructor that accepts a Set of Transformers directly.

constructor(loader: TransformerSetLoader, config: RouterViewModelConfig = RouterViewModelConfig())

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

Processes a Transmission.Effect through the router.

Link copied to clipboard

Processes a Transmission.Signal through the router.