Linjiajia
2023-07-25 82e57df230ecb744af6c8865f80870ba03c86d89
app/src/main/java/com/application/zhangshi_app_android/ui/function/TourismActivityViewModel.java
@@ -1,16 +1,31 @@
package com.application.zhangshi_app_android.ui.function;
import static com.android.app_base.base.BaseConfig.CODE_SUCCESS;
import android.app.Application;
import android.text.TextUtils;
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.android.app_base.utils.Utils;
import com.application.zhangshi_app_android.bean.PageResponseBean;
import com.application.zhangshi_app_android.bean.TourismBean;
import com.application.zhangshi_app_android.bean.TourismRequestBean;
import com.application.zhangshi_app_android.data.DataRepository;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
/**
 * @author Ljj
@@ -19,6 +34,12 @@
 */
public class TourismActivityViewModel extends BaseViewModel<DataRepository> {
    private MutableLiveData<List<TourismBean>> dataListLiveData;
    private MutableLiveData<List<TourismBean>> checkListLiveData;//选中的数据
    private MutableLiveData<Integer> operateTypeLiveData;//右上角操作框 操作类型 0 弹出选择操作 1.完成 2.删除
    private MutableLiveData<TourismRequestBean> requestBeanLiveData;//请求参数
    private MutableLiveData<List<TourismBean>> moreListLiveData;//下拉加载数据
    private MutableLiveData<List<TourismBean>> finishDeleteListLiveData;//已经完成删除的数据,用于adapter的remove操作
    public TourismActivityViewModel(@NonNull Application application) {
        super(application);
@@ -29,8 +50,203 @@
        return DataRepository.getInstance();
    }
    /**
     * 获取数据
     */
    public void getTourism() {
        if (getRequestBeanLiveData().getValue() == null){
            getRequestBeanLiveData().setValue(new TourismRequestBean());
        }
        getRequestBeanLiveData().getValue().setPageNum(1);
        model.getTourism(getRequestBeanLiveData().getValue().toMap())
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new Observer<ResultData<PageResponseBean<TourismBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addSubscribe(d);
                    }
                    @Override
                    public void onNext(ResultData<PageResponseBean<TourismBean>> 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 getMoreTourism() {
        if (getRequestBeanLiveData().getValue() == null){
            getRequestBeanLiveData().setValue(new TourismRequestBean());
        }
        getRequestBeanLiveData().getValue().setPageNum(getRequestBeanLiveData().getValue().getPageNum() + 1);
        model.getTourism(getRequestBeanLiveData().getValue().toMap())
                .compose(RxUtils.schedulersTransformer())
                .doFinally(new Action() {
                    @Override
                    public void run() throws Exception {
                        changeStateView(StateViewEnum.DATA_FINISH);
                    }
                })
                .subscribe(new Observer<ResultData<PageResponseBean<TourismBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addSubscribe(d);
                    }
                    @Override
                    public void onNext(ResultData<PageResponseBean<TourismBean>> 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<TourismBean> deleteList = getCheckListLiveData().getValue();
        if (deleteList == null){
            return;
        }
        StringBuffer sb = new StringBuffer();
        deleteList.forEach(TourismBean -> sb.append(TourismBean.getId()).append(","));
        sb.deleteCharAt(sb.length()-1);
        model.deleteTourism(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){
                            messageLiveData.postValue("删除成功");
                            finishDeleteListLiveData.postValue(checkListLiveData.getValue());
                            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(TourismBean bean) {
        model.deleteTourism(String.valueOf(bean.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<TourismBean> list = new ArrayList<>();
                            list.add(bean);
                            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<TourismBean>> 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<TourismBean>> checkListLiveData) {
        this.checkListLiveData = checkListLiveData;
    }
    @NonNull
    public MutableLiveData<TourismRequestBean> getRequestBeanLiveData() {
        if (requestBeanLiveData == null){
            requestBeanLiveData = new MutableLiveData<>();
        }
        if (requestBeanLiveData.getValue() == null){
            requestBeanLiveData.setValue(new TourismRequestBean());
        }
        return requestBeanLiveData;
    }
    public void setRequestBeanLiveData(MutableLiveData<TourismRequestBean> requestBeanLiveData) {
        this.requestBeanLiveData = requestBeanLiveData;
    }
    public MutableLiveData<List<TourismBean>> getDataListLiveData() {
        if (dataListLiveData == null) {
        if (dataListLiveData == null){
            dataListLiveData = new MutableLiveData<>();
        }
        return dataListLiveData;
@@ -40,17 +256,92 @@
        this.dataListLiveData = dataListLiveData;
    }
    /**
     *  获取年度健康情况
     */
    public void getTourism() {
        List<TourismBean> beans =  new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            TourismBean bean = new TourismBean();
            beans.add(bean);
    @NonNull
    public MutableLiveData<List<TourismBean>> getMoreListLiveData() {
        if (moreListLiveData == null){
            moreListLiveData = new MutableLiveData<>();
        }
        dataListLiveData.setValue(beans);
        return moreListLiveData;
    }
    public void setMoreListLiveData(MutableLiveData<List<TourismBean>> 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<TourismBean>> getFinishDeleteListLiveData() {
        if (finishDeleteListLiveData == null){
            finishDeleteListLiveData = new MutableLiveData<>();
        }
        return finishDeleteListLiveData;
    }
    public void setFinishDeleteListLiveData(MutableLiveData<List<TourismBean>> finishDeleteListLiveData) {
        this.finishDeleteListLiveData = finishDeleteListLiveData;
    }
    /**
     * 改变排序
     * @param b true 降序 false 升序
     */
    public void changeOrder(boolean b) {
        List<TourismBean> list = getDataListLiveData().getValue();
        if (list == null || list.isEmpty()){
            return;
        }
        if (b) {
            // 从新到旧,null排最后
            list.sort((o1, o2) -> {
                String happenTime1 = o1.getStartTime();
                String happenTime2 = o2.getStartTime();
                if (TextUtils.isEmpty(happenTime1) && TextUtils.isEmpty(happenTime2)) {
                    return 0;
                } else if (TextUtils.isEmpty(happenTime1)) {
                    return 1;
                } else if (TextUtils.isEmpty(happenTime2)) {
                    return -1;
                } else {
                    Date date1 = Utils.parseDate(happenTime1);
                    Date date2 = Utils.parseDate(happenTime2);
                    if (date1 != null && date2 != null) {
                        return date2.compareTo(date1);
                    }
                    return 0;
                }
            });
        } else  {
            // 从旧到新,null排最前
            list.sort((o1, o2) -> {
                String happenTime1 = o1.getStartTime();
                String happenTime2 = o2.getStartTime();
                if (TextUtils.isEmpty(happenTime1) && TextUtils.isEmpty(happenTime2)) {
                    return 0;
                } else if (TextUtils.isEmpty(happenTime1)) {
                    return -1;
                } else if (TextUtils.isEmpty(happenTime2)) {
                    return 1;
                } else {
                    Date date1 = Utils.parseDate(happenTime1);
                    Date date2 = Utils.parseDate(happenTime2);
                    if (date1 != null && date2 != null) {
                        return date1.compareTo(date2);
                    }
                    return 0;
                }
            });
        }
        getDataListLiveData().setValue(list);
    }
}