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

引言 :

    1. LiveData的使用方式
    1. 手写LiveData
    1. LiveData+ViewModule 在项目中的结合使用

结合ViewModule 进行简单封装

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

    private LiveData> mLiveData;
    private Disposable mDisposable;

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

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

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

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

        });
        return liveData;
    }


    public Disposable getDisposable() {
        return mDisposable;
    }

    public LiveData> asLiveData() {
        return mLiveData;
    }

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

    @Override
    public LiveData> followUser(String userId) {
        JetNetLiveDataMap listASyncLiveDataMap = new JetNetLiveDataMap() {
            @Override
            protected Observable> 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 在项目中的结合使用)