Search in sources :

Example 1 with Consumer

use of io.reactivex.functions.Consumer in project cw-omnibus by commonsguy.

the class MainActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final ViewPager pager = (ViewPager) findViewById(R.id.pager);
    final MaterialTabs tabs = (MaterialTabs) findViewById(R.id.tabs);
    observable = (Observable<PermissionRoster>) getLastNonConfigurationInstance();
    if (observable == null) {
        observable = Observable.create(new PermissionSource(this)).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).cache();
    }
    sub = observable.subscribe(new Consumer<PermissionRoster>() {

        @Override
        public void accept(PermissionRoster roster) throws Exception {
            pager.setAdapter(new PermissionTabAdapter(MainActivity.this, getFragmentManager(), roster));
            tabs.setViewPager(pager);
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable error) throws Exception {
            Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
            Log.e(getClass().getSimpleName(), "Exception processing request", error);
        }
    });
}
Also used : MaterialTabs(io.karim.MaterialTabs) Consumer(io.reactivex.functions.Consumer) ViewPager(android.support.v4.view.ViewPager)

Example 2 with Consumer

use of io.reactivex.functions.Consumer in project BigImageViewer by Piasy.

the class LongImageActivity method saveImage.

@SuppressWarnings("MissingPermission")
private void saveImage() {
    disposePermissionRequest();
    mPermissionRequest = new RxPermissions(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(new Consumer<Boolean>() {

        @Override
        public void accept(Boolean granted) throws Exception {
            if (granted) {
                mBigImageView.saveImageIntoGallery();
            }
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            throwable.printStackTrace();
        }
    });
}
Also used : RxPermissions(com.tbruyelle.rxpermissions2.RxPermissions) Consumer(io.reactivex.functions.Consumer)

Example 3 with Consumer

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

the class FlowableDoAfterNextTest method testIfFunctionThrowsThatNoMoreEventsAreProcessed.

@Test
public void testIfFunctionThrowsThatNoMoreEventsAreProcessed() {
    final AtomicInteger count = new AtomicInteger();
    final RuntimeException e = new RuntimeException();
    Burst.items(1, 2).create().doAfterNext(new Consumer<Integer>() {

        @Override
        public void accept(Integer t) throws Exception {
            count.incrementAndGet();
            throw e;
        }
    }).test().assertError(e).assertValue(1);
    assertEquals(1, count.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Consumer(io.reactivex.functions.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 4 with Consumer

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

the class ObservableSwitchIfEmptyTest method testSwitchWhenNotEmpty.

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

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

Example 5 with Consumer

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

the class DownloadType method startDownload.

public Observable<DownloadStatus> startDownload() {
    return Flowable.just(1).doOnSubscribe(new Consumer<Subscription>() {

        @Override
        public void accept(Subscription subscription) throws Exception {
            log(startLog());
            record.start();
        }
    }).flatMap(new Function<Integer, Publisher<DownloadStatus>>() {

        @Override
        public Publisher<DownloadStatus> apply(Integer integer) throws Exception {
            return download();
        }
    }).doOnNext(new Consumer<DownloadStatus>() {

        @Override
        public void accept(DownloadStatus status) throws Exception {
            record.update(status);
        }
    }).doOnError(new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            log(errorLog());
            record.error();
        }
    }).doOnComplete(new Action() {

        @Override
        public void run() throws Exception {
            log(completeLog());
            record.complete();
        }
    }).doOnCancel(new Action() {

        @Override
        public void run() throws Exception {
            log(cancelLog());
            record.cancel();
        }
    }).doFinally(new Action() {

        @Override
        public void run() throws Exception {
            log(finishLog());
            record.finish();
        }
    }).toObservable();
}
Also used : Action(io.reactivex.functions.Action) Consumer(io.reactivex.functions.Consumer) Publisher(org.reactivestreams.Publisher) Subscription(org.reactivestreams.Subscription) ParseException(java.text.ParseException) IOException(java.io.IOException)

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