| | |
| | | package com.runt.open.mvvm.base.model; |
| | | |
| | | import androidx.lifecycle.MutableLiveData; |
| | | |
| | | import com.google.gson.Gson; |
| | | import com.runt.open.mvvm.data.HttpApiResult; |
| | | import com.runt.open.mvvm.data.PageResult; |
| | | import com.runt.open.mvvm.retrofit.observable.HttpObserver; |
| | | import org.json.JSONObject; |
| | | |
| | | import java.lang.reflect.ParameterizedType; |
| | | import java.util.ArrayList; |
| | | |
| | | import io.reactivex.Observable; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 分页 |
| | |
| | | |
| | | /** |
| | | * 请求地址 |
| | | * @param page 页数 |
| | | * @param objects 参数 |
| | | * @return |
| | | */ |
| | | public abstract Observable<HttpApiResult<PageResult<D>>> request(int page,Object... objects); |
| | | protected abstract String requestUrl(); |
| | | |
| | | /** |
| | | * 数据请求 |
| | | */ |
| | | public void requestData(Observable<HttpApiResult<PageResult<D>>> observable){ |
| | | httpObserverOn( observable, new PageHttpObserver()); |
| | | } |
| | | /** |
| | | * 数据请求 |
| | | * @param page 页数 |
| | | * @param param 请求参数 |
| | | */ |
| | | public void requestData(int page, Map param){ |
| | | final ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass(); |
| | | Class<D> entityClass = (Class<D>) type.getActualTypeArguments()[0]; |
| | | httpObserverOn( commonApi.getPageData(requestUrl(), page, SIZE, param), new HttpObserver<PageResult>() { |
| | | @Override |
| | | protected void onSuccess(PageResult data) { |
| | | //数据转换 |
| | | ArrayList<D> list = new ArrayList<>(); |
| | | for(Object map : data.rows){ |
| | | list.add(new Gson().fromJson(new JSONObject((Map) map).toString(),entityClass)); |
| | | } |
| | | liveData.postValue(list); |
| | | } |
| | | |
| | | @Override |
| | | protected void onFailed(HttpApiResult httpResult) { |
| | | mActivity.showToast(httpResult.msg); |
| | | liveFailed.postValue(1); |
| | | } |
| | | }); |
| | | } |
| | | public MutableLiveData<ArrayList<D>> getLiveData(){ |
| | | return liveData; |
| | | } |