nilupeng
2022-08-11 6c29fda2b7408c13a486554c5be9d3c6b8296b95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.runt.open.mvvm.base.model;
 
import androidx.lifecycle.MutableLiveData;
 
import com.runt.open.mvvm.data.HttpApiResult;
import com.runt.open.mvvm.data.PageResult;
import com.runt.open.mvvm.retrofit.observable.HttpObserver;
 
import java.util.ArrayList;
 
import io.reactivex.Observable;
 
/**
 * 分页
 * Created by Administrator on 2021/11/3 0003.
 */
public abstract class LoadPageViewModel<D> extends BaseViewModel {
 
    public final int SIZE = 10;
    private MutableLiveData<ArrayList<D>> liveData = new MutableLiveData<>();
    private MutableLiveData liveFailed = new MutableLiveData();
 
    /**
     * 请求地址
     * @param page      页数
     * @param objects   参数
     * @return
     */
    public abstract Observable<HttpApiResult<PageResult<D>>> request(int page,Object... objects);
 
    /**
     * 数据请求
     */
    public void requestData(Observable<HttpApiResult<PageResult<D>>> observable){
        httpObserverOn( observable, new PageHttpObserver());
    }
 
    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);
        }
    }
 
}