pl.rspective.voucherify.client.utils.RxUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of voucherify-java-sdk Show documentation
Show all versions of voucherify-java-sdk Show documentation
Voucherify-java-sdk is a Java client (can be used in Android application as well) which was created to simplify integration with Voucherify backend (http://www.voucherify.io)
package pl.rspective.voucherify.client.utils;
import pl.rspective.voucherify.client.callback.VoucherifyCallback;
import java.util.concurrent.Executor;
import retrofit.RetrofitError;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.schedulers.Schedulers;
/**
* Utils's class used to wrap sync and async calls into RX world
*/
public final class RxUtils {
/**
*
* @param executor
* @param observable
* @param callback
* @param
* @return
*/
public static VoucherifyCallback subscribe(final Executor executor, Observable observable, final VoucherifyCallback callback) {
observable
.subscribe(
new Action1() {
@Override
public void call(final R r) {
executor.execute(new Runnable() {
@Override
public void run() {
callback.onSuccess(r);
}
});
}
},
new Action1() {
@Override
public void call(final Throwable throwable) {
executor.execute(new Runnable() {
@Override
public void run() {
callback.onFailure(RetrofitError.unexpectedError("", throwable));
}
});
}
});
return callback;
}
/**
* Convert method into observable
* @param represents return type of the wrapped method
*/
public abstract static class DefFunc implements Func0> {
@Override
public final Observable call() {
return Observable.just(method());
}
public abstract T method();
}
/**
* Requests are manged in io thread by default
* @param func
* @param
* @return
*/
public static Observable defer(DefFunc func) {
return Observable.defer(func).subscribeOn(Schedulers.io());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy