Search in sources :

Example 1 with RetryWithDelay

use of me.jessyan.rxerrorhandler.handler.RetryWithDelay in project MVPArms by JessYanCoding.

the class UserPresenter method requestUsers.

public void requestUsers(final boolean pullToRefresh) {
    //请求外部存储权限用于适配android6.0的权限管理机制
    PermissionUtil.externalStorage(new PermissionUtil.RequestPermission() {

        @Override
        public void onRequestPermissionSuccess() {
        //request permission success, do something.
        }
    }, mRootView.getRxPermissions(), mRootView, mErrorHandler);
    //上拉刷新默认只请求第一页
    if (pullToRefresh)
        lastUserId = 1;
    //关于RxCache缓存库的使用请参考 http://www.jianshu.com/p/b58ef6b0624b
    //是否驱逐缓存,为ture即不使用缓存,每次上拉刷新即需要最新数据,则不使用缓存
    boolean isEvictCache = pullToRefresh;
    if (pullToRefresh && isFirst) {
        //默认在第一次上拉刷新时使用缓存
        isFirst = false;
        isEvictCache = false;
    }
    mModel.getUsers(lastUserId, isEvictCache).subscribeOn(Schedulers.io()).retryWhen(//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
    new RetryWithDelay(3, 2)).doOnSubscribe(new Action0() {

        @Override
        public void call() {
            if (pullToRefresh)
                //显示上拉刷新的进度条
                mRootView.showLoading();
            else
                //显示下拉加载更多的进度条
                mRootView.startLoadMore();
        }
    }).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread()).doAfterTerminate(new Action0() {

        @Override
        public void call() {
            if (pullToRefresh)
                //隐藏上拉刷新的进度条
                mRootView.hideLoading();
            else
                //隐藏下拉加载更多的进度条
                mRootView.endLoadMore();
        }
    }).compose(//使用RXlifecycle,使subscription和activity一起销毁
    RxUtils.<List<User>>bindToLifecycle(mRootView)).subscribe(new ErrorHandleSubscriber<List<User>>(mErrorHandler) {

        @Override
        public void onNext(List<User> users) {
            //记录最后一个id,用于下一次请求
            lastUserId = users.get(users.size() - 1).getId();
            //如果是上拉刷新则清空列表
            if (pullToRefresh)
                mUsers.clear();
            for (User user : users) {
                mUsers.add(user);
            }
            //通知更新数据
            mAdapter.notifyDataSetChanged();
        }
    });
}
Also used : Action0(rx.functions.Action0) User(me.jessyan.mvparms.demo.mvp.model.entity.User) ArrayList(java.util.ArrayList) List(java.util.List) RetryWithDelay(me.jessyan.rxerrorhandler.handler.RetryWithDelay) PermissionUtil(com.jess.arms.utils.PermissionUtil)

Aggregations

PermissionUtil (com.jess.arms.utils.PermissionUtil)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 User (me.jessyan.mvparms.demo.mvp.model.entity.User)1 RetryWithDelay (me.jessyan.rxerrorhandler.handler.RetryWithDelay)1 Action0 (rx.functions.Action0)1