RouterViewModel
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
The TransformerSetLoader that provides the transformers for the router
Configuration options for the router setup
Constructors
Convenience constructor that accepts a Set of Transformers directly.
Functions
Processes a Transmission.Effect through the router.
Processes a Transmission.Signal through the router.