-
Notifications
You must be signed in to change notification settings - Fork 3
2. StateViewModel
Gabriel Brasileiro edited this page Jun 13, 2020
·
6 revisions
StateViewModel manages interactions with Reducer. In your base we have the implementation of StateView<State> interface to can use onStateChanged auxiliar extension.
The implementation of StateViewModel<State, StateEvent> need receive Reducer<State, StateEvent>(With State and StateEvent interfaces) to works correctly.
When all is fine you can update your State using the updateTo(StateEvent) method passing your respective StateEvent.
State implementation checklist:
- Extend your event implementation from the Event interface;
- Extend your state event implementation from the StateEvent interface.
See ReducerScope, State and StateEvent doc to understand better.
Example:
class PersonViewModel(
reducer: Reducer<PersonData, PersonStateEvent>
// private val anyUseCase: AnyUseCase
) : StateViewModel<PersonData, PersonStateEvent>(reducer) {
fun saveName(name: String) {
if (name != requireStateValue().name) {
updateTo(PersonStateEvent.UpdateName(name))
}
}
fun saveAge(age: String) {
if (age != requireStateValue().age) {
updateTo(PersonStateEvent.UpdateAge(age))
}
}
}*Method requireStateValue() inside of core.
Read about listener extensions to process LiveData of your view models with simple way.