View Models

Learn about Android Blaze’s View Model to host your business logic.

The BlazeViewModel is used to host the logic of your screen. In order to use it create a new class and extend the BlazeViewModel.

 

  1. Structured business logic.
  1. Event logging. Will print to Logcat every input (user action) and output (state or event) emision.
 

In order to extend the BlazeViewModel pass a type of State, Event and UserAction to its generics. You can then listen to State via the BlazeViewModel.state property.

 

In your UI, use pushUserAction() to forward actions (such as clicks) to your BlazeViewModel. Once an action is forwarded, you will receive it via the processUserAction callback of your BlazeViewModel. If the action results into an update of state use emitState() to emit the new state to your UI. emitEvent() is also available.

 

State vs Event

A State represents the state of your UI and gets persisted across configuration changes. An Event is a fire-and-forget emission you want to signal to your UI (such as a brief update message “message saved”).

Did this answer your question?
😞
😐
🤩