Search in sources :

Example 36 with ObservableEmitter

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();
}
Also used : ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) Record(io.rx_cache2.internal.Record) ObservableEmitter(io.reactivex.ObservableEmitter)

Example 37 with ObservableEmitter

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("下载完成");
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) Progress(com.lzy.okgo.model.Progress) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) FileCallback(com.lzy.okgo.callback.FileCallback) NonNull(io.reactivex.annotations.NonNull) File(java.io.File) ObservableEmitter(io.reactivex.ObservableEmitter) OnClick(butterknife.OnClick)

Example 38 with ObservableEmitter

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);
}
Also used : Disposable(io.reactivex.disposables.Disposable) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) ArrayList(java.util.ArrayList) List(java.util.List) PixivIllustBean(pri.weiqiang.liyujapanese.mvp.bean.pixivIllust.PixivIllustBean) ObservableEmitter(io.reactivex.ObservableEmitter) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody)

Example 39 with ObservableEmitter

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();
            }
        });
    }
}
Also used : Bitmap(android.graphics.Bitmap) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) Bundle(android.os.Bundle) FileNotFoundException(java.io.FileNotFoundException) Uri(android.net.Uri) ObservableEmitter(io.reactivex.ObservableEmitter) FileNotFoundException(java.io.FileNotFoundException) ContentResolver(android.content.ContentResolver)

Aggregations

ObservableEmitter (io.reactivex.ObservableEmitter)39 ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)38 Disposable (io.reactivex.disposables.Disposable)24 IOException (java.io.IOException)7 Observable (io.reactivex.Observable)6 NonNull (io.reactivex.annotations.NonNull)6 File (java.io.File)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Intent (android.content.Intent)4 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)4 Consumer (io.reactivex.functions.Consumer)4 Word (pri.weiqiang.liyujapanese.mvp.bean.dic.Word)4 Observer (io.reactivex.Observer)3 Bitmap (android.graphics.Bitmap)2 Uri (android.net.Uri)2 NonNull (android.support.annotation.NonNull)2 OnClick (butterknife.OnClick)2 Progress (com.lzy.okgo.model.Progress)2 FileNotFoundException (java.io.FileNotFoundException)2