-
Notifications
You must be signed in to change notification settings - Fork 4
Simple Usage
From RxJava: One of the most common operations when dealing with asynchronous tasks on Android is to observe the task's
result or outcome on the main UI thread. Using vanilla Android, this would
typically be accomplished with an AsyncTask.
Using RxJava You would use an Observable, the problem people often run into is that it couples the caller and the receiver.
Using EventService, you can have a fully decoupled MVC model:
You will first create an "Action" class that takes in what input variables and output events it emits.
@EventProducer(generated = {
@Event
})
@Action
@ActionHelper
public class LikePostAction implements Action {
@Override
public ActionResult processRequest(EventServiceImpl service, ActionRequest actionRequest, Bundle bundle) {
//Send the like action to your server here.
return new LikePostActionEvent();
}
}- Implement processRequest() as needed, EventServiceImpl gives you access to the Service/Context object.
- @EventClass will generate LikePostActionEvent, the resulting event class.
- @RequestAction will generate PsLikePostActionAction, the request identifier class.
- @RequestActionHelper will generate LikePostActionHelper, the request helper class.
PennStation.requestAction(PsLikePostAction.helper());This is all you need from the caller's perspective, notice how no hard reference is passed in.
@EventListener(producers = {
LikePostAction.class
})
public class SampleActivity extends Activity {
private final SampleActivityEventListener mListener = new SampleActivityEventListener() {
@Override
public void onEventMainThread(LikePostActionEvent event) {
//Task has completed.
}
};
@Override
protected void onResume() {
super.onResume();
PennStation.registerListener(mListener);
}
@Override
protected void onPause() {
super.onPause();
PennStation.unRegisterListener(mListener);
}For each listener that cares about the task result, add the action to @EventListener.
This will add the required event methods to the generated ClassEventListener class.
All the events emitted will be passed back on the main thread by default.