2. Actions

James Shvarts edited this page Dec 11, 2018 · 1 revision

Actions (aka Intentions) are the result of the User interacting with the UI. Each Action gets dispatched to its corresponding ViewModel. An action is the only trigger that can initiate the data flow cycle which produces new States.

Actions are implemented as Kotlin sealed classes. Here are the Actions applicable to the Note Detail screen in the sample app:

    sealed class Action : BaseAction {
        data class LoadNoteDetail(val noteId: Long) : Action()
        data class DeleteNote(val noteId: Long) : Action()
    }

Note that each Action above takes a noteId required for the ViewModel to do its work. It's not always the case. If no parameters are applicable, we can simply do:

    sealed class Action : BaseAction {
        object LoadNotes : Action()
    }

The Actions are sent to the ViewModel via a single pipeline (dispatch(action)) API:

    viewModel.dispatch(Action.LoadNoteDetail(noteId)) 

Having a single pipeline for dispatching Actions makes the code much easier to manage and simplifies our unit testing (as you will see later).

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.