From 6f670794733a4c426f421c50413a87655e33a287 Mon Sep 17 00:00:00 2001 From: brooklet Date: Mon, 14 Aug 2017 15:29:46 +0800 Subject: [PATCH] update RxJava1.0 to RxJava2.0 --- .../sampletaishan/MainActivity.java | 34 +++++++++---------- rxtaishan/build.gradle | 7 ++-- .../com/lengyue524/rxtaishan/RxTaiShan.java | 25 ++++++++------ settings.gradle | 2 +- taishan/build.gradle | 3 +- 5 files changed, 38 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/lengyue524/sampletaishan/MainActivity.java b/app/src/main/java/com/lengyue524/sampletaishan/MainActivity.java index ff15720..54f6bc7 100644 --- a/app/src/main/java/com/lengyue524/sampletaishan/MainActivity.java +++ b/app/src/main/java/com/lengyue524/sampletaishan/MainActivity.java @@ -22,12 +22,12 @@ import java.io.File; import java.util.ArrayList; +import io.reactivex.functions.Function; import me.iwf.photopicker.PhotoPicker; -import rx.Observable; -import rx.android.schedulers.AndroidSchedulers; -import rx.functions.Action1; -import rx.functions.Func1; -import rx.schedulers.Schedulers; +import io.reactivex.Observable; +import io.reactivex.schedulers.Schedulers; +import io.reactivex.functions.Consumer; +import io.reactivex.android.schedulers.AndroidSchedulers; public class MainActivity extends AppCompatActivity { @@ -96,21 +96,21 @@ private void compressWithRx(File file) { .asObservable() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .doOnError(new Action1() { + .doOnError(new Consumer() { @Override - public void call(Throwable throwable) { + public void accept(Throwable throwable) { throwable.printStackTrace(); } }) - .onErrorResumeNext(new Func1>() { + .onErrorResumeNext(new Function>() { @Override - public Observable call(Throwable throwable) { + public Observable apply(Throwable throwable) { return Observable.empty(); } }) - .subscribe(new Action1() { + .subscribe(new Consumer() { @Override - public void call(byte[] bytes) { + public void accept(byte[] bytes) { showResult(bytes); } }); @@ -126,21 +126,21 @@ private void compressWithRx(Bitmap bitmap) { .asObservable() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .doOnError(new Action1() { + .doOnError(new Consumer() { @Override - public void call(Throwable throwable) { + public void accept(Throwable throwable) { throwable.printStackTrace(); } }) - .onErrorResumeNext(new Func1>() { + .onErrorResumeNext(new Function>() { @Override - public Observable call(Throwable throwable) { + public Observable apply(Throwable throwable) { return Observable.empty(); } }) - .subscribe(new Action1() { + .subscribe(new Consumer() { @Override - public void call(byte[] bytes) { + public void accept(byte[] bytes) { showResult(bytes); } }); diff --git a/rxtaishan/build.gradle b/rxtaishan/build.gradle index 268e886..9cbfbd8 100644 --- a/rxtaishan/build.gradle +++ b/rxtaishan/build.gradle @@ -1,7 +1,7 @@ apply plugin: 'com.android.library' apply plugin: 'com.github.dcendents.android-maven' -group='com.github.lengyue524' +group='com.github.brooklet' android { compileSdkVersion 21 @@ -23,6 +23,7 @@ android { dependencies { compile project (':taishan') - compile 'io.reactivex:rxjava:1.1.9' - compile 'io.reactivex:rxandroid:1.2.1' + testCompile 'junit:junit:4.12' + compile 'io.reactivex.rxjava2:rxjava:2.1.2' + compile 'io.reactivex.rxjava2:rxandroid:2.0.1' } diff --git a/rxtaishan/src/main/java/com/lengyue524/rxtaishan/RxTaiShan.java b/rxtaishan/src/main/java/com/lengyue524/rxtaishan/RxTaiShan.java index 3b60607..25cf2d8 100644 --- a/rxtaishan/src/main/java/com/lengyue524/rxtaishan/RxTaiShan.java +++ b/rxtaishan/src/main/java/com/lengyue524/rxtaishan/RxTaiShan.java @@ -14,11 +14,13 @@ import java.io.FileOutputStream; import java.io.IOException; -import rx.Observable; -import rx.Subscriber; -import rx.android.schedulers.AndroidSchedulers; -import rx.functions.Action1; -import rx.schedulers.Schedulers; +import io.reactivex.Observable; +import io.reactivex.ObservableEmitter; +import io.reactivex.ObservableOnSubscribe; +import io.reactivex.annotations.NonNull; +import io.reactivex.schedulers.Schedulers; +import io.reactivex.functions.Consumer; +import io.reactivex.android.schedulers.AndroidSchedulers; public class RxTaiShan { private TaiShan mTaiShan; @@ -57,17 +59,18 @@ public void launch() { if (compressListener != null) { compressListener.onStart(); } + asObservable().observeOn(Schedulers.computation()) - .subscribeOn(AndroidSchedulers.mainThread()).subscribe(new Action1() { + .subscribeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer() { @Override - public void call(byte[] bytes) { + public void accept(byte[] bytes) { if (compressListener != null) { compressListener.onSuccess(bytes); } } - }, new Action1() { + }, new Consumer() { @Override - public void call(Throwable throwable) { + public void accept(Throwable throwable) { if (compressListener != null) { compressListener.onError(throwable); } @@ -76,9 +79,9 @@ public void call(Throwable throwable) { } public Observable asObservable() { - return Observable.create(new Observable.OnSubscribe() { + return Observable.create(new ObservableOnSubscribe() { @Override - public void call(Subscriber subscriber) { + public void subscribe(@NonNull ObservableEmitter subscriber) throws Exception { subscriber.onNext(mTaiShan.launch()); } }); diff --git a/settings.gradle b/settings.gradle index 0628102..8282f40 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include ':app', ':taishan', ':rxtaishan', ':rxtaishan' +include ':app', ':taishan', ':rxtaishan' diff --git a/taishan/build.gradle b/taishan/build.gradle index c267825..beb5f8a 100644 --- a/taishan/build.gradle +++ b/taishan/build.gradle @@ -1,7 +1,7 @@ apply plugin: 'com.android.library' apply plugin: 'com.github.dcendents.android-maven' -group='com.github.lengyue524' +group='com.github.brooklet' android { compileSdkVersion 21 @@ -22,4 +22,5 @@ android { } dependencies { + testCompile 'junit:junit:4.12' }