Skip to content
Abhi Muktheeswarar edited this page Jul 6, 2021 · 3 revisions
interface Action

An action represents what we want to do, what we want to achieve. Actions are the only way to update the state. An Action can be triggered by the user on clicking a button in the UI, or from a SideEffect, or Middleware.

sealed interface MovieAction : Action {

    object GetMoviesAction : MovieAction
    
    data class MoviesLoadedAction(val movies: List<Movie>) : MovieAction
    
    data class ErrorLoadingMoviesAction(val error: Exception) : MovieAction
    
}

We recommend Action to be an immutable data class or object.

Everything is through actions

For example:

  1. On opening the app, we dispatch GetMoviesAction.
  2. A SideEffect on receiving theGetMoviesAction, makes an API call.
  3. Once the API response is received, the SideEffect dispatches MoviesLoadedAction if the network call is a success, otherwise dispatches ErrorLoadingMoviesAction.

Dispatch

typealias Dispatch = (Action) -> Unit

Dispatch is just a simple function that accepts an Action to dispatch it to a StateReserve.

Predefined Actions

Flywheel comes with a few predefined actions:

  • EventAction: For one-off events like showing a toast.
  • NavigateAction : To handle navigation-related flows.
  • SkipReducer: can be used along with a skipMiddleware to prevent the Action from reaching a reducer.

All the above actions are just an empty interface. It is provided to distinguish or do something based on types.

Clone this wiki locally