The Actions

There are several possible actions that our view layer will be able to publish:

In addition, we need a “load” action to load our content when the UI first appears.

Some of these actions have associated data. For example, to add a new to-do item, we need some sort of model object describing the item. Some actions need no additional data, such as the “load” or “unselect-all” actions.

To that end, we have an Action class. This class is abstract, with concrete subclasses for each specific action. This way, some of our code can just deal with actions in general via the Action base type, while the rest of our code can work with the specific action types where needed. Each concrete class can hold whatever data that action needs.

The Action class:

package com.commonsware.android.todo.impl;

import com.google.auto.value.AutoValue;
import java.util.Collections;
import java.util.List;

public abstract class Action {
  public static Action add(ToDoModel model) {
    return(new AutoValue_Action_Add(model));
  }

  public static Action edit(ToDoModel model) {
    return(new AutoValue_Action_Edit(model));
  }

  public static Action delete(List<ToDoModel> models) {
    return(new AutoValue_Action_Delete(Collections.unmodifiableList(models)));
  }

  public static Action delete(ToDoModel model) {
    return(delete(Collections.singletonList(model)));
  }

  public static Action select(int position) {
    return(new AutoValue_Action_Select(position));
  }

  public static Action unselect(int position) {
    return(new AutoValue_Action_Unselect(position));
  }

  public static Action unselectAll() {
    return(new UnselectAll());
  }

  public static Action show(ToDoModel model) {
    return(new AutoValue_Action_Show(model));
  }

  public static Action filter(FilterMode mode) {
    return(new AutoValue_Action_Filter(mode));
  }

  public static Action load() {
    return(new Action.Load());
  }

  @AutoValue
  public static abstract class Add extends Action {
    public abstract ToDoModel model();
  }

  @AutoValue
  public static abstract class Edit extends Action {
    public abstract ToDoModel model();
  }

  @AutoValue
  public static abstract class Delete extends Action {
    public abstract List<ToDoModel> models();
  }

  @AutoValue
  static abstract class Select extends Action {
    public abstract int position();
  }

  @AutoValue
  static abstract class Unselect extends Action {
    public abstract int position();
  }

  static class UnselectAll extends Action {

  }

  @AutoValue
  static abstract class Show extends Action {
    public abstract ToDoModel current();
  }

  @AutoValue
  static abstract class Filter extends Action {
    public abstract FilterMode filterMode();
  }

  public static class Load extends Action {

  }
}

This looks complicated, but it is just a number of occurrences of the same basic pattern. For example, for the “add” action we have an Add subclass of Action, set up via AutoValue:

  @AutoValue
  public static abstract class Add extends Action {
    public abstract ToDoModel model();
  }

We also have a static method named add() on Action to create an instance of an Action.Add:

  public static Action add(ToDoModel model) {
    return(new AutoValue_Action_Add(model));
  }

Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.