Search in sources :

Example 66 with Subscription

use of rx.Subscription in project okhttp-OkGo by jeasonlzy.

the class RxCommonActivity method retrofitRequest.

@OnClick(R.id.retrofitRequest)
public void retrofitRequest(View view) {
    Subscription subscription = //
    ServerApi.getServerModel("aaa", "bbb").doOnSubscribe(new Action0() {

        @Override
        public void call() {
            //开始请求前显示对话框
            showLoading();
        }
    }).map(new Func1<LzyResponse<ServerModel>, ServerModel>() {

        @Override
        public ServerModel call(LzyResponse<ServerModel> response) {
            return response.data;
        }
    }).observeOn(//切换到主线程
    AndroidSchedulers.mainThread()).subscribe(new Action1<ServerModel>() {

        @Override
        public void call(ServerModel serverModel) {
            //请求成功
            dismissLoading();
            handleResponse(serverModel, null, null);
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            //请求失败
            throwable.printStackTrace();
            showToast("请求失败");
            dismissLoading();
            handleError(null, null);
        }
    });
    addSubscribe(subscription);
}
Also used : Action0(rx.functions.Action0) LzyResponse(com.lzy.demo.model.LzyResponse) ServerModel(com.lzy.demo.model.ServerModel) Subscription(rx.Subscription) OnClick(butterknife.OnClick)

Example 67 with Subscription

use of rx.Subscription in project okhttp-OkGo by jeasonlzy.

the class RxCommonActivity method commonRequest.

@OnClick(R.id.commonRequest)
public void commonRequest(View view) {
    Subscription subscription = //
    OkGo.post(Urls.URL_METHOD).headers("aaa", //
    "111").params("bbb", //
    "222").getCall(StringConvert.create(), //以上为产生请求事件,请求默认发生在IO线程
    RxAdapter.<String>create()).doOnSubscribe(new Action0() {

        @Override
        public void call() {
            //开始请求前显示对话框
            showLoading();
        }
    }).observeOn(//切换到主线程
    AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {

        @Override
        public void call(String s) {
            //请求成功,关闭对话框
            dismissLoading();
            handleResponse(s, null, null);
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            throwable.printStackTrace();
            //请求失败
            dismissLoading();
            showToast("请求失败");
            handleError(null, null);
        }
    });
    addSubscribe(subscription);
}
Also used : Action0(rx.functions.Action0) Subscription(rx.Subscription) OnClick(butterknife.OnClick)

Example 68 with Subscription

use of rx.Subscription in project nucleus by konmik.

the class DeliverReplay method call.

@Override
public Observable<Delivery<View, T>> call(Observable<T> observable) {
    final ReplaySubject<Notification<T>> subject = ReplaySubject.create();
    final Subscription subscription = observable.materialize().filter(new Func1<Notification<T>, Boolean>() {

        @Override
        public Boolean call(Notification<T> notification) {
            return !notification.isOnCompleted();
        }
    }).subscribe(subject);
    return view.switchMap(new Func1<View, Observable<Delivery<View, T>>>() {

        @Override
        public Observable<Delivery<View, T>> call(final View view) {
            return view == null ? Observable.<Delivery<View, T>>never() : subject.map(new Func1<Notification<T>, Delivery<View, T>>() {

                @Override
                public Delivery<View, T> call(Notification<T> notification) {
                    return new Delivery<>(view, notification);
                }
            });
        }
    }).doOnUnsubscribe(new Action0() {

        @Override
        public void call() {
            subscription.unsubscribe();
        }
    });
}
Also used : Action0(rx.functions.Action0) Subscription(rx.Subscription) Func1(rx.functions.Func1) Notification(rx.Notification)

Example 69 with Subscription

use of rx.Subscription in project nucleus by konmik.

the class RxPresenterTest method testStartedRestartableIsNotUnsubscribed.

@Test
public void testStartedRestartableIsNotUnsubscribed() throws Exception {
    RxPresenter presenter = new RxPresenter();
    presenter.create(null);
    Func0<Subscription> restartable = mock(Func0.class);
    Subscription subscription = mock(Subscription.class);
    when(restartable.call()).thenReturn(subscription);
    when(subscription.isUnsubscribed()).thenReturn(false);
    presenter.restartable(1, restartable);
    assertTrue(presenter.isUnsubscribed(1));
    presenter.start(1);
    assertFalse(presenter.isUnsubscribed(1));
}
Also used : Subscription(rx.Subscription) Test(org.junit.Test)

Example 70 with Subscription

use of rx.Subscription in project nucleus by konmik.

the class RxPresenterTest method testAddRemove.

@Test
public void testAddRemove() throws Exception {
    RxPresenter presenter = new RxPresenter();
    Subscription mock = Mockito.mock(Subscription.class);
    when(mock.isUnsubscribed()).thenReturn(false);
    presenter.add(mock);
    presenter.remove(mock);
    verify(mock, atLeastOnce()).isUnsubscribed();
    verify(mock, times(1)).unsubscribe();
    presenter.onDestroy();
    verifyNoMoreInteractions(mock);
}
Also used : Subscription(rx.Subscription) Test(org.junit.Test)

Aggregations

Subscription (rx.Subscription)302 Test (org.junit.Test)82 List (java.util.List)75 CompositeSubscription (rx.subscriptions.CompositeSubscription)55 Action0 (rx.functions.Action0)45 ArrayList (java.util.ArrayList)41 CountDownLatch (java.util.concurrent.CountDownLatch)38 Func1 (rx.functions.Func1)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 Observable (rx.Observable)17 LinkedList (java.util.LinkedList)13 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)11 PlayList (io.github.ryanhoo.music.data.model.PlayList)11 MetaChangedEvent (io.hefuyi.listener.event.MetaChangedEvent)10 View (android.view.View)9 IOException (java.io.IOException)9 AndroidSchedulers (rx.android.schedulers.AndroidSchedulers)8 Intent (android.content.Intent)7