Skip to main content

ViewModel

MediatorK works naturally with Android ViewModel — inject the mediator and dispatch requests from viewModelScope.


Basic usage

class UserViewModel(private val mediator: Mediator) : ViewModel() {

val user = MutableStateFlow<User?>(null)

fun load(id: String) {
viewModelScope.launch {
user.value = mediator.send(GetUserQuery(id))
}
}
}

With Koin

Declare the ViewModel in your Koin module and inject the mediator via get():

// koin module
viewModel { UserViewModel(get()) }

Handling validation errors

When ValidationBehavior is in the pipeline, catch ValidationException and surface its message in your UI state:

class CreateTodoViewModel(private val mediator: Mediator) : ViewModel() {

val error = MutableStateFlow<String?>(null)

fun submit(title: String, dueDate: LocalDate) {
viewModelScope.launch {
try {
mediator.send(CreateTodoCommand(title, dueDate))
} catch (e: ValidationException) {
error.value = e.message
}
}
}
}

See Validation for how ValidationBehavior and ValidationException are set up.


Next

Ktor