Linjiajia
2023-10-07 31b7700b976a46901f67c5d7a00281ca4745fc9f
app/src/main/java/com/application/zhangshi_app_android/ui/function/HomeDevicesActivityViewModel.java
@@ -3,27 +3,31 @@
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.FamilyMemorabiliaBean;
import com.application.zhangshi_app_android.bean.HomeDevicesBean;
import com.application.zhangshi_app_android.bean.HomeDevicesResponseBean;
import com.application.zhangshi_app_android.bean.HomeDevicesRequestBean;
import com.application.zhangshi_app_android.bean.IncomeAndExpensesBean;
import com.application.zhangshi_app_android.bean.PageResponseBean;
import com.application.zhangshi_app_android.data.DataRepository;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.ArrayList;
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
@@ -31,8 +35,13 @@
 * @desc 家庭设备 ViewModel
 */
public class HomeDevicesActivityViewModel extends BaseViewModel<DataRepository> {
    private MutableLiveData<List<HomeDevicesBean>> dataListLiveData;//数据
    private MutableLiveData<List<HomeDevicesBean>> dataListLiveData;//获取的数据
    private MutableLiveData<List<HomeDevicesBean>> checkListLiveData;//选中的数据
    private MutableLiveData<List<HomeDevicesBean>> moreListLiveData;//下拉加载数据
    private MutableLiveData<HomeDevicesRequestBean> requestBeanLiveData;//请求参数
    private MutableLiveData<List<HomeDevicesBean>> finishDeleteListLiveData;//已经完成删除的数据,用于adapter的remove操作
    private MutableLiveData<Integer> operateTypeLiveData;//操作类型 0 选择操作 1.完成 2.删除
    public HomeDevicesActivityViewModel(@NonNull Application application) {
        super(application);
@@ -43,17 +52,24 @@
        return DataRepository.getInstance();
    }
    public void getHomeDevices(){
        model.getHomeDevices(new HashMap<>())
    /**
     * 获取数据
     */
    public void getHomeDevices() {
        if (getRequestBeanLiveData().getValue() == null){
            getRequestBeanLiveData().setValue(new HomeDevicesRequestBean());
        }
        getRequestBeanLiveData().getValue().setPageNum(1);
        model.getHomeDevices(getRequestBeanLiveData().getValue().toMap())
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new Observer<ResultData<HomeDevicesResponseBean>>() {
                .subscribe(new Observer<ResultData<PageResponseBean<HomeDevicesBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addSubscribe(d);
                    }
                    @Override
                    public void onNext(ResultData<HomeDevicesResponseBean> data) {
                    public void onNext(ResultData<PageResponseBean<HomeDevicesBean>> data) {
                        if (data.getCode() == CODE_SUCCESS){
                            if (data.getData().getData().isEmpty()){
                                changeStateView(StateViewEnum.DATA_NULL);
@@ -77,6 +93,159 @@
                    }
                });
    }
    /**
     * 加载更多
     */
    public void getMoreHomeDevices() {
        if (getRequestBeanLiveData().getValue() == null){
            getRequestBeanLiveData().setValue(new HomeDevicesRequestBean());
        }
        getRequestBeanLiveData().getValue().setPageNum(getRequestBeanLiveData().getValue().getPageNum() + 1);
        model.getHomeDevices(getRequestBeanLiveData().getValue().toMap())
                .compose(RxUtils.schedulersTransformer())
                .doFinally(new Action() {
                    @Override
                    public void run() throws Exception {
                        changeStateView(StateViewEnum.DATA_FINISH);
                    }
                })
                .subscribe(new Observer<ResultData<PageResponseBean<HomeDevicesBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addSubscribe(d);
                    }
                    @Override
                    public void onNext(ResultData<PageResponseBean<HomeDevicesBean>> 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<HomeDevicesBean> deleteList = getCheckListLiveData().getValue();
        if (deleteList == null){
            return;
        }
        StringBuffer sb = new StringBuffer();
        deleteList.forEach(HomeDevicesBean -> sb.append(HomeDevicesBean.getId()).append(","));
        sb.deleteCharAt(sb.length()-1);
        model.deleteHomeDevices(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(HomeDevicesBean HomeDevicesBean) {
        model.deleteHomeDevices(String.valueOf(HomeDevicesBean.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<HomeDevicesBean> list = new ArrayList<>();
                            list.add(HomeDevicesBean);
                            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<HomeDevicesBean>> 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<HomeDevicesBean>> checkListLiveData) {
        this.checkListLiveData = checkListLiveData;
    }
    @NonNull
    public MutableLiveData<HomeDevicesRequestBean> getRequestBeanLiveData() {
        if (requestBeanLiveData == null){
            requestBeanLiveData = new MutableLiveData<>();
        }
        if (requestBeanLiveData.getValue() == null){
            requestBeanLiveData.setValue(new HomeDevicesRequestBean());
        }
        return requestBeanLiveData;
    }
    public void setRequestBeanLiveData(MutableLiveData<HomeDevicesRequestBean> requestBeanLiveData) {
        this.requestBeanLiveData = requestBeanLiveData;
    }
    public MutableLiveData<List<HomeDevicesBean>> getDataListLiveData() {
        if (dataListLiveData == null){
@@ -89,38 +258,112 @@
        this.dataListLiveData = dataListLiveData;
    }
    @NonNull
    public MutableLiveData<List<HomeDevicesBean>> getMoreListLiveData() {
        if (moreListLiveData == null){
            moreListLiveData = new MutableLiveData<>();
        }
        return moreListLiveData;
    }
    public void setMoreListLiveData(MutableLiveData<List<HomeDevicesBean>> 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<HomeDevicesBean>> getFinishDeleteListLiveData() {
        if (finishDeleteListLiveData == null){
            finishDeleteListLiveData = new MutableLiveData<>();
        }
        return finishDeleteListLiveData;
    }
    public void setFinishDeleteListLiveData(MutableLiveData<List<HomeDevicesBean>> finishDeleteListLiveData) {
        this.finishDeleteListLiveData = finishDeleteListLiveData;
    }
    /**
     * 按时间排序
     * @param type 0 从新到旧 1 从旧到新
     * 改变排序
     * @param b true 降序 false 升序
     */
    public void sortDataList(int type){
        List<HomeDevicesBean> list = dataListLiveData.getValue();
    public void changeOrder(boolean b) {
        List<HomeDevicesBean> list = getDataListLiveData().getValue();
        if (list == null || list.isEmpty()){
            return;
        }
        list.sort(new Comparator<HomeDevicesBean>() {
            @Override
            public int compare(HomeDevicesBean o1, HomeDevicesBean o2) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", java.util.Locale.getDefault());
                Date date1 = null;
                Date date2 = null;
                try {
                    date1 = format.parse(o1.getCreateDate());
                    date2 = format.parse(o2.getCreateDate());
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                if (date1 != null && date2 != null) {
                    if (type == 0){
        if (b) {
            // 从新到旧,null排最后
            list.sort((o1, o2) -> {
                String happenTime1 = o1.getHappenTime();
                String happenTime2 = o2.getHappenTime();
                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);
                    }else {
                    }
                    return 0;
                }
            });
        } else {
            // 从旧到新,null排最前
            list.sort((o1, o2) -> {
                String happenTime1 = o1.getHappenTime();
                String happenTime2 = o2.getHappenTime();
                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;
                }
                return 0;
            });
        }
        getDataListLiveData().setValue(list);
    }
    /**
     * 本家数据 置顶
     */
    public void ownTop(){
        List<HomeDevicesBean> 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;
        });
        dataListLiveData.postValue(list);
        getDataListLiveData().setValue(list);
    }
}