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);
}
}
}));
}
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();
}
}
});
}
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);
}
});
}
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);
}
});
}
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);
}
}));
}
Aggregations