Search in sources :

Example 36 with Action

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

the class MaybeFromActionTest method fromActionTwice.

@Test
public void fromActionTwice() {
    final AtomicInteger atomicInteger = new AtomicInteger();
    Action run = new Action() {

        @Override
        public void run() throws Exception {
            atomicInteger.incrementAndGet();
        }
    };
    Maybe.fromAction(run).test().assertResult();
    assertEquals(1, atomicInteger.get());
    Maybe.fromAction(run).test().assertResult();
    assertEquals(2, atomicInteger.get());
}
Also used : Action(io.reactivex.functions.Action) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 37 with Action

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

the class MaybeFromActionTest method noErrorLoss.

@Test
public void noErrorLoss() throws Exception {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        final CountDownLatch cdl1 = new CountDownLatch(1);
        final CountDownLatch cdl2 = new CountDownLatch(1);
        TestObserver<Object> to = Maybe.fromAction(new Action() {

            @Override
            public void run() throws Exception {
                cdl1.countDown();
                cdl2.await();
            }
        }).subscribeOn(Schedulers.single()).test();
        assertTrue(cdl1.await(5, TimeUnit.SECONDS));
        to.cancel();
        cdl2.countDown();
        int timeout = 10;
        while (timeout-- > 0 && errors.isEmpty()) {
            Thread.sleep(100);
        }
        TestHelper.assertUndeliverable(errors, 0, InterruptedException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : Action(io.reactivex.functions.Action) Test(org.junit.Test)

Example 38 with Action

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

the class MaybeFromActionTest method callable.

@SuppressWarnings("unchecked")
@Test
public void callable() throws Exception {
    final int[] counter = { 0 };
    Maybe<Void> m = Maybe.fromAction(new Action() {

        @Override
        public void run() throws Exception {
            counter[0]++;
        }
    });
    assertTrue(m.getClass().toString(), m instanceof Callable);
    assertNull(((Callable<Void>) m).call());
    assertEquals(1, counter[0]);
}
Also used : Action(io.reactivex.functions.Action) Test(org.junit.Test)

Example 39 with Action

use of io.reactivex.functions.Action in project RxFacebook by YouClap.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    callbackManager = CallbackManager.Factory.create();
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(final View view) {
            List<String> perm = new ArrayList<>();
            perm.add("email");
            perm.add("public_profile");
            RxFacebookLogin.logInWithReadPermissions(perm).subscribe(new Consumer<LoginResult>() {

                @Override
                public void accept(@NonNull LoginResult loginResult) throws Exception {
                    Log.d(LOG_TAG, "accept " + loginResult.getAccessToken());
                }
            }, new Consumer<Throwable>() {

                @Override
                public void accept(@NonNull Throwable throwable) throws Exception {
                    Log.e(LOG_TAG, "error ", throwable);
                }
            }, new Action() {

                @Override
                public void run() throws Exception {
                    Log.e(LOG_TAG, "onCompleted");
                }
            });
        }
    });
}
Also used : Action(io.reactivex.functions.Action) Consumer(io.reactivex.functions.Consumer) NonNull(io.reactivex.annotations.NonNull) LoginResult(com.facebook.login.LoginResult) FloatingActionButton(android.support.design.widget.FloatingActionButton) ArrayList(java.util.ArrayList) List(java.util.List) View(android.view.View) Toolbar(android.support.v7.widget.Toolbar)

Example 40 with Action

use of io.reactivex.functions.Action in project Mvp-Rxjava-Retrofit-dagger2 by pengMaster.

the class RecordSuperviseListPresenter method downLoadSuperviseRecordBaseInfo.

/**
 * 下载监督记录基本信息
 *
 * @param id
 */
public void downLoadSuperviseRecordBaseInfo(final String id) {
    Observable<SuperviseContentGsonBean> superviseContent = mModel.getSuperviseContentNet(id);
    Observable<superviseForm> recordBaseInfo = mModel.getRecordBaseInfo(id);
    Observable<LawDocumentGsonBean> lawFileNet = mModel.getLawFileNet(id);
    Observable.zip(recordBaseInfo, superviseContent, lawFileNet, new Function3<superviseForm, SuperviseContentGsonBean, LawDocumentGsonBean, RecordBaseGsonBean>() {

        @Override
        public RecordBaseGsonBean apply(@NonNull superviseForm superviseForm, @NonNull SuperviseContentGsonBean categoryResult, @NonNull LawDocumentGsonBean lawDocumentGsonBean) throws Exception {
            // 合并两次请求的结果
            RecordBaseGsonBean baseGsonBean = new RecordBaseGsonBean();
            baseGsonBean.setSuperviseForm(superviseForm);
            baseGsonBean.setCategoryResult(categoryResult);
            baseGsonBean.setLawDocumentGsonBean(lawDocumentGsonBean);
            baseGsonBean.setId(id);
            return baseGsonBean;
        }
    }).subscribeOn(Schedulers.io()).doOnSubscribe(new // 在执行任务前,做准备操作
    Consumer<Disposable>() {

        @Override
        public void accept(Disposable disposable) throws Exception {
            // 在执行任务之前 do some thing ...
            mRootView.showLoading();
        }
    }).observeOn(AndroidSchedulers.mainThread()).doFinally(new // 任务结束 do some thing ...
    Action() {

        @Override
        public void run() throws Exception {
            mRootView.hideLoading();
        }
    }).subscribe(new Consumer<RecordBaseGsonBean>() {

        @Override
        public void accept(@NonNull RecordBaseGsonBean recordBaseGsonBean) throws Exception {
            mRootView.hideLoading();
            mRootView.startActivity(recordBaseGsonBean);
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(@NonNull Throwable throwable) throws Exception {
            ToastUtils.showShort(throwable.getMessage());
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) Action(io.reactivex.functions.Action) Function3(io.reactivex.functions.Function3) SuperviseContentGsonBean(com.mtm.mrecord.mvp.model.entity.SuperviseContentGsonBean) com.mtm.mrecord.mvp.model.entity.superviseForm(com.mtm.mrecord.mvp.model.entity.superviseForm) Consumer(io.reactivex.functions.Consumer) NonNull(io.reactivex.annotations.NonNull) LawDocumentGsonBean(com.mtm.mrecord.mvp.model.entity.LawDocumentGsonBean) RecordBaseGsonBean(com.mtm.mrecord.mvp.model.entity.RecordBaseGsonBean)

Aggregations

Action (io.reactivex.functions.Action)45 Test (org.junit.Test)19 Consumer (io.reactivex.functions.Consumer)9 TestException (io.reactivex.exceptions.TestException)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Disposable (io.reactivex.disposables.Disposable)5 IOException (java.io.IOException)4 NonNull (android.support.annotation.NonNull)3 View (android.view.View)3 NonNull (io.reactivex.annotations.NonNull)3 TestScheduler (io.reactivex.schedulers.TestScheduler)3 Uri (android.net.Uri)2 TextView (android.widget.TextView)2 BindView (butterknife.BindView)2 ActionBarProvider (com.bluelinelabs.conductor.demo.ActionBarProvider)2 ObservableScoper (com.uber.autodispose.ObservableScoper)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ProgressDialog (android.app.ProgressDialog)1 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1