Skip to content

Commit

Permalink
1. Update gradle version and rxjava version.
Browse files Browse the repository at this point in the history
2. Refactor some method name.
3. Remove some unnecessary doOnNext method.
  • Loading branch information
wind0ws committed Aug 10, 2017
1 parent 14aab6c commit 83ab02b
Show file tree
Hide file tree
Showing 17 changed files with 343 additions and 112 deletions.
60 changes: 44 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
# RxBus2
>This is like EventBus but use RxJava inside.
[![](https://jitpack.io/v/wind0ws/rxbus2.svg)](https://jitpack.io/#wind0ws/rxbus2)

>中文说明,请点[这里](http://www.jianshu.com/p/7f4a709d2be5)查看.
This is seems like [EventBus](https://github.com/greenrobot/EventBus) which intent for post and listen event but use RxJava2 inside.

# Features

* Support annotation(RxSubscribe):auto register and unregister event listen.
* Support annotation(RxSubscribe):auto register and unregister event.
* Support sticky event(Just like sticky broadcast).
* Support 3 type Bus:
* Support 3 type Bus:
* RxBus(Publish Bus)
* BehaviorBus
* ReplayBus

## Getting started
## [Getting started](https://jitpack.io/#wind0ws/rxbus2)
The first step is to include RxBus 2 into your project, for example, as a Gradle compile dependency:

Because of using [jitpack.io](https://jitpack.io/),so we need add the jitpack.io repository in your root project gradle:
Expand All @@ -19,14 +23,20 @@ Because of using [jitpack.io](https://jitpack.io/),so we need add the jitpack.io
allprojects {
repositories {
jcenter()
//...some other repo.
maven { url "https://jitpack.io" }
}
}
```
and then add rxbus2 dependency in your module gradle:

```groovy
compile "com.github.wind0ws:rxbus2:1.0.0"
implementation "com.github.wind0ws:rxbus2:1.0.1"
```
> for gradle version below 3.0, add dependency like this:
>
```groovy
compile "com.github.wind0ws:rxbus2:1.0.1"
```

We are done for integration.
Expand All @@ -50,7 +60,7 @@ public class MyApplication extends Application {
* write listen event method

```java
@RxSubscribe(observeOnThread = EventThread.MAIN)
@RxSubscribe(observeOnThread = EventThread.MAIN)
public void listenRxIntegerEvent(int code) {
String text = String.format("{ Receive event: %s\nCurrent thread: %s }", code, Thread.currentThread());
Log.d("RxBus",text)
Expand All @@ -64,21 +74,21 @@ public class MyApplication extends Application {
}
```

* register and unregister
* register and unregister listen method

```java
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rx_bus);
RxBus.getInstance().register(this);
RxBus.getDefault().register(this);
}

@Override
protected void onDestroy() {
super.onDestroy();
//auto release register with Annotation RxSubscribe.
RxBus.getInstance().unregister(this);
RxBus.getDefault().unregister(this);
}
```

Expand All @@ -88,8 +98,8 @@ public class MyApplication extends Application {
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnFireEvent:
RxBus.getInstance().post(100);
RxBus.getInstance().post("Hi,Fire string event");
RxBus.getDefault().post(100);
RxBus.getDefault().post("Hi,Fire string event");
break;
}
}
Expand All @@ -99,8 +109,8 @@ public void onClick(View view) {

for example,ReplayBus.

```
ReplayBus.getInstance()
```java
ReplayBus.getDefault()
.ofType(String.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<String>() {
Expand All @@ -110,7 +120,25 @@ ReplayBus.getInstance()
Log.d("ReplayBus”,text);
}
});
ReplayBus.getInstance().post("ReplayBus"+ RandomUtil.random(100));
ReplayBus.getDefault().post("ReplayBus"+ RandomUtil.random(100));
```
### Proguard
add this line into your "proguard-rules.pro" file.
>If you use annotation, you need it. Don't forget keep your bean or entity in proguard.
```groovy
# For using annotation
-keepattributes *Annotation*
-keepattributes RuntimeVisibleAnnotations
-keepattributes RuntimeInvisibleAnnotations
-keepattributes RuntimeVisibleParameterAnnotations
-keepattributes RuntimeInvisibleParameterAnnotations
-keepclassmembers class ** {
@com.threshold.rxbus2.annotation.RxSubscribe <methods>;
}
-keep enum com.threshold.rxbus2.util.EventThread { *; }
```
## FAQ
Expand All @@ -120,4 +148,4 @@ ReplayBus.getInstance()
* Can you demonstrate detailed?
Please see app module in this repo,It show 3 type of bus usage.
Please see app module in this repo,It show 3 type bus usage.
32 changes: 21 additions & 11 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.threshold.rxbus2demo"
minSdkVersion 16
targetSdkVersion 25
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:26.0.1'
testImplementation 'junit:junit:4.12'

compile project(':rxbus')
implementation project(':rxbus')

// 打Log
// https://mvnrepository.com/artifact/com.orhanobut/logger
compile 'com.orhanobut:logger:1.15'
implementation 'com.orhanobut:logger:2.1.1'

// https://mvnrepository.com/artifact/io.reactivex.rxjava2/rxjava
implementation group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.1.2'

// https://mvnrepository.com/artifact/io.reactivex.rxjava2/rxandroid
compile('io.reactivex.rxjava2:rxandroid:2.0.1') {
implementation('io.reactivex.rxjava2:rxandroid:2.0.1') {
exclude group: 'io.reactivex.rxjava2', module: 'rxjava'
}

// https://mvnrepository.com/artifact/com.jakewharton.rxrelay2/rxrelay
implementation('com.jakewharton.rxrelay2:rxrelay:2.0.0'){
exclude group: 'io.reactivex.rxjava2',module: 'rxjava'
}
}
154 changes: 154 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,157 @@
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
-dontwarn java.lang.invoke.*
-dontwarn **$$Lambda$*

-dontwarn kotlin.**

-keepclassmembers class * {
public <init> (org.json.JSONObject);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

#beans
-keep class **.*Bean {*;}

#指定压缩级别
-optimizationpasses 5

#不跳过非公共的库的类成员
-dontskipnonpubliclibraryclassmembers

#混淆时采用的算法
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

#把混淆类中的方法名也混淆了
-useuniqueclassmembernames

#优化时允许访问并修改有修饰符的类和类的成员
-allowaccessmodification

#将文件来源重命名为“SourceFile”字符串
-renamesourcefileattribute SourceFile
#保留行号
-keepattributes SourceFile,LineNumberTable

#保持所有实现 Serializable 接口的类成员
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}

-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
#Fragment不需要在AndroidManifest.xml中注册,需要额外保护下
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.app.Fragment
#如果引用了v4或者v7包
-dontwarn android.support.**
# support-v7-appcompat
-keep public class * extends android.support.v7.app.AppCompatActivity
-keep public class * extends android.support.v7.widget.** { *; }
-keep public class * extends android.support.v7.internal.widget.** { *; }
-keep public class * extends android.support.v7.internal.view.menu.** { *; }
-keep public class * extends android.support.v4.view.ActionProvider {
public <init>(android.content.Context);
}
# support-design
-dontwarn android.support.design.**
-keep class android.support.design.** { *; }
-keep interface android.support.design.** { *; }
-keep public class android.support.design.R$* { *; }
# bottom navigation
-keep public class android.support.design.widget.BottomNavigationView { *; }
-keep public class android.support.design.internal.BottomNavigationMenuView { *; }
-keep public class android.support.design.internal.BottomNavigationPresenter { *; }
-keep public class android.support.design.internal.BottomNavigationItemView { *; }

# 保持测试相关的代码
-dontnote junit.framework.**
-dontnote junit.runner.**
-dontwarn android.test.**
-dontwarn android.support.test.**
-dontwarn org.junit.**

-keep public class * extends android.view.View {
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
public void set*(...);
}

#保持 native 方法不被混淆
-keepclasseswithmembernames class * {
native <methods>;
}

#保持自定义控件类不被混淆
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}

#保持自定义控件类不被混淆
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
#保持自定义控件类不被混淆
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}

#保持 Parcelable 不被混淆
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}

#保持 Serializable 不被混淆
-keepnames class * implements java.io.Serializable

#保持 Serializable 不被混淆并且enum 类也不被混淆
-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();
}

#保持枚举 enum 类不被混淆 如果混淆报错,建议直接使用上面的 -keepclassmembers class * implements java.io.Serializable即可
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

-keepclassmembers class * {
public void *ButtonClicked(android.view.View);
}

#保护R文件
-keep class **.R$* { *;}

# For using annotation and rxbus2
-keepattributes *Annotation*
-keepattributes RuntimeVisibleAnnotations
-keepattributes RuntimeInvisibleAnnotations
-keepattributes RuntimeVisibleParameterAnnotations
-keepattributes RuntimeInvisibleParameterAnnotations

-keepclassmembers class ** {
@com.threshold.rxbus2.annotation.RxSubscribe <methods>;
}
-keep enum com.threshold.rxbus2.util.EventThread { *; }
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("BehaviorBus");
setContentView(R.layout.activity_behavior_bus);
textView = (TextView) findViewById(R.id.text);
textView = findViewById(R.id.text);
behaviorBus = new BehaviorBus("Default Item");
compositeDisposable = new CompositeDisposable();
}
Expand Down
Loading

0 comments on commit 83ab02b

Please sign in to comment.