Search in sources :

Example 1 with ObservableSource

use of io.reactivex.ObservableSource in project android-mvp-architecture by MindorksOpenSource.

the class AppDataManager method seedDatabaseOptions.

@Override
public Observable<Boolean> seedDatabaseOptions() {
    GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation();
    final Gson gson = builder.create();
    return mDbHelper.isOptionEmpty().concatMap(new Function<Boolean, ObservableSource<? extends Boolean>>() {

        @Override
        public ObservableSource<? extends Boolean> apply(Boolean isEmpty) throws Exception {
            if (isEmpty) {
                Type type = new TypeToken<List<Option>>() {
                }.getType();
                List<Option> optionList = gson.fromJson(CommonUtils.loadJSONFromAsset(mContext, AppConstants.SEED_DATABASE_OPTIONS), type);
                return saveOptionList(optionList);
            }
            return Observable.just(false);
        }
    });
}
Also used : Type(java.lang.reflect.Type) GsonBuilder(com.google.gson.GsonBuilder) ObservableSource(io.reactivex.ObservableSource) TypeToken(com.google.gson.reflect.TypeToken) Gson(com.google.gson.Gson) Option(com.mindorks.framework.mvp.data.db.model.Option) List(java.util.List)

Example 2 with ObservableSource

use of io.reactivex.ObservableSource in project RxJavaInAction by fengzhizi715.

the class FlatMapForParallel3 method main.

public static void main(String[] args) {
    int threadNum = Runtime.getRuntime().availableProcessors() + 1;
    final ExecutorService executor = Executors.newFixedThreadPool(threadNum);
    final Scheduler scheduler = Schedulers.from(executor);
    Observable.range(1, 100).flatMap(new Function<Integer, ObservableSource<String>>() {

        @Override
        public ObservableSource<String> apply(Integer integer) throws Exception {
            return Observable.just(integer).subscribeOn(scheduler).map(new Function<Integer, String>() {

                @Override
                public String apply(Integer integer) throws Exception {
                    return integer.toString();
                }
            });
        }
    }).doFinally(new Action() {

        @Override
        public void run() throws Exception {
            executor.shutdown();
        }
    }).subscribe(new Consumer<String>() {

        @Override
        public void accept(String str) throws Exception {
            System.out.println(str);
        }
    });
}
Also used : Function(io.reactivex.functions.Function) Action(io.reactivex.functions.Action) ObservableSource(io.reactivex.ObservableSource) Scheduler(io.reactivex.Scheduler) ExecutorService(java.util.concurrent.ExecutorService)

Example 3 with ObservableSource

use of io.reactivex.ObservableSource in project atlas by alibaba.

the class MergeExcutorServices method main.

public static void main(String[] args) throws InterruptedException {
    final MergeObject mergeTask = new MergeObject(null, null, null);
    final MergeObject mergeTask1 = new MergeObject(null, null, null);
    String[] aa = new String[] { "a", "b", "c", "d", "e" };
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    Observable.fromArray(aa).flatMap(new Function<String, ObservableSource<String>>() {

        @Override
        public ObservableSource<String> apply(String s) throws Exception {
            return Observable.just(s).map(new Function<String, String>() {

                @Override
                public String apply(String s) throws Exception {
                    return s + s;
                }
            }).subscribeOn(Schedulers.computation());
        }
    }).observeOn(Schedulers.newThread()).subscribe(new Observer<String>() {

        @Override
        public void onSubscribe(Disposable d) {
        }

        @Override
        public void onNext(String value) {
            System.out.println(value);
            System.out.println("xxxx");
        }

        @Override
        public void onError(Throwable e) {
            System.out.println("onError");
        }

        @Override
        public void onComplete() {
            System.out.println("onComplete");
            countDownLatch.countDown();
        }
    });
    countDownLatch.await();
// Observable.just(1,2,3,4).doOnSubscribe(new Consumer<Disposable>() {
// @Override
// public void accept(Disposable disposable) throws Exception {
// System.out.println("doOnSubscribe0 +"+Thread.currentThread().getName());
// 
// }
// }).map(new Function<Integer, Integer>() {
// 
// @Override
// public Integer apply(Integer integer) throws Exception {
// System.out.println("map0 +"+Thread.currentThread().getName());
// 
// return integer;
// }
// }).subscribeOn(Schedulers.newThread()).map(new Function<Integer, Integer>() {
// @Override
// public Integer apply(Integer integer) throws Exception {
// System.out.println("map +"+Thread.currentThread().getName());
// return integer+1;
// }
// }).subscribeOn(Schedulers.computation()).map(new Function<Integer, Integer>() {
// @Override
// public Integer apply(Integer integer) throws Exception {
// System.out.println("map5 +"+Thread.currentThread().getName());
// 
// return integer;
// }
// }).doOnSubscribe(new Consumer<Disposable>() {
// @Override
// public void accept(Disposable disposable) throws Exception {
// System.out.println("doOnSubscribe +"+Thread.currentThread().getName());
// 
// 
// }
// }).subscribeOn(Schedulers.io()).observeOn(Schedulers.single()).doOnSubscribe(new Consumer<Disposable>() {
// @Override
// public void accept(Disposable disposable) throws Exception {
// System.out.println("doOnSubscribe1 +"+Thread.currentThread().getName());
// 
// }
// }).subscribeOn(Schedulers.computation()).map(new Function<Integer, Integer>() {
// @Override
// public Integer apply(Integer integer) throws Exception {
// System.out.println("map1 +"+Thread.currentThread().getName());
// 
// return integer+1;
// }
// }).observeOn(Schedulers.computation()).map(new Function<Integer, Integer>() {
// @Override
// public Integer apply(Integer integer) throws Exception {
// System.out.println("map3 +"+Thread.currentThread().getName());
// 
// return integer;
// }
// }).blockingSubscribe(new Consumer<Integer>() {
// @Override
// public void accept(Integer integer) throws Exception {
// System.out.println("Consumer +"+integer+Thread.currentThread().getName());
// 
// }
// });
// Set<String>set = new HashSet<>();
// set.add("a");
// set.add("b");
// set.add("f");
// set.add("c");
// System.out.println(set.toArray()[0]);
// Thread.sleep(5000);
}
Also used : Disposable(io.reactivex.disposables.Disposable) ObservableSource(io.reactivex.ObservableSource) CountDownLatch(java.util.concurrent.CountDownLatch) RemoteException(android.os.RemoteException) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with ObservableSource

use of io.reactivex.ObservableSource in project EnableHands by LeviWGG.

the class NewsModel method getNewsInfo.

@Override
public void getNewsInfo(final INewsContract.INewsPresenter iNewsPresenter, int id) {
    Retrofit retrofit = new ServiceFactory().create(NewsService.BASE_URL);
    NewsService newsService = retrofit.create(NewsService.class);
    Observable<NewsInfo> observable = newsService.getNewsInfo(id);
    observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).flatMap(new Function<NewsInfo, ObservableSource<List<NewsInfo.T1348647909107Bean>>>() {

        @Override
        public ObservableSource<List<NewsInfo.T1348647909107Bean>> apply(NewsInfo newsInfo) throws Exception {
            return Observable.just(newsInfo.getT1348647909107());
        }
    }).filter(new Predicate<List<NewsInfo.T1348647909107Bean>>() {

        @Override
        public boolean test(List<NewsInfo.T1348647909107Bean> t1348647909107Beans) throws Exception {
            if (t1348647909107Beans.isEmpty()) {
                return false;
            }
            return true;
        }
    }).subscribe(new Consumer<List<NewsInfo.T1348647909107Bean>>() {

        @Override
        public void accept(List<NewsInfo.T1348647909107Bean> t1348647909107Beans) throws Exception {
            // 遍历新闻信息,去掉没有URL的子项
            for (int i = 0; i < t1348647909107Beans.size(); i++) {
                if (t1348647909107Beans.get(i).getUrl() == null) {
                    t1348647909107Beans.remove(i);
                }
            }
            iNewsPresenter.setNewsInfo(t1348647909107Beans);
        }
    });
// return (Observable<T>) observable;
}
Also used : ServiceFactory(app.main.wangliwei.enablehands.http.ServiceFactory) Predicate(io.reactivex.functions.Predicate) Retrofit(retrofit2.Retrofit) ObservableSource(io.reactivex.ObservableSource) NewsInfo(app.main.wangliwei.enablehands.bean.NewsInfo) NewsService(app.main.wangliwei.enablehands.http.NewsService) List(java.util.List)

Example 5 with ObservableSource

use of io.reactivex.ObservableSource in project EnableHands by LeviWGG.

the class PicturePresenterImp method getWeixinNews.

@Override
public void getWeixinNews() {
    Retrofit retrofit = new ServiceFactory().create(BASE_URL);
    WeixinService weixinService = retrofit.create(WeixinService.class);
    Observable<Weixin> observable = weixinService.getWeixinNews(page, 10, "json", KEY);
    observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).flatMap(new Function<Weixin, ObservableSource<List<Weixin.ResultBean.ListBean>>>() {

        @Override
        public ObservableSource<List<Weixin.ResultBean.ListBean>> apply(Weixin weixin) throws Exception {
            return Observable.just(weixin.getResult().getList());
        }
    }).filter(new Predicate<List<Weixin.ResultBean.ListBean>>() {

        @Override
        public boolean test(List<Weixin.ResultBean.ListBean> listBeans) throws Exception {
            if (listBeans.isEmpty()) {
                return false;
            }
            return true;
        }
    }).subscribe(new Consumer<List<Weixin.ResultBean.ListBean>>() {

        @Override
        public void accept(List<Weixin.ResultBean.ListBean> listBeans) throws Exception {
            if (page == 1) {
                mView.setWeixinNews(listBeans);
            } else {
                mView.loadMore(listBeans);
            }
            page++;
        }
    });
}
Also used : ServiceFactory(app.main.wangliwei.enablehands.http.ServiceFactory) Predicate(io.reactivex.functions.Predicate) Retrofit(retrofit2.Retrofit) Weixin(app.main.wangliwei.enablehands.bean.Weixin) WeixinService(app.main.wangliwei.enablehands.http.WeixinService) ObservableSource(io.reactivex.ObservableSource) List(java.util.List)

Aggregations

ObservableSource (io.reactivex.ObservableSource)8 List (java.util.List)4 Predicate (io.reactivex.functions.Predicate)3 ServiceFactory (app.main.wangliwei.enablehands.http.ServiceFactory)2 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 Disposable (io.reactivex.disposables.Disposable)2 Type (java.lang.reflect.Type)2 Retrofit (retrofit2.Retrofit)2 RemoteException (android.os.RemoteException)1 NonNull (android.support.annotation.NonNull)1 NewsInfo (app.main.wangliwei.enablehands.bean.NewsInfo)1 Weixin (app.main.wangliwei.enablehands.bean.Weixin)1 NewsService (app.main.wangliwei.enablehands.http.NewsService)1 WeixinService (app.main.wangliwei.enablehands.http.WeixinService)1 TypeToken (com.google.gson.reflect.TypeToken)1 Option (com.mindorks.framework.mvp.data.db.model.Option)1 Question (com.mindorks.framework.mvp.data.db.model.Question)1 BarCode (com.nytimes.android.external.store3.base.impl.BarCode)1 RedditData (com.nytimes.android.sample.data.model.RedditData)1