Skip to content

Actions

Marcel Pintó Biescas edited this page Dec 26, 2015 · 7 revisions

Setup

Create an interface that contains your Actions.

public interface Actions {

  String GET_PUBLIC_REPOS = "get_public_repos";
  String GET_USER = "get_user";

  void getPublicRepositories();

  void getUserDetails(String userId);
}

Create a RxActionCreator class that will create RxAction according to the previous interface

public class GitHubActionCreator extends RxActionCreator implements Actions { 

  /**
   * If you want to give more things to the constructor like API or Preferences or any other
   * parameter you can buy make sure to call super(dispatcher, manager)
   */
  public GitHubActionCreator(Dispatcher dispatcher, SubscriptionManager manager) {
    super(dispatcher, manager);
  }

  // Below implement the defined Actions
}

Most of the actions are requests to an API, DB or actions that you want to perform. Thus when creating a RxAction you must pass the results of those requests. The recommended way to do so, is creating a Keys interface that will help us retrive the data.

public interface Keys {
  String PUBLIC_REPOS = "repos";
  String USER = "user";
  String ID = "id";
}

Let's create our first action.

  @Override
  public void getUserDetails(String userId) {
    final RxAction action = newRxAction(GET_USER, Keys.ID, userId);
    ...
  }

The method newRxAction will help you to create a new RxAction. Pass the Action Type and the pair-value parameters (Key - Object).

Now we have a RxAction we can make a request or some logic in order to retrive the needed action result.

  @Override
  public void getUserDetails(String userId) {
    final RxAction action = newRxAction(GET_USER, Keys.ID, userId);
    addRxAction(action, NetworkManager.getApi()
        .getUser(userId)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(user -> {
          action.getData().put(USER, user);
          postRxAction(action);
        }, throwable -> postError(action, throwable)));
  }

Here is were we apply RxJava, we create a new Observable and we subscribe to it. This subscription should be added to our SubscriptionManager. (Go to SubscriptionManager section for more info).

When this subscription returns a valid data we will put that into our RxAction using a Key. Then we should post it to the dispatcher, using the predefined method postRxAction

In case of error we will post an Error to the dispatcher, using the predefined method postError (Go to Error Handling section for more info).

Extras:

Avoid duplicated requests. When you add a Subscription into the SubscriptionManager, this subscription will remain there till it's unsubscribed. Use the hasRxAction(RxAction) method to check if we have an ongoing subscription.

  @Override
  public void getUserDetails(String userId) {
    final RxAction action = newRxAction(GET_USER, Keys.ID, userId);
    if (hasRxAction(action)) return; // Return or cancel previous 
    ...
   }

Notice that this hasRxAction internally will check if the subscription exists and if it's still subscribed.


Next Setup Store

Clone this wiki locally