Search in sources :

Example 31 with NonNull

use of io.reactivex.annotations.NonNull in project android-mvp-architecture by MindorksOpenSource.

the class OpenSourcePresenter method onViewPrepared.

@Override
public void onViewPrepared() {
    getMvpView().showLoading();
    getCompositeDisposable().add(getDataManager().getOpenSourceApiCall().subscribeOn(getSchedulerProvider().io()).observeOn(getSchedulerProvider().ui()).subscribe(new Consumer<OpenSourceResponse>() {

        @Override
        public void accept(@NonNull OpenSourceResponse openSourceResponse) throws Exception {
            if (openSourceResponse != null && openSourceResponse.getData() != null) {
                getMvpView().updateRepo(openSourceResponse.getData());
            }
            getMvpView().hideLoading();
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(@NonNull Throwable throwable) throws Exception {
            if (!isViewAttached()) {
                return;
            }
            getMvpView().hideLoading();
            // handle the error here
            if (throwable instanceof ANError) {
                ANError anError = (ANError) throwable;
                handleApiError(anError);
            }
        }
    }));
}
Also used : Consumer(io.reactivex.functions.Consumer) OpenSourceResponse(com.mindorks.framework.mvp.data.network.model.OpenSourceResponse) NonNull(io.reactivex.annotations.NonNull) ANError(com.androidnetworking.error.ANError)

Example 32 with NonNull

use of io.reactivex.annotations.NonNull in project RxJavaInAction by fengzhizi715.

the class TestEditTextActivity method initViews.

private void initViews() {
    Observable<CharSequence> ObservablePhone = RxTextView.textChanges(phone);
    Observable<CharSequence> ObservablePassword = RxTextView.textChanges(password);
    Observable.combineLatest(ObservablePhone, ObservablePassword, new BiFunction<CharSequence, CharSequence, ValidationResult>() {

        @Override
        public ValidationResult apply(@NonNull CharSequence o1, @NonNull CharSequence o2) throws Exception {
            if (o1.length() > 0 || o2.length() > 0) {
                login.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_login_pressed));
            } else {
                login.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_login_normal));
            }
            ValidationResult result = new ValidationResult();
            if (o1.length() == 0) {
                result.flag = false;
                result.message = "手机号码不能为空";
            } else if (o1.length() != 11) {
                result.flag = false;
                result.message = "手机号码需要11位";
            } else if (o1 != null && !AppUtils.isPhoneNumber(o1.toString())) {
                result.flag = false;
                result.message = "手机号码需要数字";
            } else if (o2.length() == 0) {
                result.flag = false;
                result.message = "密码不能为空";
            }
            return result;
        }
    }).subscribe(new Consumer<ValidationResult>() {

        @Override
        public void accept(@NonNull ValidationResult r) throws Exception {
            result = r;
        }
    });
    RxView.clicks(login).compose(RxUtils.useRxViewTransformer(TestEditTextActivity.this)).subscribeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Object>() {

        @Override
        public void accept(@NonNull Object o) throws Exception {
            if (result == null)
                return;
            if (result.flag) {
                Toast.makeText(TestEditTextActivity.this, "模拟登录成功", Toast.LENGTH_SHORT).show();
            } else if (Preconditions.isNotBlank(result.message)) {
                Toast.makeText(TestEditTextActivity.this, result.message, Toast.LENGTH_SHORT).show();
            }
        }
    });
}
Also used : BiFunction(io.reactivex.functions.BiFunction) NonNull(io.reactivex.annotations.NonNull) ValidationResult(com.safframework.study.rxbinding.domain.ValidationResult)

Example 33 with NonNull

use of io.reactivex.annotations.NonNull in project RxJavaInAction by fengzhizi715.

the class TestHttpClientWithMaybe method main.

public static void main(String[] args) {
    Maybe.create(new MaybeOnSubscribe<String>() {

        @Override
        public void subscribe(@NonNull MaybeEmitter<String> e) throws Exception {
            String url = "http://www.163.com";
            e.onSuccess(url);
        }
    }).map(new Function<String, CloseableHttpResponse>() {

        @Override
        public CloseableHttpResponse apply(@NonNull String url) throws Exception {
            CloseableHttpClient client = HttpClients.createDefault();
            HttpGet get = new HttpGet(url);
            return client.execute(get);
        }
    }).subscribe(new Consumer<CloseableHttpResponse>() {

        @Override
        public void accept(CloseableHttpResponse response) throws Exception {
            // 服务器返回码
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("statusCode = " + statusCode);
            HttpEntity entity = response.getEntity();
            // 服务器返回内容
            String respStr = null;
            if (entity != null) {
                respStr = EntityUtils.toString(entity, "UTF-8");
            }
            System.out.println(respStr);
            // 释放资源
            EntityUtils.consume(entity);
        }
    });
}
Also used : Function(io.reactivex.functions.Function) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) NonNull(io.reactivex.annotations.NonNull) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 34 with NonNull

use of io.reactivex.annotations.NonNull in project RxJavaInAction by fengzhizi715.

the class RoundRobinForParallel1 method main.

public static void main(String[] args) {
    final AtomicInteger batch = new AtomicInteger(0);
    Observable.range(1, 100).groupBy(new Function<Integer, Integer>() {

        @Override
        public Integer apply(@NonNull Integer integer) throws Exception {
            return batch.getAndIncrement() % 5;
        }
    }).flatMap(new Function<GroupedObservable<Integer, Integer>, ObservableSource<?>>() {

        @Override
        public ObservableSource<?> apply(@NonNull GroupedObservable<Integer, Integer> integerIntegerGroupedObservable) throws Exception {
            return integerIntegerGroupedObservable.observeOn(Schedulers.io()).map(new Function<Integer, String>() {

                @Override
                public String apply(@NonNull Integer integer) throws Exception {
                    return integer.toString();
                }
            });
        }
    }).subscribe(new Consumer<Object>() {

        @Override
        public void accept(@NonNull Object o) throws Exception {
            System.out.println(o);
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Function(io.reactivex.functions.Function) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NonNull(io.reactivex.annotations.NonNull) GroupedObservable(io.reactivex.observables.GroupedObservable)

Example 35 with NonNull

use of io.reactivex.annotations.NonNull in project AndroidLife by CaMnter.

the class RxJavaMapActivity method initData.

@Override
protected void initData() {
    /**
     * map一对一的类型转换
     * 通过map改变订阅者接受的参数
     * 传入的是Integer,改后变为String
     * 订阅者接收到的也是String
     */
    this.disposable.add(Flowable.just(KEY).map(new Function<Integer, String>() {

        @Override
        public String apply(@NonNull Integer integer) throws Exception {
            switch(integer) {
                case KEY:
                    return VALUE;
                default:
                    return VALUE;
            }
        }
    }).subscribe(new Consumer<String>() {

        @Override
        public void accept(@NonNull String s) throws Exception {
            RxJavaMapActivity.this.rxMapOneTV.setText(s);
        }
    }));
    RxData data1 = new RxData();
    data1.setId(106L);
    RxData data2 = new RxData();
    data2.setId(206L);
    RxData data3 = new RxData();
    data3.setId(266L);
    RxData[] data = { data1, data2, data3 };
    /**
     * map一对一的类型转换
     * 通过map改变订阅者接受的参数
     * 传入的是RxData,改后变为Long
     * 订阅者接收到的也是Long
     */
    this.disposable.add(Flowable.fromArray(data).map(new Function<RxData, Long>() {

        @Override
        public Long apply(@NonNull RxData rxData) throws Exception {
            return rxData.getId();
        }
    }).subscribe(new Consumer<Long>() {

        @Override
        public void accept(@NonNull Long aLong) throws Exception {
            String text = RxJavaMapActivity.this.rxMapTwoTV.getText().toString();
            text += aLong + " ";
            RxJavaMapActivity.this.rxMapTwoTV.setText(text);
        }
    }));
    RxData parentData = new RxData();
    RxChildData childData1 = new RxChildData();
    childData1.setChildContent("childData1");
    RxChildData childData2 = new RxChildData();
    childData2.setChildContent("childData2");
    RxChildData childData3 = new RxChildData();
    childData3.setChildContent("childData3");
    RxChildData[] childData = { childData1, childData2, childData3 };
    parentData.setChildData(childData);
    /**
     * flatMap一对多的类型转换
     * flatMap() 和 map() 有一个相同点:它也是把传入的参数转化之后返回另一个对象。
     * 和 map() 不同的是, flatMap() 中返回的是个 Observable 对象,
     * 并且这个 Observable 对象并不是被直接发送到了 Subscriber 的回调方法中。
     * flatMap() 的原理是这样的:
     * 1. 使用传入的事件对象创建一个 Observable 对象;
     * 2. 并不发送这个 Observable, 而是将它激活,于是它开始发送事件;
     * 3. 每一个创建出来的 Observable 发送的事件,都被汇入同一个 Observable,而
     * 这个 Observable 负责将这些事件统一交给 Subscriber 的回调方法。这三个步骤,把
     * 事件拆成了两级,通过一组新创建的 Observable 将初始的对象『铺平』之后通过统一路径
     * 分发了下去。而这个『铺平』就是 flatMap() 所谓的 flat
     */
    this.disposable.add(Flowable.fromArray(parentData).flatMap(new Function<RxData, Publisher<RxChildData>>() {

        @Override
        public Publisher<RxChildData> apply(@NonNull RxData rxData) throws Exception {
            return Flowable.fromArray(rxData.getChildData());
        }
    }).subscribe(new Consumer<RxChildData>() {

        @Override
        public void accept(@NonNull RxChildData rxChildData) throws Exception {
            String text = RxJavaMapActivity.this.rxFlatMapThrTV.getText().toString();
            text += rxChildData.getChildContent() + " ";
            RxJavaMapActivity.this.rxFlatMapThrTV.setText(text);
        }
    }));
}
Also used : Publisher(org.reactivestreams.Publisher) RxChildData(com.camnter.newlife.bean.RxChildData) Consumer(io.reactivex.functions.Consumer) NonNull(io.reactivex.annotations.NonNull) RxData(com.camnter.newlife.bean.RxData)

Aggregations

NonNull (io.reactivex.annotations.NonNull)39 Disposable (io.reactivex.disposables.Disposable)17 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)15 Consumer (io.reactivex.functions.Consumer)14 Function (io.reactivex.functions.Function)13 BuildDetails (com.khmelenko.lab.varis.network.response.BuildDetails)6 OnClick (butterknife.OnClick)5 List (java.util.List)5 RequestBody (okhttp3.RequestBody)5 ObservableEmitter (io.reactivex.ObservableEmitter)4 ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Headers (okhttp3.Headers)3 View (android.view.View)2 ANError (com.androidnetworking.error.ANError)2 User (com.khmelenko.lab.varis.network.response.User)2 LzyResponse (com.lzy.demo.model.LzyResponse)2 ServerModel (com.lzy.demo.model.ServerModel)2 Action (io.reactivex.functions.Action)2