-
Notifications
You must be signed in to change notification settings - Fork 38
View
The view is the responsible of trigger actions, register stores and use its data. The first step is that each activity of our project must implements RxViewDispatch.
onRxStoresRegister will tell RxFlux which store to register.
@Override
public void onRxStoresRegister() {
repositoriesStore = RepositoriesStore.get(SampleApp.get(this).getRxFlux().getDispatcher());
repositoriesStore.register();
}
How we create our store is up to us. In the example we get the instance of the store and we call the method from RxStore called register(). This will register our store into the dispatcher
Note: is safe to call register several times, we will only register once.
onRxStoreChanged will be called every time a store post a RxStoreChange.
@Override
public void onRxStoreChanged(RxStoreChange change) {
switch (change.getStoreId()) {
case RepositoriesStore.ID:
switch (change.getRxAction().getType()) {
case Actions.GET_PUBLIC_REPOS:
adapter.setRepos(repositoriesStore.getRepositories());
break;
}
break;
}
}
RxStoreChange has a store identifier and a RxAction. With these two, the view can perform the desired logic.
onRxError will be called when an Error is posted into the dispatcher.
@Override
public void onRxError(RxError error) {
setLoadingFrame(false);
Throwable throwable = error.getThrowable();
if (throwable != null) {
Snackbar.make(coordinatorLayout, "An error ocurred", Snackbar.LENGTH_INDEFINITE)
.setAction("Retry",
v -> SampleApp.get(this).getGitHubActionCreator().retry(error.getAction()))
.show();
} else {
Toast.makeText(this, "Unknown error", Toast.LENGTH_LONG).show();
}
}
This method will allow us to react when we get an error. Notice that RxError contains the RxAction that was intended. Thus we can use it to retry or show proper information.
Last two methods of RxViewDispatcher are used to register any other view that is not an activity (Fragments, CustomViews, etc)
@Override
public void onRxViewRegistered() {
// If there is any fragment that needs to register store changes we can do it here
}
@Override
public void onRxViewUnRegistered() {
// If there is any fragment that has registered for store changes we can unregister now
}
RxFlux will manage the application life-cycle to register and unregister the views in the correct place avoiding any memory leak. Also will be responsible to clean up any subscription if the app is destroyed.
For more information, take a look at RxFlux.class