Search in sources :

Example 16 with ObservableEmitter

use of io.reactivex.ObservableEmitter in project RxTools by vondear.

the class RxCameraTool method initCameraEvent.

public static void initCameraEvent(final Context mContext, final RxCameraView mCameraView, final byte[] data, final String fileDir, final String picName, final double mLongitude, final double mLatitude, final boolean isEconomize, final OnRxCamera onRxCamera) {
    onRxCamera.onBefore();
    Observable.create(new ObservableOnSubscribe<Integer>() {

        @Override
        public void subscribe(@NonNull final ObservableEmitter<Integer> e) {
            File fileParent = new File(fileDir);
            File cacheParent = new File(RxConstants.PICTURE_CACHE_PATH);
            if (!cacheParent.exists()) {
                cacheParent.mkdirs();
            }
            if (!fileParent.exists()) {
                fileParent.mkdirs();
            }
            final File cachefile = new File(cacheParent, picName);
            final File compressFile = new File(fileParent, picName);
            OutputStream os = null;
            try {
                os = new FileOutputStream(cachefile);
                os.write(data);
                os.close();
                RxMagic.createBuilder(mContext).load(cachefile).setCompressListener(new OnCompressListener() {

                    @Override
                    public void onStart() {
                        TLog.d("图片压缩", "开始压缩");
                    }

                    @Override
                    public void onSuccess(File file) {
                        if (RxFileTool.copyOrMoveFile(file, compressFile, true)) {
                            TLog.d("图片压缩", "压缩完成");
                            onRxCamera.onSuccessCompress(compressFile);
                            if (mLongitude != 0 || mLatitude != 0) {
                                RxExifTool.writeLatLonIntoJpeg(compressFile.getAbsolutePath(), mLatitude, mLongitude);
                                onRxCamera.onSuccessExif(compressFile);
                                e.onNext(2);
                            } else {
                                e.onNext(3);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        TLog.d("图片压缩", "压缩异常");
                    // Logger.d(e);
                    }
                }).launch();
            } catch (IOException e1) {
                TLog.w("onPictureTaken", "Cannot write to " + compressFile, e1);
            } finally {
                if (os != null) {
                    try {
                        os.close();
                        e.onNext(1);
                    } catch (IOException e2) {
                    // Ignore
                    }
                }
            }
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Integer>() {

        // 第二步:初始化Observer
        private Disposable mDisposable;

        @Override
        public void onSubscribe(@NonNull Disposable d) {
            mDisposable = d;
        }

        @Override
        public void onNext(@NonNull Integer integer) {
            switch(integer) {
                case 1:
                    try {
                        if (isEconomize) {
                            mCameraView.stop();
                        }
                    } catch (Exception e) {
                    }
                    break;
                case 2:
                    RxToast.normal("拍照成功");
                    break;
                case 3:
                    RxToast.error("请先获取定位信息");
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onError(@NonNull Throwable e) {
        }

        @Override
        public void onComplete() {
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) OnCompressListener(com.tamsiree.rxkit.photomagic.OnCompressListener) IOException(java.io.IOException) IOException(java.io.IOException) NonNull(io.reactivex.annotations.NonNull) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ObservableEmitter(io.reactivex.ObservableEmitter)

Example 17 with ObservableEmitter

use of io.reactivex.ObservableEmitter in project Offer by woshiyizhijiao.

the class RxJavaActivity method zip2.

/**
 * 上游两个管道的其中一个在无线发送事件,而另一个管道只发送一个,最终导致oom
 */
public void zip2(View v) {
    Observable observable1 = Observable.create(new ObservableOnSubscribe<Integer>() {

        @Override
        public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
            for (int x = 0; ; x++) {
                emitter.onNext(1);
            }
        }
    }).subscribeOn(Schedulers.io());
    Observable<String> observable2 = Observable.create(new ObservableOnSubscribe<String>() {

        @Override
        public void subscribe(ObservableEmitter<String> emitter) throws Exception {
            emitter.onNext("第二个管道");
        }
    }).subscribeOn(Schedulers.io());
    Observable.zip(observable1, observable2, new BiFunction<Integer, String, String>() {

        /**
         * Calculate a value based on the input values.
         *
         * @param integer the first value
         * @param s       the second value
         * @return the result value
         * @throws Exception on error
         */
        @Override
        public String apply(Integer integer, String s) throws Exception {
            return integer + s;
        }
    }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer() {

        @Override
        public void onSubscribe(Disposable d) {
        }

        @Override
        public void onNext(Object value) {
            LogUtils.e("onNext" + value);
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onComplete() {
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) Observable(io.reactivex.Observable) Observer(io.reactivex.Observer) ObservableEmitter(io.reactivex.ObservableEmitter)

Example 18 with ObservableEmitter

use of io.reactivex.ObservableEmitter in project LiYuJapanese by 54wall.

the class SplashActivity method initData.

private void initData() {
    // 预加载数据库,音频与GIF资源(GIF资源觉得可以放在后边)
    Disposable disposable = Observable.create(new ObservableOnSubscribe<String>() {

        @Override
        public void subscribe(ObservableEmitter<String> emitter) throws Exception {
            emitter.onNext(ResourceUtils.getString(SplashActivity.this, R.string.loading_data));
            DBManager.getInstance().init();
            GifManager.getInstance().init();
            // SoundPoolManager.init考虑后续变为异步处理进入五十音图在启动
            SoundPoolManager.getInstance().init();
            emitter.onNext(ResourceUtils.getString(SplashActivity.this, R.string.loading_data_success));
            emitter.onComplete();
            // 添加有米广告需要屏蔽下两行代码
            // Thread.sleep(1000);
            startActivity(new Intent(SplashActivity.this, MainActivity.class));
            finish();
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<String>() {

        @Override
        public void accept(String s) throws Exception {
            mTextView.setText(s);
        }
    });
    addDisposable(disposable);
}
Also used : Disposable(io.reactivex.disposables.Disposable) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) Intent(android.content.Intent) ObservableEmitter(io.reactivex.ObservableEmitter)

Example 19 with ObservableEmitter

use of io.reactivex.ObservableEmitter in project LiYuJapanese by 54wall.

the class FavWordsFragmentModelImpl method getData.

@Override
public void getData(Consumer<List<Word>> consumer, Consumer<Throwable> throwable, final String lessonId) {
    Disposable disposable = Observable.create(new ObservableOnSubscribe<List<Word>>() {

        @Override
        public void subscribe(ObservableEmitter<List<Word>> emitter) throws Exception {
            List<Word> list;
            list = DBManager.getInstance().getFav(lessonId);
            if (list == null) {
                Log.e(TAG, "list == null");
            // emitter.onError(new Exception());//经常报这个错误io.reactivex.exceptions.OnErrorNotImplementedException
            } else {
                emitter.onNext(list);
            }
            emitter.onComplete();
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(consumer, throwable);
    mCompositeDisposable.add(disposable);
}
Also used : Disposable(io.reactivex.disposables.Disposable) Word(pri.weiqiang.liyujapanese.mvp.bean.dic.Word) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) ObservableEmitter(io.reactivex.ObservableEmitter)

Example 20 with ObservableEmitter

use of io.reactivex.ObservableEmitter in project LiYuJapanese by 54wall.

the class LessonsFragmentModelImpl method getData.

@Override
public void getData(Consumer<List<Book>> consumer) {
    Disposable disposable = Observable.create(new ObservableOnSubscribe<List<Book>>() {

        @Override
        public void subscribe(ObservableEmitter<List<Book>> emitter) throws Exception {
            List<Book> list;
            list = DBManager.getInstance().getBooks();
            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) Book(pri.weiqiang.liyujapanese.mvp.bean.dic.Book) ObservableEmitter(io.reactivex.ObservableEmitter)

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