use of io.reactivex.ObservableEmitter in project RxCache by VictorAlbertos.
the class EvictExpirableRecordsPersistence method oEvictingTask.
private Observable<String> oEvictingTask() {
Observable<String> oEvictingTask = Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
if (!couldBeExpirableRecords) {
emitter.onNext(Locale.RECORD_CAN_NOT_BE_EVICTED_BECAUSE_NO_ONE_IS_EXPIRABLE);
emitter.onComplete();
return;
}
int storedMB = persistence.storedMB();
if (!reachedPercentageMemoryToStart(storedMB)) {
emitter.onComplete();
return;
}
List<String> allKeys = persistence.allKeys();
float releasedMBSoFar = 0f;
for (String key : allKeys) {
if (reachedPercentageMemoryToStop(storedMB, releasedMBSoFar)) {
break;
}
Record record = persistence.retrieveRecord(key, isEncrypted, encryptKey);
if (record == null)
continue;
if (!record.getExpirable())
continue;
persistence.evict(key);
emitter.onNext(key);
releasedMBSoFar += record.getSizeOnMb();
}
couldBeExpirableRecords = reachedPercentageMemoryToStop(storedMB, releasedMBSoFar);
emitter.onComplete();
}
}).subscribeOn((Schedulers.io())).observeOn(Schedulers.io()).doOnError(new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
throwable.printStackTrace();
}
});
return oEvictingTask.share();
}
use of io.reactivex.ObservableEmitter in project okhttp-OkGo by jeasonlzy.
the class RxFileDownloadActivity method fileDownload2.
@OnClick(R.id.fileDownload2)
public void fileDownload2(View view) {
Observable.create(new ObservableOnSubscribe<Progress>() {
@Override
public void subscribe(@NonNull final ObservableEmitter<Progress> e) throws Exception {
String etString = etUrl.getText().toString();
String url = TextUtils.isEmpty(etString) ? Urls.URL_DOWNLOAD : etString;
//
OkGo.<File>get(url).headers("aaa", //
"111").params("bbb", //
"222").execute(new FileCallback() {
@Override
public void onSuccess(Response<File> response) {
e.onComplete();
}
@Override
public void onError(Response<File> response) {
e.onError(response.getException());
}
@Override
public void downloadProgress(Progress progress) {
e.onNext(progress);
}
});
}
}).doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(@NonNull Disposable disposable) throws Exception {
btnFileDownload2.setText("正在下载中...");
}
}).observeOn(//
AndroidSchedulers.mainThread()).subscribe(new Observer<Progress>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
addDisposable(d);
}
@Override
public void onNext(@NonNull Progress progress) {
String downloadLength = Formatter.formatFileSize(getApplicationContext(), progress.currentSize);
String totalLength = Formatter.formatFileSize(getApplicationContext(), progress.totalSize);
tvDownloadSize.setText(downloadLength + "/" + totalLength);
String speed = Formatter.formatFileSize(getApplicationContext(), progress.speed);
tvNetSpeed.setText(String.format("%s/s", speed));
tvProgress.setText(numberFormat.format(progress.fraction));
pbProgress.setMax(10000);
pbProgress.setProgress((int) (progress.fraction * 10000));
}
@Override
public void onError(@NonNull Throwable e) {
e.printStackTrace();
btnFileDownload2.setText("下载出错");
showToast(e.getMessage());
}
@Override
public void onComplete() {
btnFileDownload2.setText("下载完成");
}
});
}
use of io.reactivex.ObservableEmitter in project LiYuJapanese by 54wall.
the class PixivIllustFragmentModelImpl method getIllusts.
@Override
public void getIllusts(final String mode, Consumer<List<PixivIllustBean>> consumer) {
Disposable disposable = Observable.create(new ObservableOnSubscribe<List<PixivIllustBean>>() {
@Override
public void subscribe(final ObservableEmitter<List<PixivIllustBean>> emitter) throws Exception {
service.getIllusts(mode, new Consumer<ResponseBody>() {
@Override
public void accept(ResponseBody responseBody) throws Exception {
List<PixivIllustBean> list = handleResponseBody(responseBody);
if (list == null) {
emitter.onError(new Exception());
} else {
emitter.onNext(list);
}
}
});
emitter.onComplete();
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(consumer);
mCompositeDisposable.add(disposable);
}
use of io.reactivex.ObservableEmitter in project EnableHands by LeviWGG.
the class ScanQRCodeFragment method onActivityResult.
@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (RESULT_OK != resultCode)
return;
if (data == null)
return;
Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
Uri uri = data.getData();
String result = null;
ContentResolver cr = getActivity().getContentResolver();
try {
Bitmap bmp = BitmapFactory.decodeStream(cr.openInputStream(uri));
result = QRCodeDecoder.syncDecodeQRCode(bmp);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
emitter.onNext(result);
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Bundle bundle = new Bundle();
bundle.putString("QRCODE", s);
setFragmentResult(RESULT_OK, bundle);
pop();
}
});
}
}
Aggregations