Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: port to RxJava 2.X #83

Merged
merged 40 commits into from
Nov 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
e200ab5
chore: update android gradle plugin
Nov 27, 2016
f4a5cc2
being rxjava 2 migration. see #82
Nov 27, 2016
1a574ac
fix: delete pseudo concat fragment (already covered)
Nov 27, 2016
88da53c
chore: add required 2.x dependencies
Nov 27, 2016
f45864c
chore: update README + table of contents for e.g.s
kaushikgopal Nov 27, 2016
a7add45
fix: readme update links
kaushikgopal Nov 27, 2016
cdea8e5
fix: link for orchestrating observables
kaushikgopal Nov 27, 2016
5351e76
Updated retrofit setup to return RxJava2 observables.
marcinkunert Nov 27, 2016
3cff7c4
Adjusted retrofit related examples
marcinkunert Nov 27, 2016
abcb417
Added packagingOptions so RxJava 1 and RxJava 2 can work on the same …
marcinkunert Nov 27, 2016
6b05d3c
Updated RxJava to 2.0.1
marcinkunert Nov 27, 2016
e3a0abb
Replaced RxJava1 retrofit adapter with the RxJava2 version
marcinkunert Nov 27, 2016
0f6dd0c
Updated buffer demo to RxJava2
marcinkunert Nov 27, 2016
b855d0f
Updated debounce demo to RxJava2
marcinkunert Nov 27, 2016
9b2a17a
feat: port example Simple timing demos (using timer, interval or delay)
Nov 27, 2016
5f78f0a
fix: resolve merge conflict on build.gradel
Nov 27, 2016
76032a9
chore: update README + table of contents for e.g.s
kaushikgopal Nov 27, 2016
922b981
fix: readme update links
kaushikgopal Nov 27, 2016
3353483
fix: link for orchestrating observables
kaushikgopal Nov 27, 2016
bdcd334
Updated retrofit setup to return RxJava2 observables.
marcinkunert Nov 27, 2016
ecdb7ff
Adjusted retrofit related examples
marcinkunert Nov 27, 2016
9b79441
feat: port Retrofit related examples
Nov 27, 2016
ae5ce9f
Updated RxJava to 2.0.1
marcinkunert Nov 27, 2016
8e197da
Replaced RxJava1 retrofit adapter with the RxJava2 version
marcinkunert Nov 27, 2016
47e6616
Merge pull request #84 from marcinkunert/feat/rxjava_2
kaushikgopal Nov 27, 2016
9e21aff
feat: port example Accumulate calls (using buffer) (#85)
kaushikgopal Nov 27, 2016
5cc38d5
feat: port example Instant/Auto searching text listeners (using Subje…
kaushikgopal Nov 27, 2016
5585bba
fix: remove silly comment style helpers
Nov 27, 2016
c240b93
feat: port pagination + bus examples fix: no null returns
Nov 28, 2016
6696135
feat: port example Background work & concurrency (using Schedulers)
Nov 28, 2016
ca56b5a
feat: port example Two-way data binding for TextViews (using Publish…
Nov 28, 2016
74ece66
feat: port example Simple and Advanced polling (using interval and re…
Nov 28, 2016
96aff27
feat: port example Simple and Advanced exponential backoff (using del…
Nov 28, 2016
d83a666
feat: port timeout + playground
Nov 28, 2016
59b8195
feat: port example Form validation (using combineLatest)
Nov 28, 2016
897ea73
feat: port Activity rotation examples
Nov 28, 2016
23f1fbf
feat: port volley example
Nov 28, 2016
9a5f53b
feat: remove direct Rx 1.X dependencies
Nov 28, 2016
4151646
feat: add Timeout example to README
Nov 28, 2016
4a1c945
feat: point to PR
Nov 28, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 95 additions & 61 deletions README.md

Large diffs are not rendered by default.

31 changes: 23 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,34 @@ dependencies {
compile "com.android.support:appcompat-v7:${supportLibVersion}"
compile "com.android.support:recyclerview-v7:${supportLibVersion}"

compile 'io.reactivex:rxandroid:1.2.0'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
compile 'io.reactivex:rxjava:1.1.4'
compile 'io.reactivex:rxjava-math:1.0.0'
compile 'com.github.kaushikgopal:CoreTextUtils:c703fa12b6'
compile 'com.jakewharton.rxbinding:rxbinding:0.2.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.jakewharton.rxrelay:rxrelay:1.2.0'
compile 'com.jakewharton.timber:timber:2.4.2'
compile "com.squareup.retrofit2:retrofit:${retrofitVersion}"
compile "com.squareup.retrofit2:adapter-rxjava:${retrofitVersion}"
compile "com.squareup.retrofit2:converter-gson:${retrofitVersion}"
compile "com.squareup.okhttp3:okhttp:${okhttpVersion}"
compile "com.squareup.okhttp3:okhttp-urlconnection:${okhttpVersion}"
compile 'com.mcxiaoke.volley:library:1.0.19'

// ----------------------------------
// Rx dependencies

compile 'io.reactivex.rxjava2:rxjava:2.0.1'

// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'

// libs like rxbinding + rxjava-math haven't been ported to RxJava 2.x yet, so this helps
compile "com.github.akarnokd:rxjava2-interop:0.6.1"

compile 'io.reactivex:rxjava-math:1.0.0'
compile 'com.jakewharton.rxrelay:rxrelay:1.2.0'
compile 'com.jakewharton.rxbinding:rxbinding:0.2.0'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'

// ----------------------------------

debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3'
}
Expand All @@ -71,4 +82,8 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

packagingOptions {
pickFirst 'META-INF/rxjava.properties'
}
}
38 changes: 21 additions & 17 deletions app/src/main/java/com/morihacky/android/rxjava/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ public class MainActivity

private RxBus _rxBus = null;

// This is better done with a DI Library like Dagger
public RxBus getRxBusSingleton() {
if (_rxBus == null) {
_rxBus = new RxBus();
}

return _rxBus;
}

@Override
public void onBackPressed() {
super.onBackPressed();
Expand All @@ -34,24 +25,37 @@ protected void onCreate(Bundle savedInstanceState) {

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, new MainFragment(), this.toString())
.commit();
.replace(android.R.id.content, new MainFragment(), this.toString())
.commit();
}
}

// This is better done with a DI Library like Dagger
public RxBus getRxBusSingleton() {
if (_rxBus == null) {
_rxBus = new RxBus();
}

return _rxBus;
}

private void _removeWorkerFragments() {
Fragment frag = getSupportFragmentManager()//
.findFragmentByTag(RotationPersist1WorkerFragment.class.getName());
Fragment frag = getSupportFragmentManager().findFragmentByTag(RotationPersist1WorkerFragment.class.getName());

if (frag != null) {
getSupportFragmentManager().beginTransaction().remove(frag).commit();
getSupportFragmentManager()
.beginTransaction()
.remove(frag)
.commit();
}

frag = getSupportFragmentManager()//
.findFragmentByTag(RotationPersist2WorkerFragment.class.getName());
frag = getSupportFragmentManager().findFragmentByTag(RotationPersist2WorkerFragment.class.getName());

if (frag != null) {
getSupportFragmentManager().beginTransaction().remove(frag).commit();
getSupportFragmentManager()
.beginTransaction()
.remove(frag)
.commit();
}
}
}
4 changes: 3 additions & 1 deletion app/src/main/java/com/morihacky/android/rxjava/MyApp.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.morihacky.android.rxjava;

import android.app.Application;

import com.morihacky.android.rxjava.volley.MyVolley;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
Expand All @@ -28,6 +27,9 @@ public void onCreate() {
_instance = (MyApp) getApplicationContext();
_refWatcher = LeakCanary.install(this);

// for better RxJava debugging
//RxJavaHooks.enableAssemblyTracking();

// Initialize Volley
MyVolley.init(this);

Expand Down
12 changes: 0 additions & 12 deletions app/src/main/java/com/morihacky/android/rxjava/RxUtils.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import hu.akarnokd.rxjava.interop.RxJavaInterop;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import timber.log.Timber;

/**
Expand All @@ -48,18 +49,18 @@ public class BufferDemoFragment
private LogAdapter _adapter;
private List<String> _logs;

private Subscription _subscription;
private Disposable _disposable;

@Override
public void onResume() {
super.onResume();
_subscription = _getBufferedSubscription();
_disposable = _getBufferedDisposable();
}

@Override
public void onPause() {
super.onPause();
_subscription.unsubscribe();
_disposable.dispose();
}

@Override
Expand All @@ -85,19 +86,19 @@ public View onCreateView(LayoutInflater inflater,
// -----------------------------------------------------------------------------------
// Main Rx entities

private Subscription _getBufferedSubscription() {
return RxView.clickEvents(_tapBtn)
private Disposable _getBufferedDisposable() {
return RxJavaInterop.toV2Observable(RxView.clickEvents(_tapBtn))
.map(onClickEvent -> {
Timber.d("--------- GOT A TAP");
_log("GOT A TAP");
return 1;
})
.buffer(2, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<Integer>>() {
.subscribeWith(new DisposableObserver<List<Integer>>() {

@Override
public void onCompleted() {
public void onComplete() {
// fyi: you'll never reach here
Timber.d("----- onCompleted");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.morihacky.android.rxjava.R;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import java.util.ArrayList;
import java.util.List;
import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;
import timber.log.Timber;

public class ConcurrencyWithSchedulersDemoFragment
Expand All @@ -33,13 +32,13 @@ public class ConcurrencyWithSchedulersDemoFragment

private LogAdapter _adapter;
private List<String> _logs;
private CompositeSubscription _subscriptions = new CompositeSubscription();
private CompositeDisposable _disposables = new CompositeDisposable();

@Override
public void onDestroy() {
super.onDestroy();
ButterKnife.unbind(this);
_subscriptions.clear();
_disposables.clear();
}

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

Subscription s = _getObservable()//
DisposableObserver<Boolean> d = _getDisposableObserver();

_getObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(_getObserver()); // Observer
.subscribe(d);

_subscriptions.add(s);
_disposables.add(d);
}

private Observable<Boolean> _getObservable() {
Expand All @@ -86,11 +87,11 @@ private Observable<Boolean> _getObservable() {
* 2. onError
* 3. onNext
*/
private Observer<Boolean> _getObserver() {
return new Observer<Boolean>() {
private DisposableObserver<Boolean> _getDisposableObserver() {
return new DisposableObserver<Boolean>() {

@Override
public void onCompleted() {
public void onComplete() {
_log("On complete");
_progress.setVisibility(View.INVISIBLE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import hu.akarnokd.rxjava.interop.RxJavaInterop;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import timber.log.Timber;

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

private Subscription _subscription;
private Disposable _disposable;

@Override
public void onDestroy() {
super.onDestroy();
_subscription.unsubscribe();
_disposable.dispose();
ButterKnife.unbind(this);
}

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

_subscription = RxTextView.textChangeEvents(_inputSearchText)
_disposable = RxJavaInterop.toV2Observable(RxTextView.textChangeEvents(_inputSearchText))
.debounce(400, TimeUnit.MILLISECONDS)// default Scheduler is Computation
.filter(changes -> isNotNullOrEmpty(_inputSearchText.getText().toString()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(_getSearchObserver());
.subscribeWith(_getSearchObserver());
}

// -----------------------------------------------------------------------------------
// Main Rx entities

private Observer<TextViewTextChangeEvent> _getSearchObserver() {
return new Observer<TextViewTextChangeEvent>() {
private DisposableObserver<TextViewTextChangeEvent> _getSearchObserver() {
return new DisposableObserver<TextViewTextChangeEvent>() {
@Override
public void onCompleted() {
public void onComplete() {
Timber.d("--------- onComplete");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.morihacky.android.rxjava.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnTextChanged;
import rx.Subscription;
import rx.subjects.PublishSubject;
import com.morihacky.android.rxjava.R;
import io.reactivex.disposables.Disposable;
import io.reactivex.processors.PublishProcessor;


import static android.text.TextUtils.isEmpty;

Expand All @@ -25,8 +24,8 @@ public class DoubleBindingTextViewFragment
@Bind(R.id.double_binding_num2) EditText _number2;
@Bind(R.id.double_binding_result) TextView _result;

Subscription _subscription;
PublishSubject<Float> _resultEmitterSubject;
Disposable _disposable;
PublishProcessor<Float> _resultEmitterSubject;

@Override
public View onCreateView(LayoutInflater inflater,
Expand All @@ -35,12 +34,11 @@ public View onCreateView(LayoutInflater inflater,
View layout = inflater.inflate(R.layout.fragment_double_binding_textview, container, false);
ButterKnife.bind(this, layout);

_resultEmitterSubject = PublishSubject.create();
_subscription = _resultEmitterSubject//
.asObservable()//
.subscribe(aFloat -> {
_result.setText(String.valueOf(aFloat));
});
_resultEmitterSubject = PublishProcessor.create();

_disposable = _resultEmitterSubject.subscribe(aFloat -> {
_result.setText(String.valueOf(aFloat));
});

onNumberChanged();
_number2.requestFocus();
Expand All @@ -67,7 +65,7 @@ public void onNumberChanged() {
@Override
public void onDestroyView() {
super.onDestroyView();
_subscription.unsubscribe();
_disposable.dispose();
ButterKnife.unbind(this);
}
}
Loading