Skip to content

Update README.md #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 16, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 90 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# EventDispatcher
Event dispatcher - An event bus by Sysdata
EventDispatcher - An event bus by Sysdata
=============================

The EventDispatcher events is a bus designed to separate different parts of the application, while still allowing them to communicate efficiently.
Expand All @@ -10,6 +10,95 @@ The events that are posted by the Event dispatcher are heard by all those who si
Usage
--------

1. You've got to initialize the EventDispatcher. We suggest to do it in the MainApplication's onCreate().

```java
EventDispatcher.useEventProcessor(RxEventProcessor.newInstance());
```

2. Register the EventDispatcher when the Activity/Fragment/Service is created and unregister it when it is destroyed.

```java

@Override
public void onCreate() {
super.onCreate();
// register the event dispatcher in order to receive
// events that are posted on the Bus.
EventDispatcher.register(this);
}

@Override
public void onDestroy() {
super.onDestroy();
// unregister the event dispatcher
EventDispatcher.unregister(this);
}
```
3. Create events, post and receive them: Once the EventDispatcher is initialized and register in your Android component, you can post and receive events easily. Use `EventDispatcher.post()` to post the event you want to stream in the bus. Create a public method that has the same event type in its signature and annotate it with the `@RxSubscribe` signature in order to receive the Object that has been posted.

```java
@Override
public void onCreate() {
super.onCreate();
// post an example event
EventDispatcher.post(new ExampleEvent());
}

@RxSubscribe
public void onConsumeExampleEvent(ExampleEvent event) {
// do what you want with the incoming event
}

/**
* This is an example empty event: remember to add the "Event" annotation!!
*/
@Event(type = Event.Type.UI)
public class ExampleEvent {

public ExampleEvent() {
// empty constructor
}

}
```

The posting class can be different from the receiving one: both must be registered to the EventDispatcher, though! Remember that each Class that you want to use as an event MUST have the `@Event` annotation. You can choose between 5 type of events based on which is the use of the designed event: GENERIC, DATA, NETWORK, CONTEXT and UI. The difference is that UI events will be posted on the UI thread, meanwhile the others will be posted in a separated Thread.

Handle configuration changes
--------

EventDispatcher allows you to handle configuration changes easily. By using `EventDispatcher.loadPoint()` and `EventDispatcher.savePoint()` you will be able to receive posted events even after a configuration change (i.e. rotation or other lifecycle events).

```java

private String mEventDispatcherTag;

@Override
public void onCreate() {
super.onCreate();
// retrieve the saved state, if present
if (savedInstanceState != null && savedInstanceState.containsKey("ett")) {
this.mEventDispatcherTag = savedInstanceState.getString("ett");
}
// loadPoint() is used to handle events' saved state between configuration changes
EventDispatcher.loadPoint(this, this.mEventDispatcherTag);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save the EventDispatcher's tag
outState.putString("ett", mEventDispatcherTag);
}

@Override
public void onDestroy() {
super.onDestroy();
// save point is used to save the state in order to restore it later, after the configuration change.
this.mEventDispatcherTag = EventDispatcher.savePoint(this);
}
```

Download
--------
Expand Down