Skip to content

Latest commit

 

History

History
50 lines (33 loc) · 1.15 KB

viewmodel.md

File metadata and controls

50 lines (33 loc) · 1.15 KB

ViewModel Structure

Base Type

Generic view model should extend androidx.lifecycle.ViewModel.

State Exposure

State is exposed via state property of type T where T is your custom data class. To propagate changes form ViewModel to compose UI layer use MutableState get/set delegates:

var state by mutableStateOf(MovesSubPageState())
    private set

Note, state's set is private.

Interactions from UI layer

All interactions from UI layer should be done via direct calls to viewmodel.

class ErrorDialogViewModel : ViewModel() {
    
    fun hideError() {  }
}

@Composable
fun ErrorDialog(vm: ErrorDialogViewModel) {
    ...
    DismissButton(onClick = { vm.hideError() })
}

Dependencies

ViewModels accept usually requireUseCase and Navigator objects as dependencies.

ErrorHandling

Use classify function to convert UseCases domain error to UIError. Then consume either value or error with when statement.

state = when (val data = getData(id).classify(MyClass::class)) {
    is Either.Left -> state.copy(error = data.value)
    is Either.Right -> state.copy(value = data.value)
}