Skip to content

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).

Clone this wiki locally