Linjiajia
2023-09-12 efafbbf142c81c233c71de636a2d3ce9dc2124f0
app/src/main/java/com/application/zhangshi_app_android/ui/function/CleanStorageActivityViewModel.java
@@ -1,17 +1,29 @@
package com.application.zhangshi_app_android.ui.function;
import static com.android.app_base.base.BaseConfig.CODE_SUCCESS;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.android.app_base.base.StateViewEnum;
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.CleanStorageBean;
import com.application.zhangshi_app_android.bean.CleanStorageRequestBean;
import com.application.zhangshi_app_android.bean.FamilyMemorabiliaBean;
import com.application.zhangshi_app_android.bean.PageResponseBean;
import com.application.zhangshi_app_android.data.DataRepository;
import java.util.ArrayList;
import java.util.List;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
/**
 * @author Ljj
@@ -20,6 +32,11 @@
 */
public class CleanStorageActivityViewModel extends BaseViewModel<DataRepository> {
    private MutableLiveData<List<CleanStorageBean>> dataListLiveData;
    private MutableLiveData<List<CleanStorageBean>> checkListLiveData;//选中的数据
    private MutableLiveData<List<CleanStorageBean>> moreListLiveData;//下拉加载数据
    private MutableLiveData<CleanStorageRequestBean> requestBeanLiveData;//请求参数
    private MutableLiveData<List<CleanStorageBean>> finishDeleteListLiveData;//已经完成删除的数据,用于adapter的remove操作
    private MutableLiveData<Integer> operateTypeLiveData;//操作类型 0 选择操作 1.完成 2.删除
    public CleanStorageActivityViewModel(@NonNull Application application) {
        super(application);
@@ -30,8 +47,203 @@
        return DataRepository.getInstance();
    }
    /**
     * 获取数据
     */
    public void getCleanStorages() {
        if (getRequestBeanLiveData().getValue() == null){
            getRequestBeanLiveData().setValue(new CleanStorageRequestBean());
        }
        getRequestBeanLiveData().getValue().setPageNum(1);
        model.getCleanStorages(getRequestBeanLiveData().getValue().toMap())
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new Observer<ResultData<PageResponseBean<CleanStorageBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addSubscribe(d);
                    }
                    @Override
                    public void onNext(ResultData<PageResponseBean<CleanStorageBean>> data) {
                        if (data.getCode() == CODE_SUCCESS){
                            if (data.getData().getData().isEmpty()){
                                changeStateView(StateViewEnum.DATA_NULL);
                            }else {
                                changeStateView(StateViewEnum.HIDE);
                                dataListLiveData.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 getMoreCleanStorages() {
        if (getRequestBeanLiveData().getValue() == null){
            getRequestBeanLiveData().setValue(new CleanStorageRequestBean());
        }
        getRequestBeanLiveData().getValue().setPageNum(getRequestBeanLiveData().getValue().getPageNum() + 1);
        model.getCleanStorages(getRequestBeanLiveData().getValue().toMap())
                .compose(RxUtils.schedulersTransformer())
                .doFinally(new Action() {
                    @Override
                    public void run() throws Exception {
                        changeStateView(StateViewEnum.DATA_FINISH);
                    }
                })
                .subscribe(new Observer<ResultData<PageResponseBean<CleanStorageBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addSubscribe(d);
                    }
                    @Override
                    public void onNext(ResultData<PageResponseBean<CleanStorageBean>> 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<CleanStorageBean> deleteList = getCheckListLiveData().getValue();
        if (deleteList == null){
            return;
        }
        StringBuffer sb = new StringBuffer();
        deleteList.forEach(CleanStorageBean -> sb.append(CleanStorageBean.getId()).append(","));
        sb.deleteCharAt(sb.length()-1);
        model.deleteCleanStorages(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(CleanStorageBean CleanStorageBean) {
        model.deleteCleanStorages(String.valueOf(CleanStorageBean.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<CleanStorageBean> list = new ArrayList<>();
                            list.add(CleanStorageBean);
                            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<CleanStorageBean>> 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<CleanStorageBean>> checkListLiveData) {
        this.checkListLiveData = checkListLiveData;
    }
    @NonNull
    public MutableLiveData<CleanStorageRequestBean> getRequestBeanLiveData() {
        if (requestBeanLiveData == null){
            requestBeanLiveData = new MutableLiveData<>();
        }
        if (requestBeanLiveData.getValue() == null){
            requestBeanLiveData.setValue(new CleanStorageRequestBean());
        }
        return requestBeanLiveData;
    }
    public void setRequestBeanLiveData(MutableLiveData<CleanStorageRequestBean> requestBeanLiveData) {
        this.requestBeanLiveData = requestBeanLiveData;
    }
    public MutableLiveData<List<CleanStorageBean>> getDataListLiveData() {
        if (dataListLiveData == null) {
        if (dataListLiveData == null){
            dataListLiveData = new MutableLiveData<>();
        }
        return dataListLiveData;
@@ -41,12 +253,59 @@
        this.dataListLiveData = dataListLiveData;
    }
    public void getCleanStorages() {
        List<CleanStorageBean> cleanStorageBeans =  new ArrayList<>();
        cleanStorageBeans.add(new CleanStorageBean(1,"保洁","全体","玻璃窗","玻璃水洗","杂物房","窗户","擦干",null));
        cleanStorageBeans.add(new CleanStorageBean(1,"保洁","全体","玻璃窗","玻璃水洗","杂物房","窗户","擦干",null));
        cleanStorageBeans.add(new CleanStorageBean(1,"保洁","全体","玻璃窗","玻璃水洗","杂物房","窗户","擦干",null));
        cleanStorageBeans.add(new CleanStorageBean(1,"保洁","全体","玻璃窗","玻璃水洗","杂物房","窗户","擦干",null));
        dataListLiveData.setValue(cleanStorageBeans);
    @NonNull
    public MutableLiveData<List<CleanStorageBean>> getMoreListLiveData() {
        if (moreListLiveData == null){
            moreListLiveData = new MutableLiveData<>();
        }
        return moreListLiveData;
    }
    public void setMoreListLiveData(MutableLiveData<List<CleanStorageBean>> 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<CleanStorageBean>> getFinishDeleteListLiveData() {
        if (finishDeleteListLiveData == null){
            finishDeleteListLiveData = new MutableLiveData<>();
        }
        return finishDeleteListLiveData;
    }
    public void setFinishDeleteListLiveData(MutableLiveData<List<CleanStorageBean>> finishDeleteListLiveData) {
        this.finishDeleteListLiveData = finishDeleteListLiveData;
    }
    /**
     * 本家数据 置顶
     */
    public void ownTop(){
        List<CleanStorageBean> list = getDataListLiveData().getValue();
        if (list == null || list.isEmpty()){
            return;
        }
        //根据ownData值为1的 放到前面
        list.sort((o1, o2) -> {
            if (o1.getOwnData() == 1 && o2.getOwnData() == 0) {
                return -1;
            } else if (o1.getOwnData() == 0 && o2.getOwnData() == 1) {
                return 1;
            }
            return 0;
        });
        getDataListLiveData().setValue(list);
    }
}