Skip to content

Commit 7522da4

Browse files
committed
1. Modiy RxBus annotation method logic, will throw exceptions if method invoke failed.
2. Refactor method. 3. Update demo. 4. Add javadoc.
1 parent dabc3b0 commit 7522da4

32 files changed

Lines changed: 352 additions & 1066 deletions

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ allprojects {
2828
}
2929
}
3030
```
31-
and then add rxbus2 dependency in your module gradle:
31+
and then add RxBus2 dependency in your module gradle:
3232

3333
```groovy
3434
   implementation group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.x.x'
3535
implementation('com.jakewharton.rxrelay2:rxrelay:2.0.0'){
3636
exclude group: 'io.reactivex.rxjava2',module: 'rxjava'
3737
}
38-
implementation "com.github.wind0ws:rxbus2:1.1.0"
38+
implementation "com.github.wind0ws:rxbus2:1.1.1"
3939
// maybe you need RxAndroid2 if you are using this on Android.
4040
// implementation('io.reactivex.rxjava2:rxandroid:2.x.x') {
4141
// exclude group: 'io.reactivex.rxjava2', module: 'rxjava'
4242
// }
43-
//remember replace "2.x.x" to the latest version.
43+
//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)
4444
```
4545
> seems complicated?
4646
> 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.
@@ -60,26 +60,26 @@ public class MyApplication extends Application {
6060
@Override
6161
public void onCreate() {
6262
super.onCreate();
63-
// if you using Annotation,and observeOnThread MAIN, you should config this.
63+
// if you using @RxSubscribe Annotation,and observeOn MAIN, you should config this.
6464
RxBus.setMainScheduler(AndroidSchedulers.mainThread());
6565
}
6666
}
6767
```
68-
> You can find AndroidSchedulers here [RxAndroid](https://github.com/ReactiveX/RxAndroid)
68+
> 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.)
6969
### Annotation usage(just for RxBus)
7070
* write listen event method
7171

7272
```java
7373
@RxSubscribe(observeOnThread = EventThread.MAIN)
7474
public void listenRxIntegerEvent(int code) {
75-
String text = String.format("{ Receive event: %s\nCurrent thread: %s }", code, Thread.currentThread());
75+
String text = String.format("{ Receive event: %s\nCurrent thread: %s }", code, Thread.currentThread().getId());
7676
Log.d("RxBus",text)
7777
}
7878
```
7979
```java
8080
@RxSubscribe(observeOnThread = EventThread.IO,isSticky = true)
8181
public void listenRxStringEvent(String event) {
82-
final String text = String.format("{ Receive event: %s\nCurrent thread: %s }", event, Thread.currentThread());
82+
final String text = String.format("{ Receive event: %s\nCurrent thread: %s }", event, Thread.currentThread().getId());
8383
Log.d("RxBus",text);
8484
}
8585
```

app/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
minSdkVersion 16
99
targetSdkVersion 26
1010
versionCode 1
11-
versionName "1.1.0"
11+
versionName "1.1.1"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {
@@ -18,6 +18,11 @@ android {
1818
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
1919
}
2020
}
21+
compileOptions {
22+
sourceCompatibility JavaVersion.VERSION_1_8
23+
targetCompatibility JavaVersion.VERSION_1_8
24+
encoding = "UTF-8"
25+
}
2126
}
2227

2328
dependencies {

app/proguard-rules.pro

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,18 @@
3636
-allowaccessmodification
3737
-renamesourcefileattribute SourceFile
3838
-keepattributes SourceFile,LineNumberTable
39+
-keep class * implements android.os.Parcelable {
40+
public static final android.os.Parcelable$Creator *;
41+
}
42+
43+
-keepnames class * implements java.io.Serializable
44+
3945
-keepclassmembers class * implements java.io.Serializable {
4046
static final long serialVersionUID;
4147
private static final java.io.ObjectStreamField[] serialPersistentFields;
48+
!static !transient <fields>;
49+
!private <fields>;
50+
!private <methods>;
4251
private void writeObject(java.io.ObjectOutputStream);
4352
private void readObject(java.io.ObjectInputStream);
4453
java.lang.Object writeReplace();
@@ -97,24 +106,6 @@
97106
public <init>(android.content.Context, android.util.AttributeSet, int);
98107
}
99108

100-
-keep class * implements android.os.Parcelable {
101-
public static final android.os.Parcelable$Creator *;
102-
}
103-
104-
-keepnames class * implements java.io.Serializable
105-
106-
-keepclassmembers class * implements java.io.Serializable {
107-
static final long serialVersionUID;
108-
private static final java.io.ObjectStreamField[] serialPersistentFields;
109-
!static !transient <fields>;
110-
!private <fields>;
111-
!private <methods>;
112-
private void writeObject(java.io.ObjectOutputStream);
113-
private void readObject(java.io.ObjectInputStream);
114-
java.lang.Object writeReplace();
115-
java.lang.Object readResolve();
116-
}
117-
118109
-keepclassmembers class * {
119110
public void *ButtonClicked(android.view.View);
120111
}

app/src/main/java/com/threshold/rxbus2demo/BehaviorBusActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.reactivex.functions.Consumer;
1717

1818
/**
19+
* Demo for showing {@link BehaviorBus} usage.
1920
* Created by threshold on 2017/1/18.
2021
*/
2122

app/src/main/java/com/threshold/rxbus2demo/MyApplication.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.orhanobut.logger.Logger;
88
import com.orhanobut.logger.PrettyFormatStrategy;
99
import com.threshold.rxbus2.RxBus;
10+
import com.threshold.rxbus2demo.util.RxLogger;
1011

1112
import io.reactivex.android.schedulers.AndroidSchedulers;
1213

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

3940

4041
//This option is optional. Using this only if you want to output RxBus log
41-
// RxBus.setLogger(new RxLogger());
42+
RxBus.setLogger(new RxLogger());
4243

4344
// RxBus.config(AndroidSchedulers.mainThread(),new RxLogger());//this method is removed in latest version.
4445
}

app/src/main/java/com/threshold/rxbus2demo/ReplayBusActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.reactivex.functions.Consumer;
1717

1818
/**
19+
* Demo for showing {@link ReplayBus} usage.
1920
* Created by threshold on 2017/1/19.
2021
*/
2122

app/src/main/java/com/threshold/rxbus2demo/RxBusActivity.java

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
import com.threshold.rxbus2.RxBus;
1111
import com.threshold.rxbus2.annotation.RxSubscribe;
1212
import com.threshold.rxbus2.util.EventThread;
13+
import com.threshold.rxbus2demo.bean.DemoBean1;
14+
import com.threshold.rxbus2demo.bean.DemoBean2;
15+
import com.threshold.rxbus2demo.bean.event.DemoEvent1;
16+
import com.threshold.rxbus2demo.bean.event.DemoEvent2;
17+
import com.threshold.rxbus2demo.bean.event.RxEvent;
1318
import com.threshold.rxbus2demo.util.RandomUtil;
1419

1520
import java.util.List;
@@ -20,6 +25,7 @@
2025
import io.reactivex.schedulers.Schedulers;
2126

2227
/**
28+
* Demo for showing {@link RxBus} usage.
2329
* Created by threshold on 2017/1/18.
2430
*/
2531

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

56-
@RxSubscribe(observeOnThread = EventThread.MAIN) @SuppressWarnings("unused")
57-
public void autoListenRxEvent(int code) {
58-
String text = String.format("{ Receive event: %s\nCurrent thread: %s }", code, Thread.currentThread());
61+
@RxSubscribe(observeOnThread = EventThread.MAIN)
62+
@SuppressWarnings("unused")
63+
public void autoListenRxEvent(DemoEvent1 demoEvent1) {
64+
String text = String.format("{autoListenRxEvent Receive DemoEvent1: %s\nThreadId: %s }\n", demoEvent1.getDemoBean1().getData(), Thread.currentThread().getId());
5965
Logger.d(text);
6066
textView.append(text);
6167
textView.append("\n");
6268
}
6369

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

77-
public void manualListenRxEvent(String id, String event) {
78-
final String text = String.format("{[%s Receive event]: %s}", id, event);
85+
// Will crash on register. Because no param in method.
86+
// @RxSubscribe(observeOnThread = EventThread.IO, isSticky = true)
87+
// @SuppressWarnings("unused")
88+
// private void autoListenRxEvent3() {
89+
//
90+
// }
91+
92+
// Will crash on register. Because two param in method. We expect ONLY ONE param.
93+
// @RxSubscribe(observeOnThread = EventThread.IO, isSticky = true)
94+
// @SuppressWarnings("unused")
95+
// private void autoListenRxEvent3(DemoEvent1 event1,DemoEvent2 event2) {
96+
//
97+
// }
98+
99+
// Will crash on receive event. Because you shouldn't update view state on BackgroundThread.
100+
//You should update UI(View) on MAIN THREAD(UI THREAD).
101+
// @RxSubscribe(observeOnThread = EventThread.IO, isSticky = true) @SuppressWarnings("unused")
102+
// private void autoListenRxEvent3(DemoEvent2 event) {
103+
// final String text = String.format("{autoListenRxEvent2 Receive sticky event: %s\nThreadId: %s }\n", event.getDemoBean2().getData(), Thread.currentThread().getId());
104+
// Logger.d(text);
105+
// textView.append(text);//will crash on here.
106+
// textView.append("\n");
107+
// }
108+
109+
@RxSubscribe(observeOnThread = EventThread.IO) @SuppressWarnings("unused")
110+
private void autoListenRxEvent3(RxEvent event) { //This method will listen DemoEvent1 and DemoEvent2 Both.
111+
final String text;
112+
if (event instanceof DemoEvent1) {
113+
text = String.format("{autoListenRxEvent3 Receive RxEvent: %s\nThreadId: %s }\n", ((DemoEvent1) event).getDemoBean1().getData(), Thread.currentThread().getId());
114+
} else if (event instanceof DemoEvent2) {
115+
text = String.format("{autoListenRxEvent3 Receive RxEvent: %s\nThreadId: %s }\n", ((DemoEvent2) event).getDemoBean2().getData(), Thread.currentThread().getId());
116+
} else {
117+
text = String.format("{autoListenRxEvent3 Receive RxEvent: %s\nThreadId: %s }\n", event, Thread.currentThread().getId());
118+
}
119+
Logger.d(text);
120+
}
121+
122+
private void manualListenRxEvent(DemoEvent1 event) {
123+
final String text = String.format("{manualListenRxEvent [Receive DemoEvent1]: %s}\n", event.getDemoBean1().getData());
79124
Logger.d(text);
80125
runOnUiThread(new Runnable() {
81126
@Override
@@ -90,31 +135,35 @@ public void run() {
90135
public void onClick(View view) {
91136
switch (view.getId()) {
92137
case R.id.btnFireEvent:
93-
RxBus.getDefault().post(RandomUtil.random(10));
94-
RxBus.getDefault().post("Hi, event "+RandomUtil.random(10));
138+
DemoEvent1 demoEvent1 = new DemoEvent1(RxBusActivity.class, new DemoBean1(String.valueOf(RandomUtil.random(10))));
139+
RxBus.getDefault().post(demoEvent1);
95140
break;
96141
case R.id.btnFireStickyEvent:
97-
RxBus.getDefault().postSticky("Hello, sticky event "+RandomUtil.random(100));
142+
DemoEvent2 demoEvent2 = new DemoEvent2(RxBusActivity.class, new DemoBean2(RandomUtil.random(10)));
143+
RxBus.getDefault().postSticky(demoEvent2);
98144
break;
99145
case R.id.btnAddNewSubscriber:
100146
Disposable subscribe = RxBus.getDefault()
101-
.ofStickyType(String.class)
147+
.ofStickyType(DemoEvent1.class)
102148
.subscribeOn(Schedulers.io())
103149
.observeOn(Schedulers.io())
104-
.subscribe(new Consumer<String>() {
150+
.subscribe(new Consumer<DemoEvent1>() {
105151
@Override
106-
public void accept(String s) throws Exception {
107-
manualListenRxEvent("Second Subscriber", s);
152+
public void accept(DemoEvent1 event1) throws Exception {
153+
manualListenRxEvent(event1);
108154
}
109155
});
110156
mCompositeDisposable.add(subscribe);
111157
view.setEnabled(false);
112158
break;
113159
case R.id.btnRemoveStickyEvent:
114-
List<String> sticky = RxBus.getDefault()
115-
.getSticky(String.class);
116-
if (sticky != null && sticky.size() > 0) {
117-
RxBus.getDefault().removeSticky(sticky.get(0));
160+
List<DemoEvent2> stickies = RxBus.getDefault()
161+
.getSticky(DemoEvent2.class);
162+
if (stickies != null && stickies.size() > 0) {
163+
RxBus.getDefault().removeStickyEventAt(DemoEvent2.class,stickies.size()-1);//remove the last sticky event
164+
textView.append("Already removed last sticky event, you can press back key and reenter this activity and see difference.\n");
165+
} else {
166+
textView.append("No sticky event found, please fire some sticky event first\n");
118167
}
119168
break;
120169
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.threshold.rxbus2demo.bean;
2+
3+
/**
4+
* DemoBean1
5+
* Created by threshold on 2018/1/24.
6+
*/
7+
8+
public class DemoBean1 {
9+
10+
public DemoBean1() {
11+
}
12+
13+
public DemoBean1(String data) {
14+
this.data = data;
15+
}
16+
17+
private String data;
18+
19+
public String getData() {
20+
return data;
21+
}
22+
23+
public void setData(String data) {
24+
this.data = data;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return "DemoBean1{" +
30+
"data='" + data + '\'' +
31+
'}';
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.threshold.rxbus2demo.bean;
2+
3+
/**
4+
* DemoBean2
5+
* Created by threshold on 2018/1/24.
6+
*/
7+
8+
public class DemoBean2 {
9+
10+
public DemoBean2() {
11+
}
12+
13+
public DemoBean2(Object data) {
14+
this.data = data;
15+
}
16+
17+
private Object data;
18+
19+
public Object getData() {
20+
return data;
21+
}
22+
23+
public void setData(Object data) {
24+
this.data = data;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return "DemoBean2{" +
30+
"data=" + data +
31+
'}';
32+
}
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.threshold.rxbus2demo.bean.event;
2+
3+
import android.support.annotation.NonNull;
4+
5+
import com.threshold.rxbus2demo.bean.DemoBean1;
6+
7+
/**
8+
* A Event that bring with a {@link DemoEvent1}
9+
* Created by threshold on 2018/1/24.
10+
*/
11+
12+
public class DemoEvent1 extends RxEvent {
13+
14+
private DemoBean1 mDemoBean1;
15+
16+
/**
17+
* Constructs a prototypical Event.
18+
*
19+
* @param source The object on which the Event initially occurred.
20+
* @throws IllegalArgumentException if source is null.
21+
*/
22+
public DemoEvent1(@NonNull Object source, @NonNull DemoBean1 demoBean1) {
23+
super(source);
24+
this.mDemoBean1 = demoBean1;
25+
}
26+
27+
@NonNull
28+
public DemoBean1 getDemoBean1() {
29+
return mDemoBean1;
30+
}
31+
}

0 commit comments

Comments
 (0)