Search in sources :

Example 26 with Function

use of io.reactivex.functions.Function in project RxDownload by ssseasonnn.

the class DownloadManagerActivity method loadData.

private void loadData() {
    RxDownload.getInstance(this).getTotalDownloadRecords().map(new Function<List<DownloadRecord>, List<DownloadItem>>() {

        @Override
        public List<DownloadItem> apply(List<DownloadRecord> downloadRecords) throws Exception {
            List<DownloadItem> result = new ArrayList<>();
            for (DownloadRecord each : downloadRecords) {
                DownloadItem bean = new DownloadItem();
                bean.record = each;
                result.add(bean);
            }
            return result;
        }
    }).subscribe(new Consumer<List<DownloadItem>>() {

        @Override
        public void accept(List<DownloadItem> downloadBeen) throws Exception {
            mAdapter.addAll(downloadBeen);
        }
    });
}
Also used : Function(io.reactivex.functions.Function) ArrayList(java.util.ArrayList) DownloadItem(zlc.season.rxdownloadproject.model.DownloadItem) ArrayList(java.util.ArrayList) List(java.util.List) DownloadRecord(zlc.season.rxdownload2.entity.DownloadRecord)

Example 27 with Function

use of io.reactivex.functions.Function in project RxRelay by JakeWharton.

the class ReplayRelayTest method testUnsubscriptionCase.

@Test(timeout = 1000)
public void testUnsubscriptionCase() {
    ReplayRelay<String> src = ReplayRelay.create();
    for (int i = 0; i < 10; i++) {
        final Observer<Object> o = TestHelper.mockObserver();
        InOrder inOrder = inOrder(o);
        String v = "" + i;
        src.accept(v);
        System.out.printf("Turn: %d%n", i);
        src.firstElement().toObservable().flatMap(new Function<String, Observable<String>>() {

            @Override
            public Observable<String> apply(String t1) {
                return Observable.just(t1 + ", " + t1);
            }
        }).subscribe(new DefaultObserver<String>() {

            @Override
            public void onNext(String t) {
                System.out.println(t);
                o.onNext(t);
            }

            @Override
            public void onError(Throwable e) {
                o.onError(e);
            }

            @Override
            public void onComplete() {
                o.onComplete();
            }
        });
        inOrder.verify(o).onNext("0, 0");
        inOrder.verify(o).onComplete();
        verify(o, never()).onError(any(Throwable.class));
    }
}
Also used : Function(io.reactivex.functions.Function) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 28 with Function

use of io.reactivex.functions.Function in project RxRelay by JakeWharton.

the class BehaviorRelayTest method testUnsubscriptionCase.

@Test(timeout = 1000)
public void testUnsubscriptionCase() {
    // FIXME was plain null which is not allowed
    BehaviorRelay<String> src = BehaviorRelay.createDefault("null");
    for (int i = 0; i < 10; i++) {
        final Observer<Object> o = TestHelper.mockObserver();
        InOrder inOrder = inOrder(o);
        String v = "" + i;
        src.accept(v);
        System.out.printf("Turn: %d%n", i);
        src.firstElement().toObservable().flatMap(new Function<String, Observable<String>>() {

            @Override
            public Observable<String> apply(String t1) {
                return Observable.just(t1 + ", " + t1);
            }
        }).subscribe(new DefaultObserver<String>() {

            @Override
            public void onNext(String t) {
                o.onNext(t);
            }

            @Override
            public void onError(Throwable e) {
                o.onError(e);
            }

            @Override
            public void onComplete() {
                o.onComplete();
            }
        });
        inOrder.verify(o).onNext(v + ", " + v);
        inOrder.verify(o).onComplete();
        verify(o, never()).onError(any(Throwable.class));
    }
}
Also used : Function(io.reactivex.functions.Function) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 29 with Function

use of io.reactivex.functions.Function in project Varis-Android by dkhmelenko.

the class BuildsDetailsPresenter method restartBuild.

/**
     * Restarts build process
     */
public void restartBuild() {
    RequestBody emptyBody = RequestBody.create(MediaType.parse("application/json"), "");
    Disposable subscription = mTravisRestClient.getApiService().restartBuild(mBuildId, emptyBody).onErrorReturn(throwable -> new Object()).flatMap(new Function<Object, SingleSource<BuildDetails>>() {

        @Override
        public SingleSource<BuildDetails> apply(@NonNull Object o) throws Exception {
            return mTravisRestClient.getApiService().getBuild(mRepoSlug, mBuildId);
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe((buildDetails, throwable) -> {
        if (throwable == null) {
            handleBuildDetails(buildDetails);
        } else {
            handleLoadingFailed(throwable);
        }
    });
    mSubscriptions.add(subscription);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Function(io.reactivex.functions.Function) BuildDetails(com.khmelenko.lab.varis.network.response.BuildDetails) NonNull(io.reactivex.annotations.NonNull) RequestBody(okhttp3.RequestBody)

Example 30 with Function

use of io.reactivex.functions.Function in project Varis-Android by dkhmelenko.

the class BuildsDetailsPresenter method startLoadingLog.

/**
     * Starts loading log file
     *
     * @param jobId Job ID
     */
public void startLoadingLog(long jobId) {
    mJobId = jobId;
    String accessToken = AppSettings.getAccessToken();
    Single<String> responseSingle;
    if (TextUtils.isEmpty(accessToken)) {
        responseSingle = mRawClient.getApiService().getLog(String.valueOf(mJobId));
    } else {
        String auth = String.format("token %1$s", AppSettings.getAccessToken());
        responseSingle = mRawClient.getApiService().getLog(auth, String.valueOf(mJobId));
    }
    Disposable subscription = responseSingle.subscribeOn(Schedulers.io()).map(s -> mRawClient.getLogUrl(mJobId)).onErrorResumeNext(new Function<Throwable, SingleSource<String>>() {

        @Override
        public SingleSource<String> apply(@NonNull Throwable throwable) throws Exception {
            String redirectUrl = "";
            HttpException httpException = (HttpException) throwable;
            Headers headers = httpException.response().headers();
            for (String header : headers.names()) {
                if (header.equals("Location")) {
                    redirectUrl = headers.get(header);
                    break;
                }
            }
            return Single.just(redirectUrl);
        }
    }).retry(LOAD_LOG_MAX_ATTEMPT).observeOn(AndroidSchedulers.mainThread()).subscribe((logUrl, throwable) -> {
        if (throwable == null) {
            getView().setLogUrl(logUrl);
        } else {
            getView().showLogError();
            getView().showLoadingError(throwable.getMessage());
        }
    });
    mSubscriptions.add(subscription);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Function(io.reactivex.functions.Function) Headers(okhttp3.Headers) NonNull(io.reactivex.annotations.NonNull) HttpException(retrofit2.HttpException)

Aggregations

Function (io.reactivex.functions.Function)60 Test (org.junit.Test)27 TestException (io.reactivex.exceptions.TestException)24 InOrder (org.mockito.InOrder)21 Observable (io.reactivex.Observable)10 Disposable (io.reactivex.disposables.Disposable)7 NonNull (io.reactivex.annotations.NonNull)5 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)5 BuildDetails (com.khmelenko.lab.varis.network.response.BuildDetails)3 BooleanSubscription (io.reactivex.internal.subscriptions.BooleanSubscription)3 Person (io.requery.test.model.Person)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Phone (io.requery.test.model.Phone)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 RequestBody (okhttp3.RequestBody)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Subscription (org.reactivestreams.Subscription)2