| | |
| | | import com.runt.open.mvvm.data.PageResult; |
| | | import com.runt.open.mvvm.retrofit.observable.HttpObserver; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.ArrayList; |
| | | |
| | | import io.reactivex.Observable; |
| | | |
| | | /** |
| | | * 分页 |
| | | * Created by Administrator on 2021/11/3 0003. |
| | | */ |
| | | public abstract class LoadPageViewModel<RESULT extends PageResult> extends BaseViewModel { |
| | | public abstract class LoadPageViewModel<D> extends BaseViewModel { |
| | | |
| | | public final int SIZE = 10; |
| | | private MutableLiveData<List> liveData = new MutableLiveData<>(); |
| | | private MutableLiveData<ArrayList<D>> liveData = new MutableLiveData<>(); |
| | | private MutableLiveData liveFailed = new MutableLiveData(); |
| | | |
| | | protected abstract String requestUrl(); |
| | | /** |
| | | * 请求地址 |
| | | * @param page 页数 |
| | | * @param objects 参数 |
| | | * @return |
| | | */ |
| | | public abstract Observable<HttpApiResult<PageResult<D>>> request(int page,Object... objects); |
| | | |
| | | public void requestData(int page,Map param){ |
| | | httpObserverOn( commonApi.getPageData(requestUrl(), page, SIZE, param), new HttpObserver<RESULT>() { |
| | | |
| | | @Override |
| | | protected void onSuccess(RESULT data) { |
| | | liveData.postValue(data.rows); |
| | | } |
| | | |
| | | @Override |
| | | protected void onFailed(HttpApiResult httpResult) { |
| | | mActivity.showToast(httpResult.msg); |
| | | liveFailed.postValue(1); |
| | | } |
| | | }); |
| | | /** |
| | | * 数据请求 |
| | | */ |
| | | public void requestData(Observable<HttpApiResult<PageResult<D>>> observable){ |
| | | httpObserverOn( observable, new PageHttpObserver()); |
| | | } |
| | | |
| | | public MutableLiveData<List> getLiveData(){ |
| | | public MutableLiveData<ArrayList<D>> getLiveData(){ |
| | | return liveData; |
| | | } |
| | | |
| | | public MutableLiveData getLiveFailed() { |
| | | return liveFailed; |
| | | } |
| | | |
| | | public class PageHttpObserver extends HttpObserver<PageResult<D>> { |
| | | @Override |
| | | protected void onSuccess(PageResult<D> data) { |
| | | liveData.postValue(data.rows); |
| | | } |
| | | |
| | | @Override |
| | | protected void onFailed(HttpApiResult httpResult) { |
| | | mActivity.showToast(httpResult.msg); |
| | | liveFailed.postValue(1); |
| | | } |
| | | } |
| | | |
| | | } |