Skip to content

Commit

Permalink
1. Modiy RxBus annotation method logic, will throw exceptions if meth…
Browse files Browse the repository at this point in the history
…od invoke failed.

2. Refactor method.
3. Update demo.
4. Add javadoc.
  • Loading branch information
wind0ws committed Jan 24, 2018
1 parent dabc3b0 commit 7522da4
Show file tree
Hide file tree
Showing 32 changed files with 352 additions and 1,066 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ allprojects {
}
}
```
and then add rxbus2 dependency in your module gradle:
and then add RxBus2 dependency in your module gradle:

```groovy
   implementation group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.x.x'
implementation('com.jakewharton.rxrelay2:rxrelay:2.0.0'){
exclude group: 'io.reactivex.rxjava2',module: 'rxjava'
}
implementation "com.github.wind0ws:rxbus2:1.1.0"
implementation "com.github.wind0ws:rxbus2:1.1.1"
// maybe you need RxAndroid2 if you are using this on Android.
// implementation('io.reactivex.rxjava2:rxandroid:2.x.x') {
// exclude group: 'io.reactivex.rxjava2', module: 'rxjava'
// }
//remember replace "2.x.x" to the latest version.
//remember replace "2.x.x" to the latest version. You can find latest version of RxBus2 on [release page](https://github.com/wind0ws/rxbus2/releases)
```
> seems complicated?
> Not at all. I just want to make your project using latest version of library and just one version. If you confused about it, just check the [gradle file](https://github.com/wind0ws/rxbus2/blob/master/app/build.gradle) on this repo.
Expand All @@ -60,26 +60,26 @@ public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// if you using Annotation,and observeOnThread MAIN, you should config this.
// if you using @RxSubscribe Annotation,and observeOn MAIN, you should config this.
RxBus.setMainScheduler(AndroidSchedulers.mainThread());
}
}
```
> You can find AndroidSchedulers here [RxAndroid](https://github.com/ReactiveX/RxAndroid)
> You can find AndroidSchedulers here [RxAndroid](https://github.com/ReactiveX/RxAndroid).(This operation is optional, do this only if you want to use @RxSubscribe Annotation and observeOn MAIN THREAD.)
### Annotation usage(just for RxBus)
* write listen event method

```java
@RxSubscribe(observeOnThread = EventThread.MAIN)
public void listenRxIntegerEvent(int code) {
String text = String.format("{ Receive event: %s\nCurrent thread: %s }", code, Thread.currentThread());
String text = String.format("{ Receive event: %s\nCurrent thread: %s }", code, Thread.currentThread().getId());
Log.d("RxBus",text)
}
```
```java
@RxSubscribe(observeOnThread = EventThread.IO,isSticky = true)
public void listenRxStringEvent(String event) {
final String text = String.format("{ Receive event: %s\nCurrent thread: %s }", event, Thread.currentThread());
final String text = String.format("{ Receive event: %s\nCurrent thread: %s }", event, Thread.currentThread().getId());
Log.d("RxBus",text);
}
```
Expand Down
7 changes: 6 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.1.0"
versionName "1.1.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -18,6 +18,11 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
encoding = "UTF-8"
}
}

dependencies {
Expand Down
27 changes: 9 additions & 18 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,18 @@
-allowaccessmodification
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}

-keepnames class * implements java.io.Serializable

-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
!static !transient <fields>;
!private <fields>;
!private <methods>;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
Expand Down Expand Up @@ -97,24 +106,6 @@
public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}

-keepnames class * implements java.io.Serializable

-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
!static !transient <fields>;
!private <fields>;
!private <methods>;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}

-keepclassmembers class * {
public void *ButtonClicked(android.view.View);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.reactivex.functions.Consumer;

/**
* Demo for showing {@link BehaviorBus} usage.
* Created by threshold on 2017/1/18.
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.threshold.rxbus2.RxBus;
import com.threshold.rxbus2demo.util.RxLogger;

import io.reactivex.android.schedulers.AndroidSchedulers;

Expand Down Expand Up @@ -38,7 +39,7 @@ public boolean isLoggable(int priority, String tag) {


//This option is optional. Using this only if you want to output RxBus log
// RxBus.setLogger(new RxLogger());
RxBus.setLogger(new RxLogger());

// RxBus.config(AndroidSchedulers.mainThread(),new RxLogger());//this method is removed in latest version.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.reactivex.functions.Consumer;

/**
* Demo for showing {@link ReplayBus} usage.
* Created by threshold on 2017/1/19.
*/

Expand Down
89 changes: 69 additions & 20 deletions app/src/main/java/com/threshold/rxbus2demo/RxBusActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
import com.threshold.rxbus2.RxBus;
import com.threshold.rxbus2.annotation.RxSubscribe;
import com.threshold.rxbus2.util.EventThread;
import com.threshold.rxbus2demo.bean.DemoBean1;
import com.threshold.rxbus2demo.bean.DemoBean2;
import com.threshold.rxbus2demo.bean.event.DemoEvent1;
import com.threshold.rxbus2demo.bean.event.DemoEvent2;
import com.threshold.rxbus2demo.bean.event.RxEvent;
import com.threshold.rxbus2demo.util.RandomUtil;

import java.util.List;
Expand All @@ -20,6 +25,7 @@
import io.reactivex.schedulers.Schedulers;

/**
* Demo for showing {@link RxBus} usage.
* Created by threshold on 2017/1/18.
*/

Expand Down Expand Up @@ -50,20 +56,22 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
// }
// });
// mCompositeDisposable.add(subscribe);
Logger.d("Is register Success:%s", RxBus.getDefault().hasObservers());
}

@RxSubscribe(observeOnThread = EventThread.MAIN) @SuppressWarnings("unused")
public void autoListenRxEvent(int code) {
String text = String.format("{ Receive event: %s\nCurrent thread: %s }", code, Thread.currentThread());
@RxSubscribe(observeOnThread = EventThread.MAIN)
@SuppressWarnings("unused")
public void autoListenRxEvent(DemoEvent1 demoEvent1) {
String text = String.format("{autoListenRxEvent Receive DemoEvent1: %s\nThreadId: %s }\n", demoEvent1.getDemoBean1().getData(), Thread.currentThread().getId());
Logger.d(text);
textView.append(text);
textView.append("\n");
}

@RxSubscribe(observeOnThread = EventThread.IO,isSticky = true) @SuppressWarnings("unused")
public void autoListenRxEvent2(String event) {
final String text = String.format("{ Receive event: %s\nCurrent thread: %s }", event, Thread.currentThread());
//now we support private method.
@RxSubscribe(observeOnThread = EventThread.IO, isSticky = true)
@SuppressWarnings("unused")
private void autoListenRxEvent2(DemoEvent2 event) {
final String text = String.format("{autoListenRxEvent2 Receive sticky DemoEvent2: %s\nThreadId: %s }\n", event.getDemoBean2().getData(), Thread.currentThread().getId());
Logger.d(text);
runOnUiThread(new Runnable() {
@Override
Expand All @@ -74,8 +82,45 @@ public void run() {
});
}

public void manualListenRxEvent(String id, String event) {
final String text = String.format("{[%s Receive event]: %s}", id, event);
// Will crash on register. Because no param in method.
// @RxSubscribe(observeOnThread = EventThread.IO, isSticky = true)
// @SuppressWarnings("unused")
// private void autoListenRxEvent3() {
//
// }

// Will crash on register. Because two param in method. We expect ONLY ONE param.
// @RxSubscribe(observeOnThread = EventThread.IO, isSticky = true)
// @SuppressWarnings("unused")
// private void autoListenRxEvent3(DemoEvent1 event1,DemoEvent2 event2) {
//
// }

// Will crash on receive event. Because you shouldn't update view state on BackgroundThread.
//You should update UI(View) on MAIN THREAD(UI THREAD).
// @RxSubscribe(observeOnThread = EventThread.IO, isSticky = true) @SuppressWarnings("unused")
// private void autoListenRxEvent3(DemoEvent2 event) {
// final String text = String.format("{autoListenRxEvent2 Receive sticky event: %s\nThreadId: %s }\n", event.getDemoBean2().getData(), Thread.currentThread().getId());
// Logger.d(text);
// textView.append(text);//will crash on here.
// textView.append("\n");
// }

@RxSubscribe(observeOnThread = EventThread.IO) @SuppressWarnings("unused")
private void autoListenRxEvent3(RxEvent event) { //This method will listen DemoEvent1 and DemoEvent2 Both.
final String text;
if (event instanceof DemoEvent1) {
text = String.format("{autoListenRxEvent3 Receive RxEvent: %s\nThreadId: %s }\n", ((DemoEvent1) event).getDemoBean1().getData(), Thread.currentThread().getId());
} else if (event instanceof DemoEvent2) {
text = String.format("{autoListenRxEvent3 Receive RxEvent: %s\nThreadId: %s }\n", ((DemoEvent2) event).getDemoBean2().getData(), Thread.currentThread().getId());
} else {
text = String.format("{autoListenRxEvent3 Receive RxEvent: %s\nThreadId: %s }\n", event, Thread.currentThread().getId());
}
Logger.d(text);
}

private void manualListenRxEvent(DemoEvent1 event) {
final String text = String.format("{manualListenRxEvent [Receive DemoEvent1]: %s}\n", event.getDemoBean1().getData());
Logger.d(text);
runOnUiThread(new Runnable() {
@Override
Expand All @@ -90,31 +135,35 @@ public void run() {
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnFireEvent:
RxBus.getDefault().post(RandomUtil.random(10));
RxBus.getDefault().post("Hi, event "+RandomUtil.random(10));
DemoEvent1 demoEvent1 = new DemoEvent1(RxBusActivity.class, new DemoBean1(String.valueOf(RandomUtil.random(10))));
RxBus.getDefault().post(demoEvent1);
break;
case R.id.btnFireStickyEvent:
RxBus.getDefault().postSticky("Hello, sticky event "+RandomUtil.random(100));
DemoEvent2 demoEvent2 = new DemoEvent2(RxBusActivity.class, new DemoBean2(RandomUtil.random(10)));
RxBus.getDefault().postSticky(demoEvent2);
break;
case R.id.btnAddNewSubscriber:
Disposable subscribe = RxBus.getDefault()
.ofStickyType(String.class)
.ofStickyType(DemoEvent1.class)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(new Consumer<String>() {
.subscribe(new Consumer<DemoEvent1>() {
@Override
public void accept(String s) throws Exception {
manualListenRxEvent("Second Subscriber", s);
public void accept(DemoEvent1 event1) throws Exception {
manualListenRxEvent(event1);
}
});
mCompositeDisposable.add(subscribe);
view.setEnabled(false);
break;
case R.id.btnRemoveStickyEvent:
List<String> sticky = RxBus.getDefault()
.getSticky(String.class);
if (sticky != null && sticky.size() > 0) {
RxBus.getDefault().removeSticky(sticky.get(0));
List<DemoEvent2> stickies = RxBus.getDefault()
.getSticky(DemoEvent2.class);
if (stickies != null && stickies.size() > 0) {
RxBus.getDefault().removeStickyEventAt(DemoEvent2.class,stickies.size()-1);//remove the last sticky event
textView.append("Already removed last sticky event, you can press back key and reenter this activity and see difference.\n");
} else {
textView.append("No sticky event found, please fire some sticky event first\n");
}
break;
}
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/com/threshold/rxbus2demo/bean/DemoBean1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.threshold.rxbus2demo.bean;

/**
* DemoBean1
* Created by threshold on 2018/1/24.
*/

public class DemoBean1 {

public DemoBean1() {
}

public DemoBean1(String data) {
this.data = data;
}

private String data;

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}

@Override
public String toString() {
return "DemoBean1{" +
"data='" + data + '\'' +
'}';
}
}
33 changes: 33 additions & 0 deletions app/src/main/java/com/threshold/rxbus2demo/bean/DemoBean2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.threshold.rxbus2demo.bean;

/**
* DemoBean2
* Created by threshold on 2018/1/24.
*/

public class DemoBean2 {

public DemoBean2() {
}

public DemoBean2(Object data) {
this.data = data;
}

private Object data;

public Object getData() {
return data;
}

public void setData(Object data) {
this.data = data;
}

@Override
public String toString() {
return "DemoBean2{" +
"data=" + data +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.threshold.rxbus2demo.bean.event;

import android.support.annotation.NonNull;

import com.threshold.rxbus2demo.bean.DemoBean1;

/**
* A Event that bring with a {@link DemoEvent1}
* Created by threshold on 2018/1/24.
*/

public class DemoEvent1 extends RxEvent {

private DemoBean1 mDemoBean1;

/**
* Constructs a prototypical Event.
*
* @param source The object on which the Event initially occurred.
* @throws IllegalArgumentException if source is null.
*/
public DemoEvent1(@NonNull Object source, @NonNull DemoBean1 demoBean1) {
super(source);
this.mDemoBean1 = demoBean1;
}

@NonNull
public DemoBean1 getDemoBean1() {
return mDemoBean1;
}
}
Loading

0 comments on commit 7522da4

Please sign in to comment.