Skip to content

Commit e7f07b5

Browse files
author
Kaushik Gopal
committed
fix: merge conflict rxjava_2
2 parents 4e4bf24 + 4a1c945 commit e7f07b5

38 files changed

+931
-1031
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ I've also been giving talks about Learning Rx using many of the examples listed
88
* [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)
99
* [Learning Rx by Example : Part 2](https://vimeo.com/190922794) \[[slides](https://speakerdeck.com/kaushikgopal/learning-rx-by-example-2)\] (Øredev 2016)
1010

11-
1211
## Examples:
1312

1413
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
2625
13. [Networking with Volley](https://github.com/kaushikgopal/RxJava-Android-Samples/blob/master/README.md#13-networking-with-volley)
2726
14. [Pagination with Rx (using Subjects)](https://github.com/kaushikgopal/RxJava-Android-Samples/blob/master/README.md#14-pagination-with-rx-using-subjects)
2827
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)]()
2929

3030
## Description
3131

@@ -215,12 +215,20 @@ The below ascii diagram expresses the intention of our next example with panache
215215

216216
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.
217217

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
219225

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.
221227

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)
223230

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.
224232

225233
## Contributing:
226234

app/build.gradle

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,34 @@ dependencies {
2929
compile "com.android.support:appcompat-v7:${supportLibVersion}"
3030
compile "com.android.support:recyclerview-v7:${supportLibVersion}"
3131

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'
3732
compile 'com.github.kaushikgopal:CoreTextUtils:c703fa12b6'
38-
compile 'com.jakewharton.rxbinding:rxbinding:0.2.0'
3933
compile 'com.jakewharton:butterknife:7.0.1'
40-
compile 'com.jakewharton.rxrelay:rxrelay:1.2.0'
4134
compile 'com.jakewharton.timber:timber:2.4.2'
4235
compile "com.squareup.retrofit2:retrofit:${retrofitVersion}"
43-
compile "com.squareup.retrofit2:adapter-rxjava:${retrofitVersion}"
4436
compile "com.squareup.retrofit2:converter-gson:${retrofitVersion}"
4537
compile "com.squareup.okhttp3:okhttp:${okhttpVersion}"
4638
compile "com.squareup.okhttp3:okhttp-urlconnection:${okhttpVersion}"
4739
compile 'com.mcxiaoke.volley:library:1.0.19'
4840

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+
4960
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3'
5061
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3'
5162
}
@@ -71,4 +82,8 @@ android {
7182
sourceCompatibility JavaVersion.VERSION_1_8
7283
targetCompatibility JavaVersion.VERSION_1_8
7384
}
85+
86+
packagingOptions {
87+
pickFirst 'META-INF/rxjava.properties'
88+
}
7489
}

app/src/main/java/com/morihacky/android/rxjava/MainActivity.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@ public class MainActivity
1313

1414
private RxBus _rxBus = null;
1515

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-
2516
@Override
2617
public void onBackPressed() {
2718
super.onBackPressed();
@@ -34,24 +25,37 @@ protected void onCreate(Bundle savedInstanceState) {
3425

3526
if (savedInstanceState == null) {
3627
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();
3930
}
4031
}
4132

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+
4242
private void _removeWorkerFragments() {
43-
Fragment frag = getSupportFragmentManager()//
44-
.findFragmentByTag(RotationPersist1WorkerFragment.class.getName());
43+
Fragment frag = getSupportFragmentManager().findFragmentByTag(RotationPersist1WorkerFragment.class.getName());
4544

4645
if (frag != null) {
47-
getSupportFragmentManager().beginTransaction().remove(frag).commit();
46+
getSupportFragmentManager()
47+
.beginTransaction()
48+
.remove(frag)
49+
.commit();
4850
}
4951

50-
frag = getSupportFragmentManager()//
51-
.findFragmentByTag(RotationPersist2WorkerFragment.class.getName());
52+
frag = getSupportFragmentManager().findFragmentByTag(RotationPersist2WorkerFragment.class.getName());
5253

5354
if (frag != null) {
54-
getSupportFragmentManager().beginTransaction().remove(frag).commit();
55+
getSupportFragmentManager()
56+
.beginTransaction()
57+
.remove(frag)
58+
.commit();
5559
}
5660
}
5761
}

app/src/main/java/com/morihacky/android/rxjava/MyApp.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.morihacky.android.rxjava;
22

33
import android.app.Application;
4-
54
import com.morihacky.android.rxjava.volley.MyVolley;
65
import com.squareup.leakcanary.LeakCanary;
76
import com.squareup.leakcanary.RefWatcher;
@@ -28,6 +27,9 @@ public void onCreate() {
2827
_instance = (MyApp) getApplicationContext();
2928
_refWatcher = LeakCanary.install(this);
3029

30+
// for better RxJava debugging
31+
//RxJavaHooks.enableAssemblyTracking();
32+
3133
// Initialize Volley
3234
MyVolley.init(this);
3335

app/src/main/java/com/morihacky/android/rxjava/RxUtils.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

app/src/main/java/com/morihacky/android/rxjava/fragments/BufferDemoFragment.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020

2121
import butterknife.Bind;
2222
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;
2627
import timber.log.Timber;
2728

2829
/**
@@ -48,18 +49,18 @@ public class BufferDemoFragment
4849
private LogAdapter _adapter;
4950
private List<String> _logs;
5051

51-
private Subscription _subscription;
52+
private Disposable _disposable;
5253

5354
@Override
5455
public void onResume() {
5556
super.onResume();
56-
_subscription = _getBufferedSubscription();
57+
_disposable = _getBufferedDisposable();
5758
}
5859

5960
@Override
6061
public void onPause() {
6162
super.onPause();
62-
_subscription.unsubscribe();
63+
_disposable.dispose();
6364
}
6465

6566
@Override
@@ -85,19 +86,19 @@ public View onCreateView(LayoutInflater inflater,
8586
// -----------------------------------------------------------------------------------
8687
// Main Rx entities
8788

88-
private Subscription _getBufferedSubscription() {
89-
return RxView.clickEvents(_tapBtn)
89+
private Disposable _getBufferedDisposable() {
90+
return RxJavaInterop.toV2Observable(RxView.clickEvents(_tapBtn))
9091
.map(onClickEvent -> {
9192
Timber.d("--------- GOT A TAP");
9293
_log("GOT A TAP");
9394
return 1;
9495
})
9596
.buffer(2, TimeUnit.SECONDS)
9697
.observeOn(AndroidSchedulers.mainThread())
97-
.subscribe(new Observer<List<Integer>>() {
98+
.subscribeWith(new DisposableObserver<List<Integer>>() {
9899

99100
@Override
100-
public void onCompleted() {
101+
public void onComplete() {
101102
// fyi: you'll never reach here
102103
Timber.d("----- onCompleted");
103104
}

app/src/main/java/com/morihacky/android/rxjava/fragments/ConcurrencyWithSchedulersDemoFragment.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
import butterknife.ButterKnife;
1616
import butterknife.OnClick;
1717
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;
1823
import java.util.ArrayList;
1924
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;
2625
import timber.log.Timber;
2726

2827
public class ConcurrencyWithSchedulersDemoFragment
@@ -33,13 +32,13 @@ public class ConcurrencyWithSchedulersDemoFragment
3332

3433
private LogAdapter _adapter;
3534
private List<String> _logs;
36-
private CompositeSubscription _subscriptions = new CompositeSubscription();
35+
private CompositeDisposable _disposables = new CompositeDisposable();
3736

3837
@Override
3938
public void onDestroy() {
4039
super.onDestroy();
4140
ButterKnife.unbind(this);
42-
_subscriptions.clear();
41+
_disposables.clear();
4342
}
4443

4544
@Override
@@ -63,12 +62,14 @@ public void startLongOperation() {
6362
_progress.setVisibility(View.VISIBLE);
6463
_log("Button Clicked");
6564

66-
Subscription s = _getObservable()//
65+
DisposableObserver<Boolean> d = _getDisposableObserver();
66+
67+
_getObservable()
6768
.subscribeOn(Schedulers.io())
6869
.observeOn(AndroidSchedulers.mainThread())
69-
.subscribe(_getObserver()); // Observer
70+
.subscribe(d);
7071

71-
_subscriptions.add(s);
72+
_disposables.add(d);
7273
}
7374

7475
private Observable<Boolean> _getObservable() {
@@ -86,11 +87,11 @@ private Observable<Boolean> _getObservable() {
8687
* 2. onError
8788
* 3. onNext
8889
*/
89-
private Observer<Boolean> _getObserver() {
90-
return new Observer<Boolean>() {
90+
private DisposableObserver<Boolean> _getDisposableObserver() {
91+
return new DisposableObserver<Boolean>() {
9192

9293
@Override
93-
public void onCompleted() {
94+
public void onComplete() {
9495
_log("On complete");
9596
_progress.setVisibility(View.INVISIBLE);
9697
}

app/src/main/java/com/morihacky/android/rxjava/fragments/DebounceSearchEmitterFragment.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
import butterknife.Bind;
2424
import butterknife.ButterKnife;
2525
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;
2930
import timber.log.Timber;
3031

3132
import static co.kaush.core.util.CoreNullnessUtils.isNotNullOrEmpty;
@@ -40,12 +41,12 @@ public class DebounceSearchEmitterFragment
4041
private LogAdapter _adapter;
4142
private List<String> _logs;
4243

43-
private Subscription _subscription;
44+
private Disposable _disposable;
4445

4546
@Override
4647
public void onDestroy() {
4748
super.onDestroy();
48-
_subscription.unsubscribe();
49+
_disposable.dispose();
4950
ButterKnife.unbind(this);
5051
}
5152

@@ -70,20 +71,20 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
7071
super.onActivityCreated(savedInstanceState);
7172
_setupLogger();
7273

73-
_subscription = RxTextView.textChangeEvents(_inputSearchText)
74+
_disposable = RxJavaInterop.toV2Observable(RxTextView.textChangeEvents(_inputSearchText))
7475
.debounce(400, TimeUnit.MILLISECONDS)// default Scheduler is Computation
7576
.filter(changes -> isNotNullOrEmpty(_inputSearchText.getText().toString()))
7677
.observeOn(AndroidSchedulers.mainThread())
77-
.subscribe(_getSearchObserver());
78+
.subscribeWith(_getSearchObserver());
7879
}
7980

8081
// -----------------------------------------------------------------------------------
8182
// Main Rx entities
8283

83-
private Observer<TextViewTextChangeEvent> _getSearchObserver() {
84-
return new Observer<TextViewTextChangeEvent>() {
84+
private DisposableObserver<TextViewTextChangeEvent> _getSearchObserver() {
85+
return new DisposableObserver<TextViewTextChangeEvent>() {
8586
@Override
86-
public void onCompleted() {
87+
public void onComplete() {
8788
Timber.d("--------- onComplete");
8889
}
8990

0 commit comments

Comments
 (0)