Search in sources :

Example 1 with CommonSubscriber

use of com.codeest.geeknews.widget.CommonSubscriber in project GeekNews by codeestX.

the class GoldPresenter method getGoldData.

@Override
public void getGoldData(String type) {
    mType = type;
    currentPage = 0;
    totalList.clear();
    Flowable<List<GoldListBean>> list = mDataManager.fetchGoldList(type, NUM_EACH_PAGE, currentPage++).compose(RxUtil.<GoldHttpResponse<List<GoldListBean>>>rxSchedulerHelper()).compose(RxUtil.<List<GoldListBean>>handleGoldResult());
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, -3);
    Flowable<List<GoldListBean>> hotList = mDataManager.fetchGoldHotList(type, new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()), NUM_HOT_LIMIT).compose(RxUtil.<GoldHttpResponse<List<GoldListBean>>>rxSchedulerHelper()).compose(RxUtil.<List<GoldListBean>>handleGoldResult());
    addSubscribe(Flowable.concat(hotList, list).subscribeWith(new CommonSubscriber<List<GoldListBean>>(mView) {

        @Override
        public void onNext(List<GoldListBean> goldListBean) {
            if (isHotList) {
                isHotList = false;
                totalList.addAll(goldListBean);
            } else {
                isHotList = true;
                totalList.addAll(goldListBean);
                mView.showContent(totalList);
            }
        }
    }));
}
Also used : GoldHttpResponse(com.codeest.geeknews.model.http.response.GoldHttpResponse) GoldListBean(com.codeest.geeknews.model.bean.GoldListBean) CommonSubscriber(com.codeest.geeknews.widget.CommonSubscriber) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) List(java.util.List) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with CommonSubscriber

use of com.codeest.geeknews.widget.CommonSubscriber in project GeekNews by codeestX.

the class VtexPresenter method getContent.

@Override
public void getContent(String type) {
    addSubscribe(Flowable.just(VtexApis.TAB_HOST + type).subscribeOn(Schedulers.io()).map(new Function<String, Document>() {

        @Override
        public Document apply(String s) {
            try {
                return Jsoup.connect(s).timeout(10000).get();
            } catch (IOException e) {
                LogUtil.d(e.toString());
                e.printStackTrace();
            }
            return null;
        }
    }).filter(new Predicate<Document>() {

        @Override
        public boolean test(@NonNull Document document) throws Exception {
            return document != null;
        }
    }).map(new Function<Document, List<TopicListBean>>() {

        @Override
        public List<TopicListBean> apply(Document doc) {
            List<TopicListBean> mList = new ArrayList<>();
            // item根节点
            Elements itemElements = doc.select("div.cell.item");
            int count = itemElements.size();
            for (int i = 0; i < count; i++) {
                // 标题
                Elements titleElements = itemElements.get(i).select("div.cell.item table tr td span.item_title > a");
                // 头像
                Elements imgElements = itemElements.get(i).select("div.cell.item table tr td img.avatar");
                // 节点
                Elements nodeElements = itemElements.get(i).select("div.cell.item table tr span.small.fade a.node");
                // 评论数
                Elements commentElements = itemElements.get(i).select("div.cell.item table tr a.count_livid");
                // 作者 & 最后回复
                Elements nameElements = itemElements.get(i).select("div.cell.item table tr span.small.fade strong a");
                // 更新时间
                Elements timeElements = itemElements.get(i).select("div.cell.item table tr span.small.fade");
                TopicListBean bean = new TopicListBean();
                if (titleElements.size() > 0) {
                    bean.setTitle(titleElements.get(0).text());
                    bean.setTopicId(parseId(titleElements.get(0).attr("href")));
                }
                if (imgElements.size() > 0) {
                    bean.setImgUrl(parseImg(imgElements.get(0).attr("src")));
                }
                if (nodeElements.size() > 0) {
                    bean.setNode(nodeElements.get(0).text());
                }
                if (nameElements.size() > 0) {
                    bean.setName(nameElements.get(0).text());
                }
                // 存在没有 最后回复者、评论数、更新时间的情况
                if (nameElements.size() > 1) {
                    bean.setLastUser(nameElements.get(1).text());
                }
                if (commentElements.size() > 0) {
                    bean.setCommentNum(Integer.valueOf(commentElements.get(0).text()));
                }
                if (timeElements.size() > 1) {
                    bean.setUpdateTime(parseTime(timeElements.get(1).text()));
                }
                mList.add(bean);
            }
            return mList;
        }
    }).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new CommonSubscriber<List<TopicListBean>>(mView) {

        @Override
        public void onNext(List<TopicListBean> mList) {
            mView.showContent(mList);
        }
    }));
}
Also used : CommonSubscriber(com.codeest.geeknews.widget.CommonSubscriber) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) TopicListBean(com.codeest.geeknews.model.bean.TopicListBean) IOException(java.io.IOException) Function(io.reactivex.functions.Function) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with CommonSubscriber

use of com.codeest.geeknews.widget.CommonSubscriber in project GeekNews by codeestX.

the class TechPresenter method getSearchTechData.

private void getSearchTechData() {
    currentPage = 1;
    addSubscribe(mDataManager.fetchGankSearchList(queryStr, currentTech, NUM_OF_PAGE, currentPage).compose(RxUtil.<GankHttpResponse<List<GankSearchItemBean>>>rxSchedulerHelper()).compose(RxUtil.<List<GankSearchItemBean>>handleResult()).map(new Function<List<GankSearchItemBean>, List<GankItemBean>>() {

        @Override
        public List<GankItemBean> apply(List<GankSearchItemBean> gankSearchItemBeen) {
            List<GankItemBean> newList = new ArrayList<>();
            for (GankSearchItemBean item : gankSearchItemBeen) {
                GankItemBean bean = new GankItemBean();
                bean.set_id(item.getGanhuo_id());
                bean.setDesc(item.getDesc());
                bean.setPublishedAt(item.getPublishedAt());
                bean.setWho(item.getWho());
                bean.setUrl(item.getUrl());
                newList.add(bean);
            }
            return newList;
        }
    }).subscribeWith(new CommonSubscriber<List<GankItemBean>>(mView) {

        @Override
        public void onNext(List<GankItemBean> gankItemBeen) {
            mView.showContent(gankItemBeen);
        }
    }));
}
Also used : CommonSubscriber(com.codeest.geeknews.widget.CommonSubscriber) ArrayList(java.util.ArrayList) List(java.util.List) GankItemBean(com.codeest.geeknews.model.bean.GankItemBean) GankSearchItemBean(com.codeest.geeknews.model.bean.GankSearchItemBean)

Aggregations

CommonSubscriber (com.codeest.geeknews.widget.CommonSubscriber)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 GankItemBean (com.codeest.geeknews.model.bean.GankItemBean)1 GankSearchItemBean (com.codeest.geeknews.model.bean.GankSearchItemBean)1 GoldListBean (com.codeest.geeknews.model.bean.GoldListBean)1 TopicListBean (com.codeest.geeknews.model.bean.TopicListBean)1 GoldHttpResponse (com.codeest.geeknews.model.http.response.GoldHttpResponse)1 Function (io.reactivex.functions.Function)1 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Calendar (java.util.Calendar)1 Document (org.jsoup.nodes.Document)1 Elements (org.jsoup.select.Elements)1