Skip to content
Marcel Pintó Biescas edited this page Dec 26, 2015 · 4 revisions

Setup

First step is to define what the store wants to make public. Create a new interface.

public interface RepositoriesStoreInterface {

  ArrayList<GitHubRepo> getRepositories();

}

Now create a Store that extends RxStore

public class RepositoriesStore extends RxStore implements RepositoriesStoreInterface {
    ...
}

RxStore is an abstract class that will handle the subscription with the dispatcher in order to receive the actions. To retrive this actions we need to implement the onRxAction

public class RepositoriesStore extends RxStore implements RepositoriesStoreInterface {

  public RepositoriesStore(Dispatcher dispatcher) {
    super(dispatcher);
  }

  @Override
  public void onRxAction(RxAction action) {

  }
}

The constructor needs the Dispatcher as a parameter in order to subscribe to it. The onRxAction method will get all the action dispatched by the Dispatcher. In order to filter them the best way is using a switch clause.

  @Override
  public void onRxAction(RxAction action) {
    switch (action.getType()) {
      case Actions.GET_PUBLIC_REPOS:
        this.gitHubRepos = action.get(Keys.PUBLIC_REPOS);
        break;
      default: // IMPORTANT if we don't modify the store just ignore
        return;
    }
    postChange(new RxStoreChange(ID, action));
  }

Because the store will receive all type of actions we apply logic only to those we care about. In order to do that we retrieve the data from the action, we apply the desired logic and we post a store change so the Views get notified. To post a store change we will use the given method postChange. This method will post a store change with the given Store id (important to identify from which store the change cames) and the action received.

Last step is to implement the public interface in order to provide the data we dessired.

  @Override
  public ArrayList<GitHubRepo> getRepositories() {
    return gitHubRepos == null ? new ArrayList<GitHubRepo>() : gitHubRepos;
  }

The view can then request, when it receive a change or whenever its need the data of this store using the public interface.


Next Setup View

Clone this wiki locally