Skip to content

Commit

Permalink
updapte rxjava to v2.0.0 (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauin authored Nov 5, 2016
1 parent 90de7cc commit 1b36c17
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 85 deletions.
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ext.versions = [

// Dependency Versions
playServices : '9.8.0',
rxJava : '1.2.0'
rxJava : '2.0.0'
]

ext.gradlePlugins = [
Expand Down
34 changes: 17 additions & 17 deletions sample/src/main/java/com/mtramin/servant_sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
import com.mtramin.servant.Servant;
import com.mtramin.servant_sampler.R;

import rx.Subscription;
import io.reactivex.disposables.Disposable;

public class MainActivity extends AppCompatActivity {

private Subscription observableClientSubscription;
private Subscription singleClientSubscription;
private Subscription completableClientSubscription;
private Subscription singleClintWithOptionsSubscription;
private Disposable observableClientDisposable;
private Disposable singleClientDisposable;
private Disposable completableClientDisposable;
private Disposable singleClintWithOptionsDisposable;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -50,18 +50,18 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
protected void onStart() {
super.onStart();
observableClientSubscription = serveObservableClient();
singleClientSubscription = serveSingleClient();
singleClintWithOptionsSubscription = serveSingleClientWithOptions();
completableClientSubscription = serveCompletableClient();
observableClientDisposable = serveObservableClient();
singleClientDisposable = serveSingleClient();
singleClintWithOptionsDisposable = serveSingleClientWithOptions();
completableClientDisposable = serveCompletableClient();
}

@Override
protected void onStop() {
observableClientSubscription.unsubscribe();
singleClientSubscription.unsubscribe();
singleClintWithOptionsSubscription.unsubscribe();
completableClientSubscription.unsubscribe();
observableClientDisposable.dispose();
singleClientDisposable.dispose();
singleClintWithOptionsDisposable.dispose();
completableClientDisposable.dispose();
super.onStop();
}

Expand All @@ -72,15 +72,15 @@ private void serveActionClient() {
);
}

private Subscription serveObservableClient() {
private Disposable serveObservableClient() {
return Servant.observable(this, LocationServices.API)
.subscribe(
googleApiClient -> Log.e("Servant", "have observable client"),
throwable -> Log.e("Servant", "error in observable client", throwable)
);
}

private Subscription serveSingleClient() {
private Disposable serveSingleClient() {
return Servant.single(new GoogleApiClientSingle<Boolean>(this, LocationServices.API) {
@Override
protected void onSingleClientConnected(GoogleApiClient googleApiClient) {
Expand All @@ -95,7 +95,7 @@ protected void onSingleClientConnected(GoogleApiClient googleApiClient) {
);
}

private Subscription serveSingleClientWithOptions() {
private Disposable serveSingleClientWithOptions() {
GoogleSignInOptions googleSignInOptions = new GoogleSignInOptions.Builder(
GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
Expand All @@ -120,7 +120,7 @@ protected void onSingleClientConnected(GoogleApiClient googleApiClient) {
);
}

private Subscription serveCompletableClient() {
private Disposable serveCompletableClient() {
return Servant.completable(new GoogleApiClientCompletable(this, LocationServices.API) {
@Override
protected void onCompletableClientConnected(GoogleApiClient googleApiClient) {
Expand Down
2 changes: 1 addition & 1 deletion servant/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ android {

dependencies {
compile "com.google.android.gms:play-services-base:$versions.playServices"
compile "io.reactivex:rxjava:$versions.rxJava"
compile "io.reactivex.rxjava2:rxjava:$versions.rxJava"
}

//apply from: '../publish.gradle'
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import com.google.android.gms.common.api.GoogleApiClient;

import rx.functions.Action1;
import io.reactivex.functions.Consumer;

/**
* Implementation serving a {@link GoogleApiClient} that is usable with Actions.
Expand All @@ -30,12 +30,12 @@
*/
class GoogleApiClientActions extends BaseClient {

private Action1<GoogleApiClient> onClientConnected;
private Action1<Throwable> onError;
private Consumer<GoogleApiClient> onClientConnected;
private Consumer<Throwable> onError;

private GoogleApiClientActions(Context context,
Action1<GoogleApiClient> onClientConnected,
Action1<Throwable> onError,
Consumer<GoogleApiClient> onClientConnected,
Consumer<Throwable> onError,
GoogleApi googleApi) {
super(context);
this.onClientConnected = onClientConnected;
Expand All @@ -47,19 +47,27 @@ private GoogleApiClientActions(Context context,

static void create(Context context,
GoogleApi googleApi,
Action1<GoogleApiClient> onClientConnected,
Action1<Throwable> onError) {
Consumer<GoogleApiClient> onClientConnected,
Consumer<Throwable> onError) {
new GoogleApiClientActions(context, onClientConnected, onError, googleApi);
}

@Override
void onClientConnected(GoogleApiClient googleApiClient) {
onClientConnected.call(googleApiClient);
try {
onClientConnected.accept(googleApiClient);
} catch (Exception e) {
onClientError(e);
}
disconnect();
}

@Override
void onClientError(Throwable throwable) {
onError.call(throwable);
try {
onError.accept(throwable);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,20 @@
import com.google.android.gms.common.api.Api.ApiOptions.HasOptions;
import com.google.android.gms.common.api.GoogleApiClient;

import rx.Completable;
import rx.CompletableSubscriber;
import rx.subscriptions.Subscriptions;
import io.reactivex.CompletableEmitter;
import io.reactivex.CompletableOnSubscribe;

/**
* Provides an interface that serves a {@link GoogleApiClient} as a {@link Completable}.
* Provides an interface that serves a {@link GoogleApiClient} as a {@link io.reactivex.Completable}.
* <p>
* Will disconnect the Client once the Completable is unsubscribed from. This is also the case once
* the Completable calls {@link CompletableSubscriber#onCompleted()}.
* Will disconnect the Client once the Completable is disposed of. This is also the case once
* the Completable calls {@link CompletableEmitter#onComplete()}.
*/
public abstract class GoogleApiClientCompletable extends BaseClient
implements Completable.OnSubscribe {
implements CompletableOnSubscribe {

private final GoogleApi googleApi;
private CompletableSubscriber completableSubscriber;
private CompletableEmitter completableEmitter;

protected GoogleApiClientCompletable(Context context, Api api) {
super(context);
Expand All @@ -49,13 +48,13 @@ protected GoogleApiClientCompletable(Context context, Api api, HasOptions option
}

@Override
public void call(CompletableSubscriber completableSubscriber) {
this.completableSubscriber = completableSubscriber;
public void subscribe(CompletableEmitter emitter) throws Exception {
this.completableEmitter = emitter;

buildClient(googleApi);
connect();

completableSubscriber.onSubscribe(Subscriptions.create(this::disconnect));
emitter.setCancellable(this::disconnect);
}

/**
Expand All @@ -69,7 +68,7 @@ public void call(CompletableSubscriber completableSubscriber) {
* Call when the Completable GoogleApiClient should complete.
*/
protected void onCompleted() {
completableSubscriber.onCompleted();
completableEmitter.onComplete();
}

/**
Expand All @@ -78,7 +77,7 @@ protected void onCompleted() {
* @param throwable throwable to emit in onError.
*/
protected void onError(Throwable throwable) {
completableSubscriber.onError(throwable);
completableEmitter.onError(throwable);
}

@Override
Expand All @@ -88,6 +87,6 @@ void onClientConnected(GoogleApiClient googleApiClient) {

@Override
void onClientError(Throwable throwable) {
completableSubscriber.onError(throwable);
completableEmitter.onError(throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,40 @@

import com.google.android.gms.common.api.GoogleApiClient;

import rx.AsyncEmitter;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.subscriptions.Subscriptions;
import io.reactivex.Emitter;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

/**
* Serves a {@link GoogleApiClient} as an Observable that will emit the Client in
* {@link Subscriber#onNext(Object)} when it is ready to be used.
* {@link Emitter#onNext(Object)} when it is ready to be used.
* <p>
* The client will be disconnected once the returned {@link Observable} is unsubscribed from.
* The client will be disconnected once the returned {@link Observable} is disposed of.
*/
class GoogleApiClientObservable extends BaseClient
implements Action1<AsyncEmitter<GoogleApiClient>> {
implements ObservableOnSubscribe<GoogleApiClient> {

private final GoogleApi googleApi;
private AsyncEmitter<GoogleApiClient> emitter;
private ObservableEmitter<GoogleApiClient> emitter;

private GoogleApiClientObservable(Context context, GoogleApi googleApi) {
super(context);
this.googleApi = googleApi;
}

static Observable<GoogleApiClient> create(Context context, GoogleApi googleApi) {
return Observable.fromEmitter(new GoogleApiClientObservable(context, googleApi),
AsyncEmitter.BackpressureMode.NONE);
return Observable.create(new GoogleApiClientObservable(context, googleApi));
}

@Override
public void call(AsyncEmitter<GoogleApiClient> emitter) {
public void subscribe(ObservableEmitter<GoogleApiClient> emitter) throws Exception {
this.emitter = emitter;

buildClient(googleApi);
connect();

emitter.setSubscription(Subscriptions.create(this::disconnect));
emitter.setCancellable(this::disconnect);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Result;

import rx.SingleSubscriber;

/**
* Provides interface that can be extended to use a {@link GoogleApiClient} as a {@link rx.Single} that
* Provides interface that can be extended to use a {@link GoogleApiClient} as a {@link io.reactivex.Single} that
* automatically performs the given request.
* <p>
* Will disconnect the client once it has emitted it's value in {@link SingleSubscriber#onSuccess(Object)}
* Will disconnect the client once it has emitted it's value in {@link io.reactivex.SingleEmitter#onSuccess(Object)}
* or once the recipient disconnects from the Single.
*/
public abstract class GoogleApiClientRequestSingle<T, R extends Result>
Expand Down Expand Up @@ -62,7 +60,7 @@ protected void onSingleClientConnected(GoogleApiClient googleApiClient) {
/**
* Unwrap the result of the request that was sent to the {@link GoogleApiClient}. The given result will
* already be checked that it was successful, otherwise a {@link ClientException} will be thrown in
* {@link SingleSubscriber#onError(Throwable)}.
* {@link io.reactivex.SingleEmitter#onError(Throwable)}.
*
* @param result result that was received
* @return result you want to return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,19 @@
import com.google.android.gms.common.api.Api;
import com.google.android.gms.common.api.GoogleApiClient;

import rx.Single;
import rx.SingleSubscriber;
import rx.subscriptions.Subscriptions;
import io.reactivex.SingleEmitter;
import io.reactivex.SingleOnSubscribe;

/**
* Provides an interface that serves a {@link GoogleApiClient} as a {@link Single}.
* Provides an interface that serves a {@link GoogleApiClient} as a {@link io.reactivex.Single}.
* <p>
* Will disconnect the Client once the Single is unsubscribed from. This is also the case if the
* Single emits anything in {@link SingleSubscriber#onSuccess(Object)}.
* Will disconnect the Client once the Single is disposed from. This is also the case if the
* Single emits anything in {@link SingleEmitter#onSuccess(Object)}.
*/
public abstract class GoogleApiClientSingle<T> extends BaseClient implements Single.OnSubscribe<T> {
public abstract class GoogleApiClientSingle<T> extends BaseClient implements SingleOnSubscribe<T> {

private final GoogleApi googleApi;
private SingleSubscriber<? super T> singleSubscriber;
private SingleEmitter<? super T> singleEmitter;

protected GoogleApiClientSingle(Context context, Api api) {
super(context);
Expand All @@ -47,13 +46,13 @@ protected GoogleApiClientSingle(Context context, Api api, Api.ApiOptions.HasOpti
}

@Override
public void call(SingleSubscriber<? super T> singleSubscriber) {
this.singleSubscriber = singleSubscriber;
public void subscribe(SingleEmitter<T> emitter) throws Exception {
this.singleEmitter = emitter;

buildClient(googleApi);
connect();

singleSubscriber.add(Subscriptions.create(this::disconnect));
emitter.setCancellable(this::disconnect);
}

/**
Expand All @@ -65,14 +64,14 @@ public void call(SingleSubscriber<? super T> singleSubscriber) {
protected abstract void onSingleClientConnected(GoogleApiClient googleApiClient);

protected void onSuccess(T result) {
if (!singleSubscriber.isUnsubscribed()) {
singleSubscriber.onSuccess(result);
if (!singleEmitter.isDisposed()) {
singleEmitter.onSuccess(result);
}
}

protected void onError(Throwable throwable) {
if (!singleSubscriber.isUnsubscribed()) {
singleSubscriber.onError(throwable);
if (!singleEmitter.isDisposed()) {
singleEmitter.onError(throwable);
}
}

Expand All @@ -83,6 +82,6 @@ void onClientConnected(GoogleApiClient googleApiClient) {

@Override
void onClientError(Throwable throwable) {
singleSubscriber.onError(throwable);
singleEmitter.onError(throwable);
}
}
Loading

0 comments on commit 1b36c17

Please sign in to comment.