use of io.reactivex.functions.Consumer in project RxDownload by ssseasonnn.
the class SingleMission method start.
@Override
public void start(final Semaphore semaphore) throws InterruptedException {
if (isCanceled()) {
return;
}
semaphore.acquire();
if (isCanceled()) {
semaphore.release();
return;
}
disposable = rxdownload.download(bean).subscribeOn(Schedulers.io()).doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
if (observer != null) {
observer.onSubscribe(disposable);
}
}
}).doFinally(new Action() {
@Override
public void run() throws Exception {
log("finally and release...");
semaphore.release();
}
}).subscribe(new Consumer<DownloadStatus>() {
@Override
public void accept(DownloadStatus value) throws Exception {
status = value;
processor.onNext(started(value));
if (observer != null) {
observer.onNext(value);
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
processor.onNext(failed(status, throwable));
if (observer != null) {
observer.onError(throwable);
}
}
}, new Action() {
@Override
public void run() throws Exception {
processor.onNext(completed(status));
setCompleted(true);
if (observer != null) {
observer.onComplete();
}
}
});
}
use of io.reactivex.functions.Consumer in project RxDownload by ssseasonnn.
the class MultiMissionDownloadActivity method onResume.
@Override
protected void onResume() {
super.onResume();
//
startMultiMission();
//
disposable1 = rxDownload.receiveDownloadStatus(url1).subscribe(new Consumer<DownloadEvent>() {
@Override
public void accept(DownloadEvent downloadEvent) throws Exception {
int flag = downloadEvent.getFlag();
switch(flag) {
case DownloadFlag.NORMAL:
binding.contentMultiMissionDownload.control1.setVisibility(View.GONE);
break;
case DownloadFlag.WAITING:
binding.contentMultiMissionDownload.control1.setVisibility(View.VISIBLE);
binding.contentMultiMissionDownload.control1.setText("等待中");
break;
case DownloadFlag.STARTED:
binding.contentMultiMissionDownload.control1.setText("下载中");
break;
case DownloadFlag.PAUSED:
binding.contentMultiMissionDownload.control1.setText("已暂停");
break;
case DownloadFlag.COMPLETED:
binding.contentMultiMissionDownload.control1.setText("已完成");
break;
case DownloadFlag.FAILED:
Throwable throwable = downloadEvent.getError();
log(throwable);
binding.contentMultiMissionDownload.control1.setText("失败");
break;
}
DownloadStatus status = downloadEvent.getDownloadStatus();
binding.contentMultiMissionDownload.progress1.setProgress(status.getPercentNumber());
}
});
//
disposable2 = rxDownload.receiveDownloadStatus(url2).subscribe(new Consumer<DownloadEvent>() {
@Override
public void accept(DownloadEvent downloadEvent) throws Exception {
int flag = downloadEvent.getFlag();
switch(flag) {
case DownloadFlag.NORMAL:
binding.contentMultiMissionDownload.control2.setVisibility(View.GONE);
break;
case DownloadFlag.WAITING:
binding.contentMultiMissionDownload.control2.setVisibility(View.VISIBLE);
binding.contentMultiMissionDownload.control2.setText("等待中");
break;
case DownloadFlag.STARTED:
binding.contentMultiMissionDownload.control2.setText("下载中");
break;
case DownloadFlag.PAUSED:
binding.contentMultiMissionDownload.control2.setText("已暂停");
break;
case DownloadFlag.COMPLETED:
binding.contentMultiMissionDownload.control2.setText("已完成");
break;
case DownloadFlag.FAILED:
Throwable throwable = downloadEvent.getError();
log(throwable);
binding.contentMultiMissionDownload.control2.setText("失败");
break;
}
DownloadStatus status = downloadEvent.getDownloadStatus();
binding.contentMultiMissionDownload.progress2.setProgress(status.getPercentNumber());
}
});
//
disposable3 = rxDownload.receiveDownloadStatus(url3).subscribe(new Consumer<DownloadEvent>() {
@Override
public void accept(DownloadEvent downloadEvent) throws Exception {
int flag = downloadEvent.getFlag();
switch(flag) {
case DownloadFlag.NORMAL:
binding.contentMultiMissionDownload.control3.setVisibility(View.GONE);
break;
case DownloadFlag.WAITING:
binding.contentMultiMissionDownload.control3.setVisibility(View.VISIBLE);
binding.contentMultiMissionDownload.control3.setText("等待中");
break;
case DownloadFlag.STARTED:
binding.contentMultiMissionDownload.control3.setText("下载中");
break;
case DownloadFlag.PAUSED:
binding.contentMultiMissionDownload.control3.setText("已暂停");
break;
case DownloadFlag.COMPLETED:
binding.contentMultiMissionDownload.control3.setText("已完成");
break;
case DownloadFlag.FAILED:
Throwable throwable = downloadEvent.getError();
log(throwable);
binding.contentMultiMissionDownload.control3.setText("失败");
break;
}
DownloadStatus status = downloadEvent.getDownloadStatus();
binding.contentMultiMissionDownload.progress3.setProgress(status.getPercentNumber());
}
});
}
use of io.reactivex.functions.Consumer in project RxJava by ReactiveX.
the class FlowableSwitchIfEmptyTest method testSwitchWhenNotEmpty.
@Test
public void testSwitchWhenNotEmpty() throws Exception {
final AtomicBoolean subscribed = new AtomicBoolean(false);
final Flowable<Integer> observable = Flowable.just(4).switchIfEmpty(Flowable.just(2).doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) {
subscribed.set(true);
}
}));
assertEquals(4, observable.blockingSingle().intValue());
assertFalse(subscribed.get());
}
use of io.reactivex.functions.Consumer in project RxJava by ReactiveX.
the class ObservableAmbTest method testSubscriptionOnlyHappensOnce.
@SuppressWarnings("unchecked")
@Test
public void testSubscriptionOnlyHappensOnce() throws InterruptedException {
final AtomicLong count = new AtomicLong();
Consumer<Disposable> incrementer = new Consumer<Disposable>() {
@Override
public void accept(Disposable s) {
count.incrementAndGet();
}
};
//this aync stream should emit first
Observable<Integer> o1 = Observable.just(1).doOnSubscribe(incrementer).delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
//this stream emits second
Observable<Integer> o2 = Observable.just(1).doOnSubscribe(incrementer).delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
TestObserver<Integer> ts = new TestObserver<Integer>();
Observable.ambArray(o1, o2).subscribe(ts);
ts.awaitTerminalEvent(5, TimeUnit.SECONDS);
ts.assertNoErrors();
assertEquals(2, count.get());
}
use of io.reactivex.functions.Consumer in project requery by requery.
the class PeopleActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle("People");
}
setContentView(R.layout.activity_people);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
data = ((PeopleApplication) getApplication()).getData();
executor = Executors.newSingleThreadExecutor();
adapter = new PersonAdapter();
adapter.setExecutor(executor);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
data.count(Person.class).get().single().subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
if (integer == 0) {
Observable.fromCallable(new CreatePeople(data)).flatMap(new Function<Observable<Iterable<Person>>, Observable<?>>() {
@Override
public Observable<?> apply(Observable<Iterable<Person>> o) {
return o;
}
}).observeOn(Schedulers.computation()).subscribe(new Consumer<Object>() {
@Override
public void accept(Object o) {
adapter.queryAsync();
}
});
}
}
});
}
Aggregations