Search in sources :

Example 56 with Function

use of io.reactivex.functions.Function in project requery by requery.

the class PeopleActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle("People");
    }
    setContentView(R.layout.activity_people);
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    data = ((PeopleApplication) getApplication()).getData();
    executor = Executors.newSingleThreadExecutor();
    adapter = new PersonAdapter();
    adapter.setExecutor(executor);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    data.count(Person.class).get().single().subscribe(new Consumer<Integer>() {

        @Override
        public void accept(Integer integer) {
            if (integer == 0) {
                Observable.fromCallable(new CreatePeople(data)).flatMap(new Function<Observable<Iterable<Person>>, Observable<?>>() {

                    @Override
                    public Observable<?> apply(Observable<Iterable<Person>> o) {
                        return o;
                    }
                }).observeOn(Schedulers.computation()).subscribe(new Consumer<Object>() {

                    @Override
                    public void accept(Object o) {
                        adapter.queryAsync();
                    }
                });
            }
        }
    });
}
Also used : LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) Observable(io.reactivex.Observable) Function(io.reactivex.functions.Function) Consumer(io.reactivex.functions.Consumer) RecyclerView(android.support.v7.widget.RecyclerView) Person(io.requery.android.example.app.model.Person)

Example 57 with Function

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

the class PublishRelayTest method testNestedSubscribe.

@Test
public void testNestedSubscribe() {
    final PublishRelay<Integer> s = PublishRelay.create();
    final AtomicInteger countParent = new AtomicInteger();
    final AtomicInteger countChildren = new AtomicInteger();
    final AtomicInteger countTotal = new AtomicInteger();
    final ArrayList<String> list = new ArrayList<String>();
    s.flatMap(new Function<Integer, Observable<String>>() {

        @Override
        public Observable<String> apply(final Integer v) {
            countParent.incrementAndGet();
            // then subscribe to subject again (it will not receive the previous value)
            return s.map(new Function<Integer, String>() {

                @Override
                public String apply(Integer v2) {
                    countChildren.incrementAndGet();
                    return "Parent: " + v + " Child: " + v2;
                }
            });
        }
    }).subscribe(new Consumer<String>() {

        @Override
        public void accept(String v) {
            countTotal.incrementAndGet();
            list.add(v);
        }
    });
    for (int i = 0; i < 10; i++) {
        s.accept(i);
    }
    //            System.out.println("countParent: " + countParent.get());
    //            System.out.println("countChildren: " + countChildren.get());
    //            System.out.println("countTotal: " + countTotal.get());
    // 9+8+7+6+5+4+3+2+1+0 == 45
    assertEquals(45, list.size());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Function(io.reactivex.functions.Function) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 58 with Function

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

the class PublishRelayTest method testUnsubscriptionCase.

@Test(timeout = 1000)
public void testUnsubscriptionCase() {
    PublishRelay<String> src = PublishRelay.create();
    for (int i = 0; i < 10; i++) {
        final Observer<Object> o = TestHelper.mockObserver();
        InOrder inOrder = inOrder(o);
        String v = "" + i;
        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();
            }
        });
        src.accept(v);
        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 59 with Function

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

the class BuildsDetailsPresenter method startLoadingData.

/**
     * Starts loading data
     *
     * @param intentUrl Intent URL
     * @param repoSlug  Repository slug
     * @param buildId   Build ID
     */
public void startLoadingData(String intentUrl, String repoSlug, long buildId) {
    mRepoSlug = repoSlug;
    mBuildId = buildId;
    Single<BuildDetails> buildDetailsSingle;
    if (!TextUtils.isEmpty(intentUrl)) {
        buildDetailsSingle = mRawClient.singleRequest(intentUrl).doOnSuccess(response -> {
            String redirectUrl = intentUrl;
            if (response.isRedirect()) {
                redirectUrl = response.header("Location", "");
            }
            parseIntentUrl(redirectUrl);
        }).flatMap(new Function<okhttp3.Response, SingleSource<BuildDetails>>() {

            @Override
            public SingleSource<BuildDetails> apply(@NonNull okhttp3.Response response) throws Exception {
                return mTravisRestClient.getApiService().getBuild(mRepoSlug, mBuildId);
            }
        });
    } else {
        buildDetailsSingle = mTravisRestClient.getApiService().getBuild(mRepoSlug, mBuildId);
    }
    Disposable subscription = buildDetailsSingle.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe((buildDetails, throwable) -> {
        if (throwable == null) {
            handleBuildDetails(buildDetails);
        } else {
            handleLoadingFailed(throwable);
        }
    });
    mSubscriptions.add(subscription);
    getView().showProgress();
}
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)

Example 60 with Function

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

the class BuildsDetailsPresenter method cancelBuild.

/**
     * Cancels build process
     */
public void cancelBuild() {
    RequestBody emptyBody = RequestBody.create(MediaType.parse("application/json"), "");
    Disposable subscription = mTravisRestClient.getApiService().cancelBuild(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)

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