Search in sources :

Example 36 with ObservableOnSubscribe

use of io.reactivex.ObservableOnSubscribe in project WorkHelperApp by hubme.

the class RxJavaSampleFragment method onTimeInterval.

/*
    将一个发射数据的Observable转换为发射那些数据发射时间间隔的Observable.
    操作符拦截原始Observable发射的数据项,替换为发射表示相邻发射物时间间隔的对象。
     */
@OnClick(R.id.tv_timeInterval)
public void onTimeInterval() {
    Disposable subscribe = Observable.create(new ObservableOnSubscribe<String>() {

        @Override
        public void subscribe(@NonNull ObservableEmitter<String> e) throws Exception {
            for (int i = 0; i < 3; i++) {
                e.onNext(i + "");
                // Thread.sleep(1000);
                SystemClock.sleep(1000);
            }
            e.onComplete();
        }
    }).subscribeOn(Schedulers.newThread()).timeInterval().subscribe(new Consumer<Timed<String>>() {

        @Override
        public void accept(@NonNull Timed<String> stringTimed) throws Exception {
            Logger.i(String.valueOf(stringTimed.time()));
        }
    });
    mCompositeDisposable.add(subscribe);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) Timed(io.reactivex.schedulers.Timed) NonNull(io.reactivex.annotations.NonNull) ObservableEmitter(io.reactivex.ObservableEmitter) OnClick(butterknife.OnClick)

Example 37 with ObservableOnSubscribe

use of io.reactivex.ObservableOnSubscribe in project SeeWeather by xcc3641.

the class MultiCityFragment method multiLoad.

private void multiLoad() {
    mWeathers.clear();
    Observable.create((ObservableOnSubscribe<CityORM>) emitter -> {
        try {
            for (CityORM cityORM : OrmLite.getInstance().query(CityORM.class)) {
                emitter.onNext(cityORM);
            }
            emitter.onComplete();
        } catch (Exception e) {
            emitter.onError(e);
        }
    }).doOnSubscribe(subscription -> mRefreshLayout.setRefreshing(true)).map(city -> Util.replaceCity(city.getName())).distinct().flatMap(cityName -> RetrofitSingleton.getInstance().fetchWeather(cityName)).filter(weather -> !C.UNKNOWN_CITY.equals(weather.status)).take(3).compose(RxUtil.fragmentLifecycle(this)).doOnNext(weather -> mWeathers.add(weather)).doOnComplete(() -> {
        mRefreshLayout.setRefreshing(false);
        mAdapter.notifyDataSetChanged();
        if (mAdapter.isEmpty()) {
            mLayout.setVisibility(View.VISIBLE);
        } else {
            mLayout.setVisibility(View.GONE);
        }
    }).doOnError(error -> {
        if (mAdapter.isEmpty() && mLayout != null) {
            mLayout.setVisibility(View.VISIBLE);
        }
    }).subscribe();
}
Also used : LinearLayout(android.widget.LinearLayout) BaseFragment(com.xiecc.seeWeather.base.BaseFragment) MultiUpdateEvent(com.xiecc.seeWeather.modules.main.domain.MultiUpdateEvent) Bundle(android.os.Bundle) ButterKnife(butterknife.ButterKnife) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) R(com.xiecc.seeWeather.R) C(com.xiecc.seeWeather.common.C) BindView(butterknife.BindView) ArrayList(java.util.ArrayList) RetrofitSingleton(com.xiecc.seeWeather.component.RetrofitSingleton) Util(com.xiecc.seeWeather.common.utils.Util) Locale(java.util.Locale) MultiCityAdapter(com.xiecc.seeWeather.modules.main.adapter.MultiCityAdapter) RxUtil(com.xiecc.seeWeather.common.utils.RxUtil) View(android.view.View) Observable(io.reactivex.Observable) Weather(com.xiecc.seeWeather.modules.main.domain.Weather) RxBus(com.xiecc.seeWeather.component.RxBus) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) LayoutInflater(android.view.LayoutInflater) OrmLite(com.xiecc.seeWeather.component.OrmLite) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) ViewGroup(android.view.ViewGroup) CityORM(com.xiecc.seeWeather.modules.main.domain.CityORM) RecyclerView(android.support.v7.widget.RecyclerView) List(java.util.List) AlertDialog(android.support.v7.app.AlertDialog) Nullable(android.support.annotation.Nullable) Snackbar(android.support.design.widget.Snackbar) WhereBuilder(com.litesuits.orm.db.assit.WhereBuilder) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) CityORM(com.xiecc.seeWeather.modules.main.domain.CityORM)

Example 38 with ObservableOnSubscribe

use of io.reactivex.ObservableOnSubscribe in project SmartCampus by Vegen.

the class HomePresenter method getNewsList.

@Override
public void getNewsList(String url) {
    Disposable disposable = Observable.create((ObservableOnSubscribe<Document>) e -> {
        Document document = Jsoup.connect(url).get();
        e.onNext(document);
        e.onComplete();
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(document -> {
        Element body = document.body();
        Element photoList = body.getElementById("ListNews").getElementById("leftbox").getElementById("photolist");
        // 新闻列表
        List<NewsList> newsListList = new ArrayList<>();
        // NEWS_TYPE_FOCUS 要闻
        newsListList.add(new NewsList(NEWS_TYPE_FOCUS, getTagNews(photoList, "xxyw", NEWS_TYPE_FOCUS)));
        // NEWS_TYPE_COMPREHENSIVE 综合
        newsListList.add(new NewsList(NEWS_TYPE_COMPREHENSIVE, getTagNews(photoList, "zhxw", NEWS_TYPE_COMPREHENSIVE)));
        // NEWS_TYPE_FLASH 快讯
        newsListList.add(new NewsList(NEWS_TYPE_FLASH, getTagNews(photoList, "xykx", NEWS_TYPE_FLASH)));
        // NEWS_TYPE_OTHER 其他
        newsListList.add(new NewsList(NEWS_TYPE_OTHER, getTagNews(photoList, "otherPic", NEWS_TYPE_OTHER)));
        List<String> strings = new ArrayList<>();
        Elements elements = photoList.select("div.more");
        for (Element element : elements) {
            String id = element.getElementsByTag("a").attr("href");
            strings.add(id);
        }
        if (mView != null) {
            mView.showNewsList(newsListList);
            mView.moreNewsClassId(strings);
            mView.hideLoading(false);
        }
    }, throwable -> {
        if (mView != null) {
            mView.showMessage(HttpError.getErrorMessage(throwable));
            mView.hideLoading(true);
        }
    });
    mHttpLinkers.add(new DisposableHolder(disposable));
}
Also used : Disposable(io.reactivex.disposables.Disposable) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) Element(org.jsoup.nodes.Element) DisposableHolder(com.vegen.smartcampus.baseframework.network.DisposableHolder) ArrayList(java.util.ArrayList) NewsList(com.itculturalfestival.smartcampus.entity.NewsList) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Example 39 with ObservableOnSubscribe

use of io.reactivex.ObservableOnSubscribe in project SmartCampus by Vegen.

the class MoreNewsPresenter method getNewsList.

@Override
public void getNewsList(String url, int newsType) {
    Disposable disposable = Observable.create((ObservableOnSubscribe<Document>) e -> {
        Document document = Jsoup.connect(url).get();
        e.onNext(document);
        e.onComplete();
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(document -> {
        Element body = document.body();
        // 下一页的参数
        String __VIEWSTATE = body.select("#__VIEWSTATE").first().attr("value").trim();
        String __VIEWSTATEGENERATOR = body.select("#__VIEWSTATEGENERATOR").first().attr("value").trim();
        String __EVENTVALIDATION = body.select("#__EVENTVALIDATION").first().attr("value").trim();
        Map<String, String> newsForm = new HashMap<>();
        newsForm.put("__VIEWSTATE", __VIEWSTATE);
        newsForm.put("__VIEWSTATEGENERATOR", __VIEWSTATEGENERATOR);
        newsForm.put("__EVENTVALIDATION", __EVENTVALIDATION);
        // 本页的数据
        Element photoList = body.getElementById("ListNews").getElementById("leftbox").getElementById("photolist");
        List<News> newsList = getTagNews(photoList, newsType);
        if (mView != null) {
            mView.nextNewsListForm(newsForm);
            mView.showNewsList(newsList);
            mView.hideLoading(false);
        }
    }, throwable -> {
        if (mView != null) {
            mView.showMessage(HttpError.getErrorMessage(throwable));
            mView.hideLoading(true);
        }
    });
    mHttpLinkers.add(new DisposableHolder(disposable));
}
Also used : Disposable(io.reactivex.disposables.Disposable) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) HashMap(java.util.HashMap) Element(org.jsoup.nodes.Element) News(com.itculturalfestival.smartcampus.entity.News) DisposableHolder(com.vegen.smartcampus.baseframework.network.DisposableHolder) Document(org.jsoup.nodes.Document)

Example 40 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)

Aggregations

ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)60 ObservableEmitter (io.reactivex.ObservableEmitter)50 Disposable (io.reactivex.disposables.Disposable)36 Observable (io.reactivex.Observable)14 ArrayList (java.util.ArrayList)12 List (java.util.List)12 SupportSQLiteDatabase (androidx.sqlite.db.SupportSQLiteDatabase)9 File (java.io.File)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