Skip to content

Commit

Permalink
[gradle][rxjava] migrate to rxjava 2.x part1
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoslacz committed Feb 20, 2017
1 parent 8bf204b commit b1ffffc
Show file tree
Hide file tree
Showing 108 changed files with 252 additions and 274 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ ext {
buildToolsVersion = '25.0.2'
supportVersion = '25.1.0'
mosbyVersion = '2.0.1'
rxJavaVersion = '1.2.3'
rxAndroidVersion = '1.2.1'
rxJavaVersion = '2.0.3'
rxAndroidVersion = '2.0.1'
rxBindingVersion = '1.0.0'
butterKnifeVersion = '8.4.0'
retrofitVersion = '2.0.2'
Expand Down
4 changes: 2 additions & 2 deletions moviper-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:' + rootProject.ext.supportVersion
compile 'io.reactivex:rxjava:' + rootProject.ext.rxJavaVersion
compile 'io.reactivex:rxandroid:' + rootProject.ext.rxAndroidVersion
compile 'io.reactivex.rxjava2:rxjava:' + rootProject.ext.rxJavaVersion
compile 'io.reactivex.rxjava2:rxandroid:' + rootProject.ext.rxAndroidVersion

compile 'junit:junit:' + rootProject.ext.junitVersion
compile project(':moviper')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import rx.Scheduler;
import rx.android.plugins.RxAndroidPlugins;
import rx.android.plugins.RxAndroidSchedulersHook;
import rx.plugins.RxJavaPlugins;
import rx.plugins.RxJavaSchedulersHook;
import rx.schedulers.Schedulers;
import io.reactivex.android.plugins.RxAndroidPlugins;
import io.reactivex.schedulers.Schedulers;

/**
* This rule registers SchedulerHooks for RxJava and RxAndroid to ensure that subscriptions always
Expand All @@ -19,46 +15,15 @@
*/
public class RxAndroidSchedulersOverrideRule implements TestRule {

private final RxJavaSchedulersHook mRxJavaSchedulersHook = new RxJavaSchedulersHook() {
@Override
public Scheduler getIOScheduler() {
return Schedulers.immediate();
}

@Override
public Scheduler getNewThreadScheduler() {
return Schedulers.immediate();
}

@Override
public Scheduler getComputationScheduler() {
return Schedulers.immediate();
}
};

private final RxAndroidSchedulersHook mRxAndroidSchedulersHook = new RxAndroidSchedulersHook() {
@Override
public Scheduler getMainThreadScheduler() {
return Schedulers.immediate();
}
};

@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
overrideHooks();
RxAndroidPlugins.setInitMainThreadSchedulerHandler(__ -> Schedulers.trampoline());
base.evaluate();
}
};
}

private void overrideHooks() {
RxAndroidPlugins.getInstance().reset();
RxAndroidPlugins.getInstance().registerSchedulersHook(mRxAndroidSchedulersHook);

RxJavaPlugins.getInstance().reset();
RxJavaPlugins.getInstance().registerSchedulersHook(mRxJavaSchedulersHook);
}
}
2 changes: 1 addition & 1 deletion moviper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ dependencies {

// TODO: probably should split to two flavours to avoid
// including rxJava in projects that don't use rx
compile 'io.reactivex:rxjava:' + rootProject.ext.rxJavaVersion
compile 'io.reactivex.rxjava2:rxjava:' + rootProject.ext.rxJavaVersion

compile 'com.jakewharton:butterknife:' + rootProject.ext.butterKnifeVersion
apt 'com.jakewharton:butterknife-compiler:' + rootProject.ext.butterKnifeVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import com.mateuszkoslacz.moviper.iface.view.ViperView;
import com.mateuszkoslacz.moviper.presenterbus.Moviper;

import rx.Subscription;
import rx.subscriptions.CompositeSubscription;
import org.reactivestreams.Subscription;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
* Created by mateuszkoslacz on 08.08.2016.
Expand Down Expand Up @@ -49,15 +51,15 @@ public abstract class BaseRxPresenter
private InteractorType interactor;


private CompositeSubscription compositeSubscription;
private CompositeDisposable compositeSubscription;

public BaseRxPresenter() {
this(null);
}

public BaseRxPresenter(Bundle args) {
super(args);
this.compositeSubscription = new CompositeSubscription();
this.compositeSubscription = new CompositeDisposable();
this.routing = createRouting();
this.interactor = createInteractor();
}
Expand Down Expand Up @@ -91,7 +93,7 @@ public InteractorType getInteractor() {
return interactor;
}

protected void addSubscription(Subscription subscription) {
protected void addSubscription(Disposable subscription) {
if (compositeSubscription != null) compositeSubscription.add(subscription);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import rx.Observable;
import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject;
import io.reactivex.Maybe;
import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;

/**
* Created by mateuszkoslacz on 24.10.2016.
Expand Down Expand Up @@ -88,7 +89,7 @@ private void routeMoviperBundle(MoviperBundle bundle) {
public <PresenterType extends ViperPresenter> Observable<PresenterType> getPresenters(
final Class<PresenterType> presenterTypeClass) {
if (!mConfig.isPresenterAccessUtilEnabled()) throw new PresentersAccessUtilNotEnabled();
return Observable.from(mPresenters)
return Observable.fromIterable(mPresenters)
.filter(presenterTypeClass::isInstance)
.map(presenterTypeClass::cast)
.subscribeOn(Schedulers.computation()); // TODO: reconsider moving to computation scheduler
Expand All @@ -111,14 +112,14 @@ public <PresenterType extends ViperPresenter> Observable<PresenterType> getPrese
* returning proper name in {@link ViperPresenter#getName()}.
* @return {@link Observable} that emits (or not) Presenter instance of given name and class.
*/
public <PresenterType extends ViperPresenter> Observable<PresenterType> getPresenterInstance(
public <PresenterType extends ViperPresenter> Maybe<PresenterType> getPresenterInstance(
final Class<PresenterType> presenterTypeClass, String name) {
if (!mConfig.isInstancePresentersEnabled()) throw new PresenterInstancesAccessNotEnabled();
return Observable.from(mPresenters)
return Observable.fromIterable(mPresenters)
.filter(moviperPresenter -> moviperPresenter.getName().equals(name))
.filter(presenterTypeClass::isInstance)
.map(presenterTypeClass::cast)
.takeFirst(presenterType -> true)
.firstElement()
.subscribeOn(Schedulers.computation());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;

import rx.observers.TestSubscriber;
import io.reactivex.observers.TestSubscriber;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand Down
8 changes: 7 additions & 1 deletion sample-independent-viper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/rxjava.properties'
}
}

dependencies {
Expand All @@ -38,6 +41,9 @@ dependencies {

compile 'com.squareup.retrofit2:retrofit:' + rootProject.ext.retrofitVersion
compile 'com.squareup.retrofit2:converter-gson:' + rootProject.ext.retrofitVersion
compile 'com.squareup.retrofit2:adapter-rxjava:' + rootProject.ext.retrofitVersion
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
compile 'com.squareup.okhttp3:logging-interceptor:' + rootProject.ext.okHttpVersion
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile "com.github.akarnokd:rxjava2-interop:0.9.1"

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import retrofit2.http.GET;
import retrofit2.http.Path;
import rx.Observable;
import io.reactivex.Observable;

public interface GitHubApiInterface {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import java.util.List;

import rx.Observable;
import io.reactivex.Observable;

public interface IndependentContract {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.mateuszkoslacz.moviper.iface.interactor.ViperRxInteractor;
import com.mateuszkoslacz.moviper.iface.routing.ViperRxRouting;

import rx.Observable;
import io.reactivex.Observable;

public interface MainContract {

Expand All @@ -14,9 +14,9 @@ interface View extends MvpView {

void enableStop();

Observable<Void> onStartClicks();
Observable<Object> onStartClicks();

Observable<Void> onStopClicks();
Observable<Object> onStopClicks();
}

interface Interactor extends ViperRxInteractor {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mateuszkoslacz.sample.viper.interactor;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.mateuszkoslacz.moviper.base.interactor.BaseRxInteractor;
import com.mateuszkoslacz.sample.retrofit.GitHubApiInterface;
import com.mateuszkoslacz.sample.viper.contract.IndependentContract;
Expand All @@ -8,9 +9,8 @@
import java.util.List;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import io.reactivex.Observable;

public class IndependentInteractor
extends BaseRxInteractor
Expand All @@ -22,7 +22,7 @@ public IndependentInteractor() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(GitHubApiInterface.GITHUB_API_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
gitHubApiInterface = retrofit.create(GitHubApiInterface.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class IndependentPresenter
extends BaseRxPresenter
Expand All @@ -25,7 +25,7 @@ public class IndependentPresenter

public static final String UNIQUE_NAME = "INDEPENDENT_PRESENTER";

private Subscription subscription;
private Disposable subscription;

@Override
public String getName() {
Expand All @@ -50,7 +50,7 @@ public void attachView(ViperView view) {

subscription = getInteractor().getUserList()
.flatMap(users -> Observable.zip(
Observable.from(users),
Observable.fromIterable(users),
Observable.interval(2, TimeUnit.SECONDS),
(user, aLong) -> user
))
Expand All @@ -64,7 +64,7 @@ public void attachView(ViperView view) {

@Override
public void detachView(boolean retainInstance) {
if (subscription != null && !subscription.isUnsubscribed()) subscription.unsubscribe();
if (subscription != null && !subscription.isDisposed()) subscription.dispose();
super.detachView(retainInstance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import com.mateuszkoslacz.sample.viper.presenter.MainPresenter;

import butterknife.BindView;
import rx.Observable;
import hu.akarnokd.rxjava.interop.RxJavaInterop;
import io.reactivex.Observable;

public class MainActivity
extends ViperButterKnifePassiveActivity<MainContract.View>
Expand Down Expand Up @@ -46,12 +47,12 @@ public void enableStop() {
}

@Override
public Observable<Void> onStartClicks() {
return RxView.clicks(btnStart);
public Observable<Object> onStartClicks() {
return RxJavaInterop.toV2Observable(RxView.clicks(btnStart).map(aVoid -> new Object()));
}

@Override
public Observable<Void> onStopClicks() {
return RxView.clicks(btnStop);
public Observable<Object> onStopClicks() {
return RxJavaInterop.toV2Observable(RxView.clicks(btnStop).map(aVoid -> new Object()));
}
}
4 changes: 2 additions & 2 deletions sample-ipc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ dependencies {
compile 'com.jakewharton:butterknife:' + rootProject.ext.butterKnifeVersion
apt 'com.jakewharton:butterknife-compiler:' + rootProject.ext.butterKnifeVersion

compile 'io.reactivex:rxjava:' + rootProject.ext.rxJavaVersion
compile 'io.reactivex:rxandroid:' + rootProject.ext.rxAndroidVersion
compile 'io.reactivex.rxjava2:rxjava:' + rootProject.ext.rxJavaVersion
compile 'io.reactivex.rxjava2:rxandroid:' + rootProject.ext.rxAndroidVersion
compile project(path: ':moviper')


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.mateuszkoslacz.moviper.ipcsample.viper.routing.ColorWidgetRouting;
import com.mateuszkoslacz.moviper.presenterbus.Moviper;

import rx.android.schedulers.AndroidSchedulers;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class ColorWidgetPresenter
extends BaseRxPresenter<ColorWidgetContract.View,
Expand Down
6 changes: 3 additions & 3 deletions sample-recyclerview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ dependencies {

compile 'com.hannesdorfmann:adapterdelegates3:' + rootProject.ext.adapterdelegates3Version

compile 'io.reactivex:rxjava:' + rootProject.ext.rxJavaVersion
compile 'io.reactivex:rxandroid:' + rootProject.ext.rxAndroidVersion
compile 'io.reactivex.rxjava2:rxjava:' + rootProject.ext.rxJavaVersion
compile 'io.reactivex.rxjava2:rxandroid:' + rootProject.ext.rxAndroidVersion
compile 'com.squareup.retrofit2:retrofit:' + rootProject.ext.retrofitVersion
compile 'com.squareup.retrofit2:converter-gson:' + rootProject.ext.retrofitVersion
compile 'com.squareup.retrofit2:adapter-rxjava:' + rootProject.ext.retrofitVersion
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
compile 'com.squareup.okhttp3:logging-interceptor:' + rootProject.ext.okHttpVersion
compile project(path: ':moviper')

Expand Down
6 changes: 3 additions & 3 deletions sample-rx-ai/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ dependencies {
compile 'com.jakewharton:butterknife:' + rootProject.ext.butterKnifeVersion
apt 'com.jakewharton:butterknife-compiler:' + rootProject.ext.butterKnifeVersion

compile 'io.reactivex:rxjava:' + rootProject.ext.rxJavaVersion
compile 'io.reactivex:rxandroid:' + rootProject.ext.rxAndroidVersion
compile 'io.reactivex.rxjava2:rxjava:' + rootProject.ext.rxJavaVersion
compile 'io.reactivex.rxjava2:rxandroid:' + rootProject.ext.rxAndroidVersion
compile 'com.squareup.retrofit2:retrofit:' + rootProject.ext.retrofitVersion
compile 'com.squareup.retrofit2:converter-gson:' + rootProject.ext.retrofitVersion
compile 'com.squareup.retrofit2:adapter-rxjava:' + rootProject.ext.retrofitVersion
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
compile 'com.squareup.okhttp3:logging-interceptor:' + rootProject.ext.okHttpVersion
compile project(path: ':moviper')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import retrofit2.http.GET;
import retrofit2.http.Path;
import rx.Observable;
import io.reactivex.Observable;

/**
* Created by jjodelka on 17/10/16.
Expand Down
Loading

0 comments on commit b1ffffc

Please sign in to comment.