美文网首页Android-Jetpack
LiveData+ViewModule 在项目中的结合使用

LiveData+ViewModule 在项目中的结合使用

作者: 程序员阿兵 | 来源:发表于2019-12-27 17:12 被阅读0次

引言 :

结合ViewModule 进行简单封装

  • 1.结合rxjava 对网络请求 成功后 liveData.postValue 形式回调
public abstract class JetNetLiveDataMap<ResultType> {

    private LiveData<DataWrapper<ResultType>> mLiveData;
    private Disposable mDisposable;

    public JetNetLiveDataMap() {
        this.mLiveData = fetchAsyncLiveData();
    }

    private LiveData<DataWrapper<ResultType>> fetchAsyncLiveData() {
        MutableLiveData<DataWrapper<ResultType>> liveData = new MutableLiveData<>();
        mDisposable = createCall().subscribeWith(new ResultObserver<ResultEntity<ResultType>>() {

            @Override
            protected void onHandleSuccess(ResultEntity<ResultType> resultEntity) {
                DataWrapper<ResultType> typeDataWrapper = new DataWrapper<>();
                typeDataWrapper.setStatus(Status.SUCCESS);
                typeDataWrapper.setData(resultEntity.data);
                liveData.postValue(typeDataWrapper);
            }

            @Override
            protected void onHandleError(NetThrowable exception) {
                super.onHandleError(exception);
                DataWrapper<ResultType> typeDataWrapper = new DataWrapper<>();
                typeDataWrapper.setStatus(Status.FAIL);
                typeDataWrapper.setNetException(exception);
                //请求成功后的数据 liveData 发送
                liveData.postValue(typeDataWrapper);
            }

        });
        return liveData;
    }


    public Disposable getDisposable() {
        return mDisposable;
    }

    public LiveData<DataWrapper<ResultType>> asLiveData() {
        return mLiveData;
    }

    protected abstract Observable<ResultEntity<ResultType>> createCall();
}
  • 2 定义简单契约
public interface IFollowContract {
    interface IFollowViewModel {
        LiveData<DataWrapper<BaseEntity>> followUser(String userId);
    }
}
  • 3 实现契约 将网络请求后的observer 回调 ==== 即createCall
/**
 * @author 桂雁彬
 * @date 2019-12-18.
 * GitHub:
 * email:guiyanbing@100tal.com
 * description:关注 取消关注
 */
public class FollowViewModel extends BaseViewModel implements IFollowContract.IFollowViewModel {

    @Override
    public LiveData<DataWrapper<BaseEntity>> followUser(String userId) {
        JetNetLiveDataMap<BaseEntity> listASyncLiveDataMap = new JetNetLiveDataMap<BaseEntity>() {
            @Override
            protected Observable<ResultEntity<BaseEntity>> createCall() {
                return RetrofitHelp
                        .getService(IFollowService.class)
                        .followUser(userId)
                        .compose(RxSchedulers.observableCompose());
            }
        };

        addDisposable(listASyncLiveDataMap.getDisposable());
        return listASyncLiveDataMap.asLiveData();
    }


在当前的页面 activity 或者fragment 初始化 ViewModule

followViewModel = ViewModelProviders.of(this).get(FollowViewModel.class);

监听回调的数据

    followViewModel.followUser(String.valueOf(page),String.valueOf(size),followBean.getUserId()).observe(FollowFragment.this, followBeanDataWrapper -> {
                            if (followBeanDataWrapper.getStatus() == Status.SUCCESS) {
                                FollowBean data = followBeanDataWrapper.getData();
                                mRecycleViewRefreshManager.backFillSuccessDataToList(data.getUsers(),
                                        data.isHas_next() ? IStatus.HAVE_MORE : IStatus.NO_MORE);
                            } else {
                                mRecycleViewRefreshManager.backFillFailStatusToList();
                            }
                        });

相关文章

网友评论

    本文标题:LiveData+ViewModule 在项目中的结合使用

    本文链接:https://www.haomeiwen.com/subject/rrvdoctx.html