Linjiajia
2023-07-25 82e57df230ecb744af6c8865f80870ba03c86d89
app/src/main/java/com/application/zhangshi_app_android/ui/function/LittleDoctorActivityViewModel.java
@@ -12,22 +12,20 @@
import com.android.app_base.base.viewmodel.BaseViewModel;
import com.android.app_base.http.ResultData;
import com.android.app_base.utils.RxUtils;
import com.application.zhangshi_app_android.bean.FamilyMemorabiliaRequestBean;
import com.application.zhangshi_app_android.bean.FamilyMemorabiliaResponseBean;
import com.application.zhangshi_app_android.bean.HomeDevicesResponseBean;
import com.application.zhangshi_app_android.bean.HonorCollectionBean;
import com.application.zhangshi_app_android.bean.HonorCollectionResponseBean;
import com.application.zhangshi_app_android.bean.LittleDoctorBean;
import com.application.zhangshi_app_android.bean.LittleDoctorRequestBean;
import com.application.zhangshi_app_android.bean.LittleDoctorResponseBean;
import com.application.zhangshi_app_android.bean.PageResponseBean;
import com.application.zhangshi_app_android.data.DataRepository;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
/**
 * @author Ljj
@@ -36,9 +34,13 @@
 */
public class LittleDoctorActivityViewModel extends BaseViewModel<DataRepository> {
    private MutableLiveData<List<LittleDoctorBean>> dataListLiveData;
    private MutableLiveData<List<LittleDoctorBean>> dataListLiveData;//获取的数据
    private MutableLiveData<List<LittleDoctorBean>> checkListLiveData;//选中的数据
    private MutableLiveData<List<LittleDoctorBean>> moreListLiveData;//下拉加载数据
    private MutableLiveData<LittleDoctorRequestBean> requestBeanLiveData;//请求参数
    private MutableLiveData<List<LittleDoctorBean>> finishDeleteListLiveData;//已经完成删除的数据,用于adapter的remove操作
    private LittleDoctorRequestBean requestBean = new LittleDoctorRequestBean();
    private MutableLiveData<Integer> operateTypeLiveData;//操作类型 0 选择操作 1.完成 2.删除
    public LittleDoctorActivityViewModel(@NonNull Application application) {
        super(application);
@@ -48,29 +50,25 @@
    protected DataRepository initModel() {
        return DataRepository.getInstance();
    }
    public MutableLiveData<List<LittleDoctorBean>> getDataListLiveData() {
        if (dataListLiveData == null){
            dataListLiveData = new MutableLiveData<>();
        }
        return dataListLiveData;
    }
    public void setDataListLiveData(MutableLiveData<List<LittleDoctorBean>> dataListLiveData) {
        this.dataListLiveData = dataListLiveData;
    }
    /**
     * 获取数据
     */
    public void getLittleDoctors() {
        model.getLittleDoctors(requestBean.toMap())
        if (getRequestBeanLiveData().getValue() == null){
            getRequestBeanLiveData().setValue(new LittleDoctorRequestBean());
        }
        getRequestBeanLiveData().getValue().setPageNum(1);
        model.getLittleDoctors(getRequestBeanLiveData().getValue().toMap())
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new Observer<ResultData<LittleDoctorResponseBean>>() {
                .subscribe(new Observer<ResultData<PageResponseBean<LittleDoctorBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addSubscribe(d);
                    }
                    @Override
                    public void onNext(ResultData<LittleDoctorResponseBean> data) {
                    public void onNext(ResultData<PageResponseBean<LittleDoctorBean>> data) {
                        if (data.getCode() == CODE_SUCCESS){
                            if (data.getData().getData().isEmpty()){
                                changeStateView(StateViewEnum.DATA_NULL);
@@ -94,6 +92,206 @@
                    }
                });
    }
    /**
     * 加载更多
     */
    public void getMoreLittleDoctors() {
        if (getRequestBeanLiveData().getValue() == null){
            getRequestBeanLiveData().setValue(new LittleDoctorRequestBean());
        }
        getRequestBeanLiveData().getValue().setPageNum(getRequestBeanLiveData().getValue().getPageNum() + 1);
        model.getLittleDoctors(getRequestBeanLiveData().getValue().toMap())
                .compose(RxUtils.schedulersTransformer())
                .doFinally(new Action() {
                    @Override
                    public void run() throws Exception {
                        changeStateView(StateViewEnum.DATA_FINISH);
                    }
                })
                .subscribe(new Observer<ResultData<PageResponseBean<LittleDoctorBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addSubscribe(d);
                    }
                    @Override
                    public void onNext(ResultData<PageResponseBean<LittleDoctorBean>> data) {
                        if (data.getCode() == CODE_SUCCESS){
                            if (data.getData().getData().isEmpty()){
                                messageLiveData.postValue("没有更多数据了");
                            }else {
                                changeStateView(StateViewEnum.HIDE);
                                moreListLiveData.postValue(data.getData().getData());
                            }
                        }else {
                            messageLiveData.postValue(data.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        messageLiveData.postValue(e.getMessage());
                    }
                    @Override
                    public void onComplete() {
                    }
                });
    }
    /**
     * 删除多选选中的数据
     */
    public void deleteCheckList() {
        List<LittleDoctorBean> deleteList = getCheckListLiveData().getValue();
        if (deleteList == null){
            return;
        }
        StringBuffer sb = new StringBuffer();
        deleteList.forEach(LittleDoctorBean -> sb.append(LittleDoctorBean.getId()).append(","));
        sb.deleteCharAt(sb.length()-1);
        model.deleteLittleDoctors(sb.toString())
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new Observer<ResultData<String>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addSubscribe(d);
                    }
                    @Override
                    public void onNext(ResultData<String> stringResultData) {
                        if (stringResultData.getCode() == CODE_SUCCESS){
                            finishDeleteListLiveData.postValue(checkListLiveData.getValue());
                            messageLiveData.postValue("删除成功");
                            getCheckListLiveData().postValue(new ArrayList<>());
                        }else {
                            messageLiveData.postValue(stringResultData.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        messageLiveData.postValue(e.getMessage());
                    }
                    @Override
                    public void onComplete() {
                    }
                });
    }
    /**
     * 删除单个数据
     */
    public void deleteItem(LittleDoctorBean littleDoctorBean) {
        model.deleteLittleDoctors(String.valueOf(littleDoctorBean.getId()))
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new Observer<ResultData<String>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addSubscribe(d);
                    }
                    @Override
                    public void onNext(ResultData<String> stringResultData) {
                        if (stringResultData.getCode() == CODE_SUCCESS){
                            List<LittleDoctorBean> list = new ArrayList<>();
                            list.add(littleDoctorBean);
                            finishDeleteListLiveData.postValue(list);
                            messageLiveData.postValue("删除成功");
                        }else {
                            messageLiveData.postValue(stringResultData.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        messageLiveData.postValue(e.getMessage());
                    }
                    @Override
                    public void onComplete() {
                    }
                });
    }
    public MutableLiveData<List<LittleDoctorBean>> getCheckListLiveData() {
        if (checkListLiveData==null){
            checkListLiveData = new MutableLiveData<>();
            checkListLiveData.postValue(new ArrayList<>());
        }
        if (checkListLiveData.getValue() == null){
            checkListLiveData.postValue(new ArrayList<>());
        }
        return checkListLiveData;
    }
    public void setCheckListLiveData(MutableLiveData<List<LittleDoctorBean>> checkListLiveData) {
        this.checkListLiveData = checkListLiveData;
    }
    @NonNull
    public MutableLiveData<LittleDoctorRequestBean> getRequestBeanLiveData() {
        if (requestBeanLiveData == null){
            requestBeanLiveData = new MutableLiveData<>();
        }
        if (requestBeanLiveData.getValue() == null){
            requestBeanLiveData.setValue(new LittleDoctorRequestBean());
        }
        return requestBeanLiveData;
    }
    public void setRequestBeanLiveData(MutableLiveData<LittleDoctorRequestBean> requestBeanLiveData) {
        this.requestBeanLiveData = requestBeanLiveData;
    }
    public MutableLiveData<List<LittleDoctorBean>> getDataListLiveData() {
        if (dataListLiveData == null){
            dataListLiveData = new MutableLiveData<>();
        }
        return dataListLiveData;
    }
    public void setDataListLiveData(MutableLiveData<List<LittleDoctorBean>> dataListLiveData) {
        this.dataListLiveData = dataListLiveData;
    }
    @NonNull
    public MutableLiveData<List<LittleDoctorBean>> getMoreListLiveData() {
        if (moreListLiveData == null){
            moreListLiveData = new MutableLiveData<>();
        }
        return moreListLiveData;
    }
    public void setMoreListLiveData(MutableLiveData<List<LittleDoctorBean>> moreListLiveData) {
        this.moreListLiveData = moreListLiveData;
    }
    @NonNull
    public MutableLiveData<Integer> getOperateTypeLiveData() {
        if (operateTypeLiveData == null){
            operateTypeLiveData = new MutableLiveData<>();
            operateTypeLiveData.setValue(0);
        }
        return operateTypeLiveData;
    }
    public void setOperateTypeLiveData(MutableLiveData<Integer> operateTypeLiveData) {
        this.operateTypeLiveData = operateTypeLiveData;
    }
    public LiveData<List<LittleDoctorBean>> getFinishDeleteListLiveData() {
        if (finishDeleteListLiveData == null){
            finishDeleteListLiveData = new MutableLiveData<>();
        }
        return finishDeleteListLiveData;
    }
    public void setFinishDeleteListLiveData(MutableLiveData<List<LittleDoctorBean>> finishDeleteListLiveData) {
        this.finishDeleteListLiveData = finishDeleteListLiveData;
    }
}