Search in sources :

Example 31 with ObservableEmitter

use of io.reactivex.ObservableEmitter 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 32 with ObservableEmitter

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

Example 33 with ObservableEmitter

use of io.reactivex.ObservableEmitter in project java by exercism.

the class HangmanTest method consecutiveGames.

@Ignore("Remove to run test")
@Test
public void consecutiveGames() {
    // This test setup is more complex because we have to order the emission of values in the
    // different observers.
    // 1. Word observable receives the work to guess
    // 2. Letter observable receives the various letters tried
    // 3. Word observable receives the new word to guess
    // 4. Letter observable receiveds the letters for the second word
    // Emitters respectively for the word and letter observables
    ObservableEmitter<String>[] emitters = new ObservableEmitter[2];
    Runnable emit = () -> {
        // Process sending the inputs in the right order
        emitters[0].onNext("secret");
        Stream.of("a", "e", "o", "s", "c", "r", "g", "t").forEach(emitters[1]::onNext);
        emitters[0].onNext("abba");
        Stream.of("a", "e", "s", "b").forEach(emitters[1]::onNext);
        emitters[0].onComplete();
    };
    Observable<String> words = createWordObservable(emitters, emit);
    Observable<String> letters = createLetterObservable(emitters, emit);
    Observable<Output> outputs = hangman.play(words, letters);
    // We collect the results of the game
    List<Output> results = new ArrayList<>();
    Disposable subscription = outputs.filter(output -> output.status != Status.PLAYING).subscribe(results::add);
    try {
        assertThat(results.size()).isEqualTo(2);
        Output first = results.get(0);
        assertThat(first.discovered).isEqualTo("secret");
        assertThat(first.guess).containsExactlyInAnyOrder("s", "e", "c", "r", "t");
        assertThat(first.misses).containsExactlyInAnyOrder("a", "o", "g");
        assertThat(first.status).isEqualTo(Status.WIN);
        Output second = results.get(1);
        assertThat(second.discovered).isEqualTo("abba");
        assertThat(second.guess).containsExactlyInAnyOrder("a", "b");
        assertThat(second.misses).containsExactlyInAnyOrder("e", "s");
        assertThat(first.status).isEqualTo(Status.WIN);
    } finally {
        subscription.dispose();
    }
}
Also used : Disposable(io.reactivex.disposables.Disposable) Disposable(io.reactivex.disposables.Disposable) java.util(java.util) Rule(org.junit.Rule) Stream(java.util.stream.Stream) Ignore(org.junit.Ignore) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ObservableEmitter(io.reactivex.ObservableEmitter) Observable(io.reactivex.Observable) Test(org.junit.Test) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) ObservableEmitter(io.reactivex.ObservableEmitter) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 34 with ObservableEmitter

use of io.reactivex.ObservableEmitter in project 91Pop by DanteAndroid.

the class FavoritePresenter method exportData.

@Override
public void exportData(final boolean onlyUrl) {
    Observable.create(new ObservableOnSubscribe<List<UnLimit91PornItem>>() {

        @Override
        public void subscribe(ObservableEmitter<List<UnLimit91PornItem>> e) throws Exception {
            List<UnLimit91PornItem> unLimit91PornItems = dataManager.loadAllLimit91PornItems();
            e.onNext(unLimit91PornItems);
            e.onComplete();
        }
    }).map(new Function<List<UnLimit91PornItem>, String>() {

        @Override
        public String apply(List<UnLimit91PornItem> unLimit91PornItems) throws Exception {
            File file = new File(SDCardUtils.EXPORT_FILE);
            if (file.exists()) {
                if (!file.delete()) {
                    throw new Exception("导出失败,因为删除原文件失败了");
                }
            }
            if (!file.createNewFile()) {
                throw new Exception("导出失败,创建新文件失败了");
            }
            if (onlyUrl) {
                for (UnLimit91PornItem unLimit91PornItem : unLimit91PornItems) {
                    CharSequence data = unLimit91PornItem.getVideoResult().getVideoUrl() + "\r\n\r\n";
                    if (TextUtils.isEmpty(data)) {
                        continue;
                    }
                    FileUtils.writeChars(file, "UTF-8", data);
                }
            } else {
                for (UnLimit91PornItem unLimit91PornItem : unLimit91PornItems) {
                    String title = unLimit91PornItem.getTitle();
                    String videoUrl = unLimit91PornItem.getVideoResult().getVideoUrl();
                    CharSequence data = title + "\r\n" + videoUrl + "\r\n\r\n";
                    if (TextUtils.isEmpty(data)) {
                        continue;
                    }
                    FileUtils.writeChars(file, "UTF-8", data);
                }
            }
            return "导出成功";
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).compose(provider.<String>bindUntilEvent(Lifecycle.Event.ON_STOP)).subscribe(new CallBackWrapper<String>() {

        @Override
        public void onBegin(Disposable d) {
        }

        @Override
        public void onSuccess(final String s) {
            ifViewAttached(new ViewAction<FavoriteView>() {

                @Override
                public void run(@NonNull FavoriteView view) {
                    view.showMessage(s, TastyToast.SUCCESS);
                }
            });
        }

        @Override
        public void onError(final String msg, int code) {
            ifViewAttached(new ViewAction<FavoriteView>() {

                @Override
                public void run(@NonNull FavoriteView view) {
                    view.showMessage(msg, TastyToast.ERROR);
                }
            });
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) ApiException(com.dante.exception.ApiException) NonNull(android.support.annotation.NonNull) List(java.util.List) UnLimit91PornItem(com.dante.data.model.UnLimit91PornItem) File(java.io.File) ObservableEmitter(io.reactivex.ObservableEmitter)

Example 35 with ObservableEmitter

use of io.reactivex.ObservableEmitter in project 91Pop by DanteAndroid.

the class AboutPresenter method cleanCacheFile.

@Override
public void cleanCacheFile(final List<File> fileDirList, final Context context) {
    Observable.create(new ObservableOnSubscribe<Boolean>() {

        @Override
        public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
            boolean result = true;
            for (File fileDir : fileDirList) {
                if (fileDir.getAbsolutePath().contains("glide")) {
                    Logger.d("开始清图片缓存");
                    GlideApp.get(context).clearDiskCache();
                    result = true;
                    break;
                } else {
                    result = AppCacheUtils.cleanCacheFile(fileDir);
                }
            }
            if (result) {
                emitter.onNext(true);
                emitter.onComplete();
            } else {
                emitter.onError(new Throwable("clean cache file failure"));
            }
        }
    }).subscribeOn(Schedulers.io()).delay(1, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).compose(provider.<Boolean>bindUntilEvent(Lifecycle.Event.ON_DESTROY)).subscribe(new CallBackWrapper<Boolean>() {

        @Override
        public void onBegin(Disposable d) {
            ifViewAttached(new ViewAction<AboutView>() {

                @Override
                public void run(@NonNull AboutView view) {
                    view.showCleanDialog("清除缓存中,请稍后...");
                }
            });
        }

        @Override
        public void onSuccess(Boolean aBoolean) {
            ifViewAttached(new ViewAction<AboutView>() {

                @Override
                public void run(@NonNull AboutView view) {
                    view.cleanCacheSuccess("清除缓存成功!");
                }
            });
        }

        @Override
        public void onError(String msg, int code) {
            ifViewAttached(new ViewAction<AboutView>() {

                @Override
                public void run(@NonNull AboutView view) {
                    view.cleanCacheFailure("清除缓存失败!");
                }
            });
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) NonNull(android.support.annotation.NonNull) File(java.io.File) 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