GeezerVison is showing github.com — fix: merge conflict rxjava_2 · linuxgg/RxJava-Android-Samples@e7f07b5
File treeExpand file treeCollapse file tree Open diff view settings java/com/morihacky/android/rxjava
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +12-4Lines changed: 12 additions & 4 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -8,7 +8,6 @@ I've also been giving talks about Learning Rx using many of the examples listed | 8 | 8 | * [Learning RxJava For Android by Example : Part 1](https://www.youtube.com/watch?v=k3D0cWyNno4) \[[slides](https://speakerdeck.com/kaushikgopal/learning-rxjava-for-android-by-example)\] (SF Android Meetup 2015) | 9 | 9 | * [Learning Rx by Example : Part 2](https://vimeo.com/190922794) \[[slides](https://speakerdeck.com/kaushikgopal/learning-rx-by-example-2)\] (Øredev 2016) | 10 | 10 |
| 11 | | - | 12 | 11 | ## Examples: | 13 | 12 |
| 14 | 13 | 1. [Background work & concurrency (using Schedulers)](https://github.com/kaushikgopal/RxJava-Android-Samples/blob/master/README.md#1-background-work--concurrency-using-schedulers) | @@ -26,6 +25,7 @@ I've also been giving talks about Learning Rx using many of the examples listed | 26 | 25 | 13. [Networking with Volley](https://github.com/kaushikgopal/RxJava-Android-Samples/blob/master/README.md#13-networking-with-volley) | 27 | 26 | 14. [Pagination with Rx (using Subjects)](https://github.com/kaushikgopal/RxJava-Android-Samples/blob/master/README.md#14-pagination-with-rx-using-subjects) | 28 | 27 | 15. [Orchestrating Observable: make parallel network calls, then combine the result into a single data point (using flatmap & zip)](https://github.com/kaushikgopal/RxJava-Android-Samples/blob/master/README.md#15-orchestrating-observable-make-parallel-network-calls-then-combine-the-result-into-a-single-data-point-using-flatmap--zip) | | 28 | +16. [Simple Timeout example (using timeout)]() | 29 | 29 |
| 30 | 30 | ## Description | 31 | 31 |
| @@ -215,12 +215,20 @@ The below ascii diagram expresses the intention of our next example with panache | 215 | 215 |
| 216 | 216 | The code for this example has already been written by one Mr.skehlet in the interwebs. Head over to [the gist](https://gist.github.com/skehlet/9418379) for the code. It's written in pure Java (6) so it's pretty comprehensible if you've understood the previous examples. I'll flush it out here again when time permits or I've run out of other compelling examples. | 217 | 217 |
| 218 | | -## Work in Progress: | | 218 | +### 16. Simple Timeout example (using timeout) | | 219 | + | | 220 | +This is a simple example demonstrating the use of the `.timeout` operator. Button 1 will complete the task before the timeout constraint, while Button 2 will force a timeout error. | | 221 | + | | 222 | +Notice how we can provide a custom Observable that indicates how to react under a timeout Exception. | | 223 | + | | 224 | +## Rx 2.x | 219 | 225 |
| 220 | | -_Examples that I would like to have here, but haven't found the time yet to flush out_ | | 226 | +All the examples here have been migrated to use RxJava 2.X. | 221 | 227 |
| 222 | | -(queue is empty) | | 228 | +* Have a look at [PR #83 to see the diff of changes between RxJava 1 and 2](https://github.com/kaushikgopal/RxJava-Android-Samples/pull/83/files) | | 229 | +* [What's different in Rx 2.x](https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0) | 223 | 230 |
| | 231 | +We use [David Karnok's Interop library](https://github.com/akarnokd/RxJava2Interop) in some cases as certain libraries like RxBindings, RxRelays, RxJava-Math etc. have not been ported yet to 2.x. | 224 | 232 |
| 225 | 233 | ## Contributing: | 226 | 234 |
| |
Collapse file Copy file name to clipboardExpand all lines: app/build.gradle +23-8Lines changed: 23 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,23 +29,34 @@ dependencies { | 29 | 29 | compile "com.android.support:appcompat-v7:${supportLibVersion}" | 30 | 30 | compile "com.android.support:recyclerview-v7:${supportLibVersion}" | 31 | 31 |
| 32 | | - compile 'io.reactivex:rxandroid:1.2.0' | 33 | | - // Because RxAndroid releases are few and far between, it is recommended you also | 34 | | - // explicitly depend on RxJava's latest version for bug fixes and new features. | 35 | | - compile 'io.reactivex:rxjava:1.1.4' | 36 | | - compile 'io.reactivex:rxjava-math:1.0.0' | 37 | 32 | compile 'com.github.kaushikgopal:CoreTextUtils:c703fa12b6' | 38 | | - compile 'com.jakewharton.rxbinding:rxbinding:0.2.0' | 39 | 33 | compile 'com.jakewharton:butterknife:7.0.1' | 40 | | - compile 'com.jakewharton.rxrelay:rxrelay:1.2.0' | 41 | 34 | compile 'com.jakewharton.timber:timber:2.4.2' | 42 | 35 | compile "com.squareup.retrofit2:retrofit:${retrofitVersion}" | 43 | | - compile "com.squareup.retrofit2:adapter-rxjava:${retrofitVersion}" | 44 | 36 | compile "com.squareup.retrofit2:converter-gson:${retrofitVersion}" | 45 | 37 | compile "com.squareup.okhttp3:okhttp:${okhttpVersion}" | 46 | 38 | compile "com.squareup.okhttp3:okhttp-urlconnection:${okhttpVersion}" | 47 | 39 | compile 'com.mcxiaoke.volley:library:1.0.19' | 48 | 40 |
| | 41 | + // ---------------------------------- | | 42 | + // Rx dependencies | | 43 | + | | 44 | + compile 'io.reactivex.rxjava2:rxjava:2.0.1' | | 45 | + | | 46 | + // Because RxAndroid releases are few and far between, it is recommended you also | | 47 | + // explicitly depend on RxJava's latest version for bug fixes and new features. | | 48 | + compile 'io.reactivex.rxjava2:rxandroid:2.0.1' | | 49 | + | | 50 | + // libs like rxbinding + rxjava-math haven't been ported to RxJava 2.x yet, so this helps | | 51 | + compile "com.github.akarnokd:rxjava2-interop:0.6.1" | | 52 | + | | 53 | + compile 'io.reactivex:rxjava-math:1.0.0' | | 54 | + compile 'com.jakewharton.rxrelay:rxrelay:1.2.0' | | 55 | + compile 'com.jakewharton.rxbinding:rxbinding:0.2.0' | | 56 | + compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0' | | 57 | + | | 58 | + // ---------------------------------- | | 59 | + | 49 | 60 | debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3' | 50 | 61 | releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3' | 51 | 62 | } | @@ -71,4 +82,8 @@ android { | 71 | 82 | sourceCompatibility JavaVersion.VERSION_1_8 | 72 | 83 | targetCompatibility JavaVersion.VERSION_1_8 | 73 | 84 | } | | 85 | + | | 86 | + packagingOptions { | | 87 | + pickFirst 'META-INF/rxjava.properties' | | 88 | + } | 74 | 89 | } |
Collapse file Copy file name to clipboardExpand all lines: app/src/main/java/com/morihacky/android/rxjava/MainActivity.java +21-17Lines changed: 21 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,15 +13,6 @@ public class MainActivity | 13 | 13 |
| 14 | 14 | private RxBus _rxBus = null; | 15 | 15 |
| 16 | | - // This is better done with a DI Library like Dagger | 17 | | - public RxBus getRxBusSingleton() { | 18 | | - if (_rxBus == null) { | 19 | | - _rxBus = new RxBus(); | 20 | | - } | 21 | | - | 22 | | - return _rxBus; | 23 | | - } | 24 | | - | 25 | 16 | @Override | 26 | 17 | public void onBackPressed() { | 27 | 18 | super.onBackPressed(); | @@ -34,24 +25,37 @@ protected void onCreate(Bundle savedInstanceState) { | 34 | 25 |
| 35 | 26 | if (savedInstanceState == null) { | 36 | 27 | getSupportFragmentManager().beginTransaction() | 37 | | - .replace(android.R.id.content, new MainFragment(), this.toString()) | 38 | | - .commit(); | | 28 | + .replace(android.R.id.content, new MainFragment(), this.toString()) | | 29 | + .commit(); | 39 | 30 | } | 40 | 31 | } | 41 | 32 |
| | 33 | + // This is better done with a DI Library like Dagger | | 34 | + public RxBus getRxBusSingleton() { | | 35 | + if (_rxBus == null) { | | 36 | + _rxBus = new RxBus(); | | 37 | + } | | 38 | + | | 39 | + return _rxBus; | | 40 | + } | | 41 | + | 42 | 42 | private void _removeWorkerFragments() { | 43 | | - Fragment frag = getSupportFragmentManager()// | 44 | | - .findFragmentByTag(RotationPersist1WorkerFragment.class.getName()); | | 43 | + Fragment frag = getSupportFragmentManager().findFragmentByTag(RotationPersist1WorkerFragment.class.getName()); | 45 | 44 |
| 46 | 45 | if (frag != null) { | 47 | | - getSupportFragmentManager().beginTransaction().remove(frag).commit(); | | 46 | + getSupportFragmentManager() | | 47 | + .beginTransaction() | | 48 | + .remove(frag) | | 49 | + .commit(); | 48 | 50 | } | 49 | 51 |
| 50 | | - frag = getSupportFragmentManager()// | 51 | | - .findFragmentByTag(RotationPersist2WorkerFragment.class.getName()); | | 52 | + frag = getSupportFragmentManager().findFragmentByTag(RotationPersist2WorkerFragment.class.getName()); | 52 | 53 |
| 53 | 54 | if (frag != null) { | 54 | | - getSupportFragmentManager().beginTransaction().remove(frag).commit(); | | 55 | + getSupportFragmentManager() | | 56 | + .beginTransaction() | | 57 | + .remove(frag) | | 58 | + .commit(); | 55 | 59 | } | 56 | 60 | } | 57 | 61 | } |
Collapse file Copy file name to clipboardExpand all lines: app/src/main/java/com/morihacky/android/rxjava/MyApp.java +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | package com.morihacky.android.rxjava; | 2 | 2 |
| 3 | 3 | import android.app.Application; | 4 | | - | 5 | 4 | import com.morihacky.android.rxjava.volley.MyVolley; | 6 | 5 | import com.squareup.leakcanary.LeakCanary; | 7 | 6 | import com.squareup.leakcanary.RefWatcher; | @@ -28,6 +27,9 @@ public void onCreate() { | 28 | 27 | _instance = (MyApp) getApplicationContext(); | 29 | 28 | _refWatcher = LeakCanary.install(this); | 30 | 29 |
| | 30 | + // for better RxJava debugging | | 31 | + //RxJavaHooks.enableAssemblyTracking(); | | 32 | + | 31 | 33 | // Initialize Volley | 32 | 34 | MyVolley.init(this); | 33 | 35 |
| |
Collapse file Copy file name to clipboardExpand all lines: app/src/main/java/com/morihacky/android/rxjava/RxUtils.java -12Lines changed: 0 additions & 12 deletions Collapse file Copy file name to clipboardExpand all lines: app/src/main/java/com/morihacky/android/rxjava/fragments/BufferDemoFragment.java +11-10Lines changed: 11 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 |
| 21 | 21 | import butterknife.Bind; | 22 | 22 | import butterknife.ButterKnife; | 23 | | -import rx.Observer; | 24 | | -import rx.Subscription; | 25 | | -import rx.android.schedulers.AndroidSchedulers; | | 23 | +import hu.akarnokd.rxjava.interop.RxJavaInterop; | | 24 | +import io.reactivex.android.schedulers.AndroidSchedulers; | | 25 | +import io.reactivex.disposables.Disposable; | | 26 | +import io.reactivex.observers.DisposableObserver; | 26 | 27 | import timber.log.Timber; | 27 | 28 |
| 28 | 29 | /** | @@ -48,18 +49,18 @@ public class BufferDemoFragment | 48 | 49 | private LogAdapter _adapter; | 49 | 50 | private List<String> _logs; | 50 | 51 |
| 51 | | - private Subscription _subscription; | | 52 | + private Disposable _disposable; | 52 | 53 |
| 53 | 54 | @Override | 54 | 55 | public void onResume() { | 55 | 56 | super.onResume(); | 56 | | - _subscription = _getBufferedSubscription(); | | 57 | + _disposable = _getBufferedDisposable(); | 57 | 58 | } | 58 | 59 |
| 59 | 60 | @Override | 60 | 61 | public void onPause() { | 61 | 62 | super.onPause(); | 62 | | - _subscription.unsubscribe(); | | 63 | + _disposable.dispose(); | 63 | 64 | } | 64 | 65 |
| 65 | 66 | @Override | @@ -85,19 +86,19 @@ public View onCreateView(LayoutInflater inflater, | 85 | 86 | // ----------------------------------------------------------------------------------- | 86 | 87 | // Main Rx entities | 87 | 88 |
| 88 | | - private Subscription _getBufferedSubscription() { | 89 | | - return RxView.clickEvents(_tapBtn) | | 89 | + private Disposable _getBufferedDisposable() { | | 90 | + return RxJavaInterop.toV2Observable(RxView.clickEvents(_tapBtn)) | 90 | 91 | .map(onClickEvent -> { | 91 | 92 | Timber.d("--------- GOT A TAP"); | 92 | 93 | _log("GOT A TAP"); | 93 | 94 | return 1; | 94 | 95 | }) | 95 | 96 | .buffer(2, TimeUnit.SECONDS) | 96 | 97 | .observeOn(AndroidSchedulers.mainThread()) | 97 | | - .subscribe(new Observer<List<Integer>>() { | | 98 | + .subscribeWith(new DisposableObserver<List<Integer>>() { | 98 | 99 |
| 99 | 100 | @Override | 100 | | - public void onCompleted() { | | 101 | + public void onComplete() { | 101 | 102 | // fyi: you'll never reach here | 102 | 103 | Timber.d("----- onCompleted"); | 103 | 104 | } | |
Collapse file Copy file name to clipboardExpand all lines: app/src/main/java/com/morihacky/android/rxjava/fragments/ConcurrencyWithSchedulersDemoFragment.java +15-14Lines changed: 15 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 | import butterknife.ButterKnife; | 16 | 16 | import butterknife.OnClick; | 17 | 17 | import com.morihacky.android.rxjava.R; | | 18 | +import io.reactivex.Observable; | | 19 | +import io.reactivex.android.schedulers.AndroidSchedulers; | | 20 | +import io.reactivex.disposables.CompositeDisposable; | | 21 | +import io.reactivex.observers.DisposableObserver; | | 22 | +import io.reactivex.schedulers.Schedulers; | 18 | 23 | import java.util.ArrayList; | 19 | 24 | import java.util.List; | 20 | | -import rx.Observable; | 21 | | -import rx.Observer; | 22 | | -import rx.Subscription; | 23 | | -import rx.android.schedulers.AndroidSchedulers; | 24 | | -import rx.schedulers.Schedulers; | 25 | | -import rx.subscriptions.CompositeSubscription; | 26 | 25 | import timber.log.Timber; | 27 | 26 |
| 28 | 27 | public class ConcurrencyWithSchedulersDemoFragment | @@ -33,13 +32,13 @@ public class ConcurrencyWithSchedulersDemoFragment | 33 | 32 |
| 34 | 33 | private LogAdapter _adapter; | 35 | 34 | private List<String> _logs; | 36 | | - private CompositeSubscription _subscriptions = new CompositeSubscription(); | | 35 | + private CompositeDisposable _disposables = new CompositeDisposable(); | 37 | 36 |
| 38 | 37 | @Override | 39 | 38 | public void onDestroy() { | 40 | 39 | super.onDestroy(); | 41 | 40 | ButterKnife.unbind(this); | 42 | | - _subscriptions.clear(); | | 41 | + _disposables.clear(); | 43 | 42 | } | 44 | 43 |
| 45 | 44 | @Override | @@ -63,12 +62,14 @@ public void startLongOperation() { | 63 | 62 | _progress.setVisibility(View.VISIBLE); | 64 | 63 | _log("Button Clicked"); | 65 | 64 |
| 66 | | - Subscription s = _getObservable()// | | 65 | + DisposableObserver<Boolean> d = _getDisposableObserver(); | | 66 | + | | 67 | + _getObservable() | 67 | 68 | .subscribeOn(Schedulers.io()) | 68 | 69 | .observeOn(AndroidSchedulers.mainThread()) | 69 | | - .subscribe(_getObserver()); // Observer | | 70 | + .subscribe(d); | 70 | 71 |
| 71 | | - _subscriptions.add(s); | | 72 | + _disposables.add(d); | 72 | 73 | } | 73 | 74 |
| 74 | 75 | private Observable<Boolean> _getObservable() { | @@ -86,11 +87,11 @@ private Observable<Boolean> _getObservable() { | 86 | 87 | * 2. onError | 87 | 88 | * 3. onNext | 88 | 89 | */ | 89 | | - private Observer<Boolean> _getObserver() { | 90 | | - return new Observer<Boolean>() { | | 90 | + private DisposableObserver<Boolean> _getDisposableObserver() { | | 91 | + return new DisposableObserver<Boolean>() { | 91 | 92 |
| 92 | 93 | @Override | 93 | | - public void onCompleted() { | | 94 | + public void onComplete() { | 94 | 95 | _log("On complete"); | 95 | 96 | _progress.setVisibility(View.INVISIBLE); | 96 | 97 | } | |
Collapse file Copy file name to clipboardExpand all lines: app/src/main/java/com/morihacky/android/rxjava/fragments/DebounceSearchEmitterFragment.java +11-10Lines changed: 11 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 23 | 23 | import butterknife.Bind; | 24 | 24 | import butterknife.ButterKnife; | 25 | 25 | import butterknife.OnClick; | 26 | | -import rx.Observer; | 27 | | -import rx.Subscription; | 28 | | -import rx.android.schedulers.AndroidSchedulers; | | 26 | +import hu.akarnokd.rxjava.interop.RxJavaInterop; | | 27 | +import io.reactivex.android.schedulers.AndroidSchedulers; | | 28 | +import io.reactivex.disposables.Disposable; | | 29 | +import io.reactivex.observers.DisposableObserver; | 29 | 30 | import timber.log.Timber; | 30 | 31 |
| 31 | 32 | import static co.kaush.core.util.CoreNullnessUtils.isNotNullOrEmpty; | @@ -40,12 +41,12 @@ public class DebounceSearchEmitterFragment | 40 | 41 | private LogAdapter _adapter; | 41 | 42 | private List<String> _logs; | 42 | 43 |
| 43 | | - private Subscription _subscription; | | 44 | + private Disposable _disposable; | 44 | 45 |
| 45 | 46 | @Override | 46 | 47 | public void onDestroy() { | 47 | 48 | super.onDestroy(); | 48 | | - _subscription.unsubscribe(); | | 49 | + _disposable.dispose(); | 49 | 50 | ButterKnife.unbind(this); | 50 | 51 | } | 51 | 52 |
| @@ -70,20 +71,20 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) { | 70 | 71 | super.onActivityCreated(savedInstanceState); | 71 | 72 | _setupLogger(); | 72 | 73 |
| 73 | | - _subscription = RxTextView.textChangeEvents(_inputSearchText) | | 74 | + _disposable = RxJavaInterop.toV2Observable(RxTextView.textChangeEvents(_inputSearchText)) | 74 | 75 | .debounce(400, TimeUnit.MILLISECONDS)// default Scheduler is Computation | 75 | 76 | .filter(changes -> isNotNullOrEmpty(_inputSearchText.getText().toString())) | 76 | 77 | .observeOn(AndroidSchedulers.mainThread()) | 77 | | - .subscribe(_getSearchObserver()); | | 78 | + .subscribeWith(_getSearchObserver()); | 78 | 79 | } | 79 | 80 |
| 80 | 81 | // ----------------------------------------------------------------------------------- | 81 | 82 | // Main Rx entities | 82 | 83 |
| 83 | | - private Observer<TextViewTextChangeEvent> _getSearchObserver() { | 84 | | - return new Observer<TextViewTextChangeEvent>() { | | 84 | + private DisposableObserver<TextViewTextChangeEvent> _getSearchObserver() { | | 85 | + return new DisposableObserver<TextViewTextChangeEvent>() { | 85 | 86 | @Override | 86 | | - public void onCompleted() { | | 87 | + public void onComplete() { | 87 | 88 | Timber.d("--------- onComplete"); | 88 | 89 | } | 89 | 90 |
| |
|