After we are done with defining the application State we are going to define the changes we want to apply to this state. In Suas, the changes are represented as Actions.

In our todo app example, we want to do the following:

  • Add new items.
  • Remove an Item.
  • Order items by moving an item around.
  • Toggle an item completion status.

We define an Action for each of these events. An action is simply a struct that implements the Action protocol in swift and a subclass of Action in Kotlin or Java.

When adding a new todo, we need the todo content:

struct AddTodo: Action {
  let text: String
}
data class AddTodo(val text: String): Action<String>("add_todo", text)
static final String ADD_TODO = "add_todo";

Action getAddTodoAction(String text) {
  return new Action<>(ADD_TODO, text);
}

If we want to delete a todo, we need the todo index so that we can remove that TodoItem from the TodoList.

struct RemoveTodo: Action {
  let index: Int
}
data class RemoveTodo(val index: Int): Action<Int>("remove_todo", index)
static final String REMOVE_TODO = "remove_todo";

Action getRemoveTodoAction(int index) {
  return new Action<>(REMOVE_TODO, index);
}

Moving a todo requires the original index of the todo and its destination index.

struct MoveTodo: Action {
  let from: Int
  let to: Int
}
data class MoveTodo(val from: Int, val to: Int): Action<Unit>("move_todo")
static final String MOVE_TODO = "move_todo";

Action getMoveTodoAction(int from, int to) {
  return new Action<>(MOVE_TODO, new Pair<>(from ,to));
}

Finally, when we want to toggle a todo marking is as completed (or uncompleted if it was completed) we need the index of that item.

struct ToggleTodo: Action {
  let index: Int
}
data class ToggleTodo(val index: Int): Action<Unit>("toggle_todo")
static final String TOGGLE_TODO = "toggle_todo";

Action getToggleTodoAction(int index) {
  return new Action<>(TOGGLE_TODO, index);
}

These 4 actions at a later time are dispatched to the store which sends them to the reducer along with the todo state. The reducer generates a new state and return it.

What's Next

After defining the State and Action the next logical element to create is the reducer.

You can also dispatch actions that contain asynchronous processing. Such as network requests, or reading and writing to disk asynchronously. Check Async Actions to read how to do that.

Related Topics

Todo app example

Other Suas architecture components
Store
Listener

Also, check:
List of example applications built with Suas