Search in sources :

Example 1 with DuanZiBean

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

the class JokeModel method showQSBKList.

public void showQSBKList(final WanNavigator.JokeModelNavigator listener, int page) {
    Function<QsbkListBean, Observable<List<DuanZiBean>>> func1 = new Function<QsbkListBean, Observable<List<DuanZiBean>>>() {

        @Override
        public Observable<List<DuanZiBean>> apply(QsbkListBean bean) {
            List<DuanZiBean> lists = new ArrayList<>();
            if (bean != null && bean.getItems() != null && bean.getItems().size() > 0) {
                List<QsbkListBean.ItemsBean> items = bean.getItems();
                for (QsbkListBean.ItemsBean bean1 : items) {
                    DuanZiBean duanZiBean = new DuanZiBean();
                    duanZiBean.setContent(bean1.getContent());
                    duanZiBean.setCreateTime(bean1.getPublished_at());
                    QsbkListBean.ItemsBean.TopicBean topic = bean1.getTopic();
                    QsbkListBean.ItemsBean.UserBean user = bean1.getUser();
                    if (topic != null) {
                        duanZiBean.setCategoryName(topic.getContent());
                    }
                    if (user != null) {
                        duanZiBean.setName(user.getLogin());
                        String thumb = user.getThumb();
                        if (!TextUtils.isEmpty(thumb)) {
                            if (!thumb.contains("http")) {
                                StringBuilder stringBuilder = new StringBuilder();
                                stringBuilder.append("http:");
                                stringBuilder.append(thumb);
                                duanZiBean.setAvatarUrl(stringBuilder.toString());
                            } else {
                                duanZiBean.setAvatarUrl(thumb);
                            }
                        }
                    }
                    lists.add(duanZiBean);
                }
            }
            return Observable.just(lists);
        }
    };
    Observer<List<DuanZiBean>> observer = new Observer<List<DuanZiBean>>() {

        @Override
        public void onError(Throwable e) {
            listener.loadFailed();
        }

        @Override
        public void onComplete() {
        }

        @Override
        public void onSubscribe(Disposable d) {
            listener.addSubscription(d);
        }

        @Override
        public void onNext(List<DuanZiBean> lists) {
            listener.loadSuccess(lists);
        }
    };
    HttpClient.Builder.getQSBKServer().getQsbkList(page).flatMap(func1).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(observer);
}
Also used : Disposable(io.reactivex.disposables.Disposable) ArrayList(java.util.ArrayList) QsbkListBean(com.example.jingbin.cloudreader.bean.wanandroid.QsbkListBean) Observable(io.reactivex.Observable) Function(io.reactivex.functions.Function) Observer(io.reactivex.Observer) ArrayList(java.util.ArrayList) List(java.util.List) DuanZiBean(com.example.jingbin.cloudreader.bean.wanandroid.DuanZiBean)

Example 2 with DuanZiBean

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

the class JokeFragment method initRefreshView.

private void initRefreshView() {
    RefreshHelper.initLinear(bindingView.xrvWan, true).setItemAnimator(new DefaultItemAnimator());
    RefreshHelper.setSwipeRefreshView(bindingView.srlWan);
    mAdapter = new JokeAdapter();
    bindingView.xrvWan.setAdapter(mAdapter);
    bindingView.srlWan.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            bindingView.srlWan.postDelayed(() -> {
                viewModel.setRefreshBK(true);
                viewModel.setPage(new Random().nextInt(100));
                viewModel.showQSBKList();
            }, 100);
        }
    });
    bindingView.xrvWan.setOnLoadMoreListener(new ByRecyclerView.OnLoadMoreListener() {

        @Override
        public void onLoadMore() {
            int page = viewModel.getPage();
            viewModel.setPage(++page);
            viewModel.setRefreshBK(false);
            viewModel.showQSBKList();
        }
    });
    bindingView.xrvWan.setOnItemLongClickListener(new ByRecyclerView.OnItemLongClickListener() {

        @Override
        public boolean onLongClick(View v, int position) {
            DialogBuild.showItems(v, mAdapter.getItemData(position).getContent());
            return false;
        }
    });
    viewModel.getData().observe(this, new Observer<List<DuanZiBean>>() {

        @Override
        public void onChanged(@Nullable List<DuanZiBean> duanZiBeans) {
            showContentView();
            if (bindingView.srlWan.isRefreshing()) {
                bindingView.srlWan.setRefreshing(false);
            }
            if (duanZiBeans != null && duanZiBeans.size() > 0) {
                if (viewModel.isRefreshBK()) {
                    mAdapter.setNewData(duanZiBeans);
                } else {
                    mAdapter.addData(duanZiBeans);
                    bindingView.xrvWan.loadMoreComplete();
                }
            } else {
                if (!viewModel.isRefreshBK()) {
                    showError();
                } else {
                    bindingView.xrvWan.loadMoreComplete();
                }
            }
        }
    });
}
Also used : ByRecyclerView(me.jingbin.library.ByRecyclerView) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) ByRecyclerView(me.jingbin.library.ByRecyclerView) View(android.view.View) DefaultItemAnimator(androidx.recyclerview.widget.DefaultItemAnimator) Random(java.util.Random) JokeAdapter(com.example.jingbin.cloudreader.adapter.JokeAdapter) List(java.util.List) DuanZiBean(com.example.jingbin.cloudreader.bean.wanandroid.DuanZiBean)

Aggregations

DuanZiBean (com.example.jingbin.cloudreader.bean.wanandroid.DuanZiBean)2 List (java.util.List)2 View (android.view.View)1 DefaultItemAnimator (androidx.recyclerview.widget.DefaultItemAnimator)1 SwipeRefreshLayout (androidx.swiperefreshlayout.widget.SwipeRefreshLayout)1 JokeAdapter (com.example.jingbin.cloudreader.adapter.JokeAdapter)1 QsbkListBean (com.example.jingbin.cloudreader.bean.wanandroid.QsbkListBean)1 Observable (io.reactivex.Observable)1 Observer (io.reactivex.Observer)1 Disposable (io.reactivex.disposables.Disposable)1 Function (io.reactivex.functions.Function)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 ByRecyclerView (me.jingbin.library.ByRecyclerView)1