Search in sources :

Example 41 with ObservableOnSubscribe

use of io.reactivex.ObservableOnSubscribe in project MyJapanese 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 42 with ObservableOnSubscribe

use of io.reactivex.ObservableOnSubscribe in project MyJapanese by 54wall.

the class GojuonFragmentModelImpl method getData.

@Override
public void getData(final int category, Consumer<List<GojuonItem>> consumer) {
    Disposable disposable = Observable.create(new ObservableOnSubscribe<List<GojuonItem>>() {

        @Override
        public void subscribe(ObservableEmitter<List<GojuonItem>> emitter) throws Exception {
            List<GojuonItem> list;
            switch(category) {
                case Constants.CATEGORY_QINGYIN:
                    list = DBManager.getInstance().getQingYin();
                    break;
                case Constants.CATEGORY_ZHUOYIN:
                    list = DBManager.getInstance().getZhuoYin();
                    break;
                case Constants.CATEGORY_AOYIN:
                    list = DBManager.getInstance().getAoYin();
                    break;
                default:
                    list = null;
                    break;
            }
            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) GojuonItem(pri.weiqiang.liyujapanese.mvp.bean.gojuon.GojuonItem) ObservableEmitter(io.reactivex.ObservableEmitter)

Example 43 with ObservableOnSubscribe

use of io.reactivex.ObservableOnSubscribe 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 44 with ObservableOnSubscribe

use of io.reactivex.ObservableOnSubscribe 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)

Example 45 with ObservableOnSubscribe

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

the class RxJavaActivity method zip1.

/**
 * zip操作符
 * 将上游的两个管道按顺序合并,合并的最终长度为管道事件最少的的管道。默认是在同一个线程中完成,想要同时进行,需要设置Schedulers.io()
 */
public void zip1(View v) {
    Observable observable1 = Observable.create(new ObservableOnSubscribe<Integer>() {

        @Override
        public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
            emitter.onNext(1);
            LogUtils.e("onNext 1");
            emitter.onNext(2);
            LogUtils.e("onNext 2");
            emitter.onNext(3);
            LogUtils.e("onNext 3");
            emitter.onNext(4);
            LogUtils.e("onNext 4");
            emitter.onComplete();
            LogUtils.e("onComplete 1");
        }
    }).subscribeOn(Schedulers.io());
    Observable observable2 = Observable.create(new ObservableOnSubscribe<String>() {

        @Override
        public void subscribe(ObservableEmitter<String> emitter) throws Exception {
            emitter.onNext("A");
            LogUtils.e("onNext A");
            emitter.onNext("B");
            LogUtils.e("onNext B");
            emitter.onNext("C");
            LogUtils.e("onNext C");
            emitter.onComplete();
            LogUtils.e("onComplete 2");
        }
    }).subscribeOn(Schedulers.io());
    Observable.zip(observable1, observable2, new BiFunction<Integer, String, String>() {

        @Override
        public String apply(Integer integer, String s) throws Exception {
            return integer + s;
        }
    }).subscribe(new Observer() {

        @Override
        public void onSubscribe(Disposable d) {
            LogUtils.e("onSubscribe");
        }

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

        @Override
        public void onError(Throwable e) {
            LogUtils.e("onError : ");
        }

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

Aggregations

ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)64 ObservableEmitter (io.reactivex.ObservableEmitter)54 Disposable (io.reactivex.disposables.Disposable)36 Observable (io.reactivex.Observable)14 ArrayList (java.util.ArrayList)12 List (java.util.List)12 File (java.io.File)11 SupportSQLiteDatabase (androidx.sqlite.db.SupportSQLiteDatabase)9 NonNull (io.reactivex.annotations.NonNull)8 IOException (java.io.IOException)8 Intent (android.content.Intent)7 Observer (io.reactivex.Observer)7 Consumer (io.reactivex.functions.Consumer)7 Bitmap (android.graphics.Bitmap)6 Bundle (android.os.Bundle)6 OnClick (butterknife.OnClick)6 Uri (android.net.Uri)5 View (android.view.View)5 BindView (butterknife.BindView)5 HashMap (java.util.HashMap)5