KViewModel
open class KViewModel<State>(initialState: State, application: Application) : AndroidViewModel, KoinComponent(source)
Legacy base ViewModel for Kindling applications that require AndroidViewModel or KoinComponent integration.
KViewModel is the original single-type-parameter tier of the Kindling ViewModel hierarchy. It provides state management, one-shot events, and async helpers while remaining compatible with Koin's by inject() delegation and the Application context.
This ViewModel follows a simplified MVI (Model-View-Intent) pattern where:
State is a single observable object (usually a data class).
Events are one-shot side effects (navigation, toasts, etc.).
When to use which ViewModel
| Scenario | Recommended base |
|---|---|
Needs Application context or Koin by inject() | KViewModel ← you are here |
| Simple screen, no framework coupling needed | dev.kindling.compose.legacy.KSimpleViewModel |
| Feature screen with a typed event contract | KViewModel (two-param) |
| Strict unidirectional MVI flow | KMviViewModel |
Basic usage
data class ProfileState(
val isLoading: Boolean = true,
val user: User? = null
)
sealed interface ProfileEvent {
data class ShowToast(val message: String) : ProfileEvent
}
class ProfileViewModel(
application: Application,
private val repository: UserRepository
) : KViewModel<ProfileState>(
initialState = ProfileState(),
application = application
) {
// Koin injection supported
private val analytics: AnalyticsService by inject()
init {
loadProfile()
}
private fun loadProfile() {
fetchData(
source = { repository.getUser() },
onResult = { result ->
result
.onSuccess { user ->
updateState { copy(user = user, isLoading = false) }
analytics.logEvent("profile_loaded")
}
.onFailure {
updateState { copy(isLoading = false) }
sendEvent(ProfileEvent.ShowToast("Failed to load profile"))
}
}
)
}
fun onLogoutClick() {
sendEvent(AppScreen.Login) // Automatic navigation if AppScreen implements KDestination
}
}Content copied to clipboard
Parameters
initialState
The state emitted immediately to all collectors on subscription.
application
The Application instance, forwarded to AndroidViewModel.
Type Parameters
State
The UI state type. Prefer immutable data classes with default values.