Search in sources :

Example 6 with Consumer

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

the class SingleMission method start.

@Override
public void start(final Semaphore semaphore) throws InterruptedException {
    if (isCanceled()) {
        return;
    }
    semaphore.acquire();
    if (isCanceled()) {
        semaphore.release();
        return;
    }
    disposable = rxdownload.download(bean).subscribeOn(Schedulers.io()).doOnSubscribe(new Consumer<Disposable>() {

        @Override
        public void accept(Disposable disposable) throws Exception {
            if (observer != null) {
                observer.onSubscribe(disposable);
            }
        }
    }).doFinally(new Action() {

        @Override
        public void run() throws Exception {
            log("finally and release...");
            semaphore.release();
        }
    }).subscribe(new Consumer<DownloadStatus>() {

        @Override
        public void accept(DownloadStatus value) throws Exception {
            status = value;
            processor.onNext(started(value));
            if (observer != null) {
                observer.onNext(value);
            }
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            processor.onNext(failed(status, throwable));
            if (observer != null) {
                observer.onError(throwable);
            }
        }
    }, new Action() {

        @Override
        public void run() throws Exception {
            processor.onNext(completed(status));
            setCompleted(true);
            if (observer != null) {
                observer.onComplete();
            }
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) Action(io.reactivex.functions.Action) Consumer(io.reactivex.functions.Consumer)

Example 7 with Consumer

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

the class MultiMissionDownloadActivity method onResume.

@Override
protected void onResume() {
    super.onResume();
    //
    startMultiMission();
    //
    disposable1 = rxDownload.receiveDownloadStatus(url1).subscribe(new Consumer<DownloadEvent>() {

        @Override
        public void accept(DownloadEvent downloadEvent) throws Exception {
            int flag = downloadEvent.getFlag();
            switch(flag) {
                case DownloadFlag.NORMAL:
                    binding.contentMultiMissionDownload.control1.setVisibility(View.GONE);
                    break;
                case DownloadFlag.WAITING:
                    binding.contentMultiMissionDownload.control1.setVisibility(View.VISIBLE);
                    binding.contentMultiMissionDownload.control1.setText("等待中");
                    break;
                case DownloadFlag.STARTED:
                    binding.contentMultiMissionDownload.control1.setText("下载中");
                    break;
                case DownloadFlag.PAUSED:
                    binding.contentMultiMissionDownload.control1.setText("已暂停");
                    break;
                case DownloadFlag.COMPLETED:
                    binding.contentMultiMissionDownload.control1.setText("已完成");
                    break;
                case DownloadFlag.FAILED:
                    Throwable throwable = downloadEvent.getError();
                    log(throwable);
                    binding.contentMultiMissionDownload.control1.setText("失败");
                    break;
            }
            DownloadStatus status = downloadEvent.getDownloadStatus();
            binding.contentMultiMissionDownload.progress1.setProgress(status.getPercentNumber());
        }
    });
    //
    disposable2 = rxDownload.receiveDownloadStatus(url2).subscribe(new Consumer<DownloadEvent>() {

        @Override
        public void accept(DownloadEvent downloadEvent) throws Exception {
            int flag = downloadEvent.getFlag();
            switch(flag) {
                case DownloadFlag.NORMAL:
                    binding.contentMultiMissionDownload.control2.setVisibility(View.GONE);
                    break;
                case DownloadFlag.WAITING:
                    binding.contentMultiMissionDownload.control2.setVisibility(View.VISIBLE);
                    binding.contentMultiMissionDownload.control2.setText("等待中");
                    break;
                case DownloadFlag.STARTED:
                    binding.contentMultiMissionDownload.control2.setText("下载中");
                    break;
                case DownloadFlag.PAUSED:
                    binding.contentMultiMissionDownload.control2.setText("已暂停");
                    break;
                case DownloadFlag.COMPLETED:
                    binding.contentMultiMissionDownload.control2.setText("已完成");
                    break;
                case DownloadFlag.FAILED:
                    Throwable throwable = downloadEvent.getError();
                    log(throwable);
                    binding.contentMultiMissionDownload.control2.setText("失败");
                    break;
            }
            DownloadStatus status = downloadEvent.getDownloadStatus();
            binding.contentMultiMissionDownload.progress2.setProgress(status.getPercentNumber());
        }
    });
    //
    disposable3 = rxDownload.receiveDownloadStatus(url3).subscribe(new Consumer<DownloadEvent>() {

        @Override
        public void accept(DownloadEvent downloadEvent) throws Exception {
            int flag = downloadEvent.getFlag();
            switch(flag) {
                case DownloadFlag.NORMAL:
                    binding.contentMultiMissionDownload.control3.setVisibility(View.GONE);
                    break;
                case DownloadFlag.WAITING:
                    binding.contentMultiMissionDownload.control3.setVisibility(View.VISIBLE);
                    binding.contentMultiMissionDownload.control3.setText("等待中");
                    break;
                case DownloadFlag.STARTED:
                    binding.contentMultiMissionDownload.control3.setText("下载中");
                    break;
                case DownloadFlag.PAUSED:
                    binding.contentMultiMissionDownload.control3.setText("已暂停");
                    break;
                case DownloadFlag.COMPLETED:
                    binding.contentMultiMissionDownload.control3.setText("已完成");
                    break;
                case DownloadFlag.FAILED:
                    Throwable throwable = downloadEvent.getError();
                    log(throwable);
                    binding.contentMultiMissionDownload.control3.setText("失败");
                    break;
            }
            DownloadStatus status = downloadEvent.getDownloadStatus();
            binding.contentMultiMissionDownload.progress3.setProgress(status.getPercentNumber());
        }
    });
}
Also used : Consumer(io.reactivex.functions.Consumer) DownloadEvent(zlc.season.rxdownload2.entity.DownloadEvent) DownloadStatus(zlc.season.rxdownload2.entity.DownloadStatus)

Example 8 with Consumer

use of io.reactivex.functions.Consumer in project RxJava by ReactiveX.

the class FlowableSwitchIfEmptyTest method testSwitchWhenNotEmpty.

@Test
public void testSwitchWhenNotEmpty() throws Exception {
    final AtomicBoolean subscribed = new AtomicBoolean(false);
    final Flowable<Integer> observable = Flowable.just(4).switchIfEmpty(Flowable.just(2).doOnSubscribe(new Consumer<Subscription>() {

        @Override
        public void accept(Subscription s) {
            subscribed.set(true);
        }
    }));
    assertEquals(4, observable.blockingSingle().intValue());
    assertFalse(subscribed.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(io.reactivex.functions.Consumer) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

Example 9 with Consumer

use of io.reactivex.functions.Consumer in project RxJava by ReactiveX.

the class ObservableAmbTest method testSubscriptionOnlyHappensOnce.

@SuppressWarnings("unchecked")
@Test
public void testSubscriptionOnlyHappensOnce() throws InterruptedException {
    final AtomicLong count = new AtomicLong();
    Consumer<Disposable> incrementer = new Consumer<Disposable>() {

        @Override
        public void accept(Disposable s) {
            count.incrementAndGet();
        }
    };
    //this aync stream should emit first
    Observable<Integer> o1 = Observable.just(1).doOnSubscribe(incrementer).delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
    //this stream emits second
    Observable<Integer> o2 = Observable.just(1).doOnSubscribe(incrementer).delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
    TestObserver<Integer> ts = new TestObserver<Integer>();
    Observable.ambArray(o1, o2).subscribe(ts);
    ts.awaitTerminalEvent(5, TimeUnit.SECONDS);
    ts.assertNoErrors();
    assertEquals(2, count.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Consumer(io.reactivex.functions.Consumer) TestObserver(io.reactivex.observers.TestObserver)

Example 10 with Consumer

use of io.reactivex.functions.Consumer 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)

Aggregations

Consumer (io.reactivex.functions.Consumer)12 Action (io.reactivex.functions.Action)3 Test (org.junit.Test)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 FloatingActionButton (android.support.design.widget.FloatingActionButton)1 ViewPager (android.support.v4.view.ViewPager)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 Toolbar (android.support.v7.widget.Toolbar)1 View (android.view.View)1 LoginResult (com.facebook.login.LoginResult)1 RxPermissions (com.tbruyelle.rxpermissions2.RxPermissions)1 MaterialTabs (io.karim.MaterialTabs)1 Observable (io.reactivex.Observable)1 ObservableEmitter (io.reactivex.ObservableEmitter)1 ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)1 NonNull (io.reactivex.annotations.NonNull)1 Disposable (io.reactivex.disposables.Disposable)1 Function (io.reactivex.functions.Function)1 BooleanSubscription (io.reactivex.internal.subscriptions.BooleanSubscription)1