Skip to content

Add GreenRobot Event Bus #4

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ out/
# Gradle files
.gradle/
build/
/*/build/

# Local configuration file (sdk path, etc)
local.properties
Expand All @@ -34,7 +35,7 @@ captures/

# Intellij
*.iml
.idea/workspace.xml
.idea/

# Keystore files
*.jks
18 changes: 13 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ android {
versionCode 1
versionName "1.0"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}

lintOptions {
abortOnError false
}
Expand All @@ -33,14 +40,15 @@ dependencies {

// WITH THESE ONES YOU CAN TEST REMOTE LIBS
// REMEMBER TO USE THESE AFTER TEST CAUSE JENKINS TROUBLES
compile "com.baseandroid:baseandroid-busadapter:$adapterVersionName"
compile "com.baseandroid:baseandroid-eventdispatcher:$adapterVersionName"
// compile "com.baseandroid:baseandroid-busadapter:$adapterVersionName"
// compile "com.baseandroid:baseandroid-eventdispatcher:$adapterVersionName"
// compile "com.baseandroid:baseandroid-rxeventdispatcher:$adapterVersionName'

// WITH THESE ONES YOU CAN USE LOCAL PROJECTS
// REMEMBER: COMMENT THESE ONES BEFOR PUSH ON DEVELOP CAUSE JENKINS TROUBLES
//compile project(path: ':baseandroid-busadapter')
//compile project(path: ':baseandroid-eventdispatcher')
//compile project(path: ':baseandroid-rxeventdispatcher')
compile project(path: ':baseandroid-busadapter')
compile project(path: ':baseandroid-eventdispatcher')
compile project(path: ':baseandroid-rxeventdispatcher')
compile project(path: ':baseandroid-greenbus')

}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="it.sysdata.eventdispatcher" >

<application
android:name=".UniversalEventBusApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
EventDispatcher.register(this);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,58 @@

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.baseandroid.events.EventDispatcher;
import com.squareup.otto.Subscribe;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {
private final static String LOG_TAG = MainActivityFragment.class.getSimpleName();

public MainActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
EventDispatcher.register(this);
return inflater.inflate(R.layout.fragment_main, container, false);
}

@Subscribe
public void onConsumePippo(final UIEvent UIEvent) {
Toast.makeText(getActivity(), "received UIEvent", Toast.LENGTH_SHORT).show();
/**
* This method will be called when a UIEvent is posted on Otto Event Bus
* @param uiEvent
*/
//@Subscribe
//public void onConsumeUIEvent(UIEvent uiEvent) {
// Toast.makeText(getActivity(), "Otto Subscribe: received UIEvent", Toast.LENGTH_SHORT).show();
//}

/**
* This method will be called when a UIEvent is posted on Rx Event Bus
* @param uiEvent
*/
//@RxSubscribe
//public void onConsumeUIEvent(UIEvent uiEvent) {
// Toast.makeText(getActivity(), "RxSubscribe: received UIEvent", Toast.LENGTH_SHORT).show();
//}

/**
* This method will be called when a UIEvent is posted on GreenRobot Event Bus
* @param uiEvent
*/
@Subscribe(threadMode = ThreadMode.MAIN_ORDERED, priority = 2)
public void onConsumeUIEvent(UIEvent uiEvent) {
Log.d(LOG_TAG, "GreenRobot Subscribe: received UIEvent");
Toast.makeText(getActivity(), "GreenRobot Subscribe: received UIEvent", Toast.LENGTH_SHORT).show();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package it.sysdata.eventdispatcher;

import android.app.Application;

import com.baseandroid.eventbus.GreenRobotEventProcessor;
import com.baseandroid.events.EventDispatcher;
import com.baseandroid.events.EventProcessor;
import com.baseandroid.events.otto.OttoEventProcessor;
import com.baseandroid.events.rx.RxEventProcessor;

/**
* Created by Conversano Luca on 20/11/17.
*/

public class UniversalEventBusApplication extends Application {
private final static String LOG_TAG = UniversalEventBusApplication.class.getSimpleName();

/**
* Defines the type of the event bus.<br>
*/
public enum EventBusType {
/**
* Otto Event Bus
*/
OTTO,
/**
* Rx Event Bus
*/
RX,
/**
* GreenRobot Event Bus
*/
GREENROBOT
}

@Override
public void onCreate() {
super.onCreate();

// init event bus
initEventBus();
}

private void initEventBus() {

//EventBusType eventBusType = EventBusType.OTTO;
//EventBusType eventBusType = EventBusType.RX;
EventBusType eventBusType = EventBusType.GREENROBOT;

switch (eventBusType) {
case OTTO:
initOttoEventBus();
break;
case RX:
initRxEventBus();
break;
case GREENROBOT:
initGreenRobotEventBus();
break;
}

}

private void initRxEventBus() {
RxEventProcessor eventProcessor = (RxEventProcessor) RxEventProcessor.newInstance();
eventProcessor.setVerbose(true);
EventDispatcher.useEventProcessor(eventProcessor);
}

private void initOttoEventBus() {
EventProcessor eventProcessor = (EventProcessor) OttoEventProcessor.newInstance();
EventDispatcher.useEventProcessor(eventProcessor);
}

private void initGreenRobotEventBus() {
GreenRobotEventProcessor eventProcessor = (GreenRobotEventProcessor) GreenRobotEventProcessor.newInstance();
eventProcessor.setVerbose(true);
EventDispatcher.useEventProcessor(eventProcessor);
}

}
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
-->

<resources>
<string name="app_name">EventDispatcher</string>
<string name="app_name">UniversalEventBus</string>
<string name="action_settings">Settings</string>
</resources>
6 changes: 4 additions & 2 deletions baseandroid-eventdispatcher/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

compileOptions {
targetCompatibility 1.7
sourceCompatibility 1.7
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

lintOptions {
abortOnError false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,29 @@ private static void startEventsConsumption() {
* This Observable (Observable.interval) cycles every EVENT_CONSUMPTION_INTERVAL millis
* we observe results on a new Thread and remove items from non-UI queues
*/
rx.Observable.interval(EVENT_CONSUMPTION_INTERVAL, TimeUnit.MILLISECONDS).onBackpressureBuffer().subscribeOn(Schedulers.newThread()).observeOn(Schedulers.newThread()).subscribe(aLong1 -> {
if (!mNetworkEvents.isEmpty()) {
Object ev = mNetworkEvents.remove(0);
logEvent(ev, false);
BUS.post(ev);
} else if (!mDataEvents.isEmpty()) {
Object ev = mDataEvents.remove(0);
logEvent(ev, false);
BUS.post(ev);
} else if (!mGenericEvents.isEmpty()) {
Object ev = mGenericEvents.remove(0);
logEvent(ev, false);
BUS.post(ev);
} else if (!mContextEvents.isEmpty()) {
Object ev = mContextEvents.remove(0);
logEvent(ev, false);
BUS.post(ev);
}
});
rx.Observable.interval(EVENT_CONSUMPTION_INTERVAL, TimeUnit.MILLISECONDS)
.onBackpressureBuffer()
.subscribeOn(Schedulers.newThread())
.observeOn(Schedulers.newThread())
.subscribe(aLong1 -> {
if (!mNetworkEvents.isEmpty()) {
Object ev = mNetworkEvents.remove(0);
logEvent(ev, false);
BUS.post(ev);
} else if (!mDataEvents.isEmpty()) {
Object ev = mDataEvents.remove(0);
logEvent(ev, false);
BUS.post(ev);
} else if (!mGenericEvents.isEmpty()) {
Object ev = mGenericEvents.remove(0);
logEvent(ev, false);
BUS.post(ev);
} else if (!mContextEvents.isEmpty()) {
Object ev = mContextEvents.remove(0);
logEvent(ev, false);
BUS.post(ev);
}
});

/*
* This Observable (Observable.interval) cycles every EVENT_CONSUMPTION_INTERVAL millis
Expand Down Expand Up @@ -232,12 +236,13 @@ public void onPost(Object o) {
//init dead events manager
if(mDeadEventManager == null) {
mDeadEventManager = new DeadEventManager();
}else{
} else {
BUS.unregister(mDeadEventManager);
UI_BUS.unregister(mDeadEventManager);
}
BUS.register(mDeadEventManager);
UI_BUS.register(mDeadEventManager);

//mark us as initialised
mInitialised = true;
startEventsConsumption();
Expand Down Expand Up @@ -270,6 +275,9 @@ public void onPost(Object o) {
Collections.sort(mContextEvents, Event.COMPARATOR);
break;
}
} else if (o != null) {
DeadEvent deadEvent = new DeadEvent(BUS, o);
BUS.post(deadEvent);
}
}

Expand Down
1 change: 1 addition & 0 deletions baseandroid-greenbus/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
57 changes: 57 additions & 0 deletions baseandroid-greenbus/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
apply plugin: 'com.android.library'

def libName = 'baseandroid-greenbus'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
minSdkVersion 9
targetSdkVersion 23
versionCode libVersionCode
versionName libVersionName
}

packagingOptions {
exclude 'LICENSE.txt'
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}

lintOptions {
abortOnError false
}

}

dependencies {
def adapterVersionName = project.ext.libVersionName

compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
// log
compile 'org.slf4j:slf4j-api:1.7.12'
compile 'com.github.tony19:logback-android-core:1.1.1-4'
compile 'com.github.tony19:logback-android-classic:1.1.1-4'
// GreenRobot Event Bus
compile 'org.greenrobot:eventbus:3.1.1'
// BusAdapter
compile "com.baseandroid:baseandroid-busadapter:$adapterVersionName"
// rxJava
compile 'io.reactivex:rxandroid:1.1.0'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
compile 'io.reactivex:rxjava:1.1.3'
}
Loading