Search in sources :

Example 1 with NaviJsonBean

use of com.example.jingbin.cloudreader.bean.wanandroid.NaviJsonBean in project CloudReader by youlookwhat.

the class NavigationViewModel method getNavigationJson.

public void getNavigationJson() {
    Disposable subscribe = HttpClient.Builder.getWanAndroidServer().getNaviJson().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<NaviJsonBean>() {

        @Override
        public void accept(NaviJsonBean naviJsonBean) throws Exception {
            if (naviJsonBean != null && naviJsonBean.getData() != null && naviJsonBean.getData().size() > 0) {
                // title
                dataTitle.setValue(naviJsonBean.getData());
                // content
                ArrayList<ArticlesBean> list = new ArrayList<>();
                // content部分对应分类的position
                ArrayList<Integer> positions = new ArrayList<>();
                for (int i = 0; i < naviJsonBean.getData().size(); i++) {
                    NaviJsonBean.DataBean dataBean = naviJsonBean.getData().get(i);
                    ArticlesBean bean = new ArticlesBean();
                    bean.setNavigationName(dataBean.getName());
                    positions.add(list.size());
                    if (i != 0) {
                        // 最后一个item可能有一个或两个
                        hashMap.put(list.size() - 1, i - 1);
                        hashMap.put(list.size() - 2, i - 1);
                    }
                    hashMap.put(list.size(), i);
                    list.add(bean);
                    list.addAll(dataBean.getArticles());
                }
                data.setValue(list);
                titlePositions.setValue(positions);
            } else {
                data.setValue(null);
                dataTitle.setValue(null);
            }
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            data.setValue(null);
            dataTitle.setValue(null);
        }
    });
    addDisposable(subscribe);
}
Also used : Disposable(io.reactivex.disposables.Disposable) NaviJsonBean(com.example.jingbin.cloudreader.bean.wanandroid.NaviJsonBean) ArticlesBean(com.example.jingbin.cloudreader.bean.wanandroid.ArticlesBean) ArrayList(java.util.ArrayList)

Aggregations

ArticlesBean (com.example.jingbin.cloudreader.bean.wanandroid.ArticlesBean)1 NaviJsonBean (com.example.jingbin.cloudreader.bean.wanandroid.NaviJsonBean)1 Disposable (io.reactivex.disposables.Disposable)1 ArrayList (java.util.ArrayList)1