From bf4e40cdf60c2a2fd8a486051a1ddac2daefef62 Mon Sep 17 00:00:00 2001 From: Linjiajia <319408893@qq.com> Date: 星期二, 28 三月 2023 19:00:24 +0800 Subject: [PATCH] 家大事记完善 --- app/src/main/java/com/application/zhangshi_app_android/ui/function/FamilyMemorabiliaActivityViewModel.java | 209 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 209 insertions(+), 0 deletions(-) diff --git a/app/src/main/java/com/application/zhangshi_app_android/ui/function/FamilyMemorabiliaActivityViewModel.java b/app/src/main/java/com/application/zhangshi_app_android/ui/function/FamilyMemorabiliaActivityViewModel.java index 1e180b5..47519be 100644 --- a/app/src/main/java/com/application/zhangshi_app_android/ui/function/FamilyMemorabiliaActivityViewModel.java +++ b/app/src/main/java/com/application/zhangshi_app_android/ui/function/FamilyMemorabiliaActivityViewModel.java @@ -1,11 +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.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.FamilyMemorabiliaBean; +import com.application.zhangshi_app_android.bean.FamilyMemorabiliaResponseBean; import com.application.zhangshi_app_android.data.DataRepository; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import io.reactivex.Observer; +import io.reactivex.disposables.Disposable; /** * @author Ljj @@ -14,6 +32,9 @@ */ public class FamilyMemorabiliaActivityViewModel extends BaseViewModel<DataRepository> { + private MutableLiveData<List<FamilyMemorabiliaBean>> checkListLiveData; + private MutableLiveData<List<FamilyMemorabiliaBean>> dataListLiveData; + private MutableLiveData<String> stringMutableLiveData; public FamilyMemorabiliaActivityViewModel(@NonNull Application application) { super(application); } @@ -22,4 +43,192 @@ protected DataRepository initModel() { return DataRepository.getInstance(); } + /** + * 鑾峰彇瀹跺ぇ浜嬭 + */ + public void getFamilyMemorabilia(int searchType) { + Map<String, Object> queryMap = new HashMap<>(); + queryMap.put("pageNum",1); + queryMap.put("pageSize",20); + if (searchType == 1){ + queryMap.put("remark",stringMutableLiveData.getValue()); + } else if (searchType == 2) { + queryMap.put("title",stringMutableLiveData.getValue()); + }else if (searchType == 3) { + queryMap.put("people",stringMutableLiveData.getValue()); + } + else if (searchType == 4) { + queryMap.put("createTime",stringMutableLiveData.getValue()); + } + else if (searchType == 5) { + queryMap.put("address",stringMutableLiveData.getValue()); + } + model.getFamilyMemorabilia(queryMap) + .compose(RxUtils.schedulersTransformer()) + .subscribe(new Observer<ResultData<FamilyMemorabiliaResponseBean>>() { + @Override + public void onSubscribe(Disposable d) { + addSubscribe(d); + } + + @Override + public void onNext(ResultData<FamilyMemorabiliaResponseBean> data) { + if (data.getCode() == CODE_SUCCESS){ + if (data.getData().getData().isEmpty()){ + changeStateView(StateViewEnum.DATA_NULL); + }else { + 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 add(FamilyMemorabiliaBean bean) { + model.addFamilyMemorabilia(bean) + .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("娣诲姞鎴愬姛"); + }else { + messageLiveData.postValue(stringResultData.getMsg()); + } + } + + @Override + public void onError(Throwable e) { + messageLiveData.postValue(e.getMessage()); + } + + @Override + public void onComplete() { + + } + }); + } + /** + * 鍒犻櫎瀹跺ぇ浜嬭 + */ + public void delete() { + List<FamilyMemorabiliaBean> deleteList = getCheckListLiveData().getValue(); + if (deleteList == null){ + return; + } + StringBuffer sb = new StringBuffer(); + deleteList.forEach(familyMemorabiliaBean -> sb.append(familyMemorabiliaBean.getId()).append(",")); + sb.deleteCharAt(sb.length()-1); + System.out.println(); + model.deleteFamilyMemorabilia(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){ + List<FamilyMemorabiliaBean> list = getDataListLiveData().getValue(); + if (list != null) { + list.removeAll(getCheckListLiveData().getValue()); + } + getDataListLiveData().postValue(list); + 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 MutableLiveData<List<FamilyMemorabiliaBean>> 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<FamilyMemorabiliaBean>> checkListLiveData) { + this.checkListLiveData = checkListLiveData; + } + + public MutableLiveData<List<FamilyMemorabiliaBean>> getDataListLiveData() { + if (dataListLiveData == null){ + dataListLiveData = new MutableLiveData<>(); + } + return dataListLiveData; + } + + public void setDataListLiveData(MutableLiveData<List<FamilyMemorabiliaBean>> dataListLiveData) { + this.dataListLiveData = dataListLiveData; + } + + public MutableLiveData<String> getStringMutableLiveData() { + if (stringMutableLiveData == null){ + stringMutableLiveData = new MutableLiveData<>(); + } + return stringMutableLiveData; + } + + public void setStringMutableLiveData(MutableLiveData<String> stringMutableLiveData) { + this.stringMutableLiveData = stringMutableLiveData; + } + + public void changeOrder(boolean b) { + List<FamilyMemorabiliaBean> dataList = getDataListLiveData().getValue(); + if (dataList == null) return; + if (b){ + dataList.sort(new Comparator<FamilyMemorabiliaBean>() { + @Override + public int compare(FamilyMemorabiliaBean o1, FamilyMemorabiliaBean o2) { + return (int) (o1.getId() - o2.getId()); + } + }); + }else { + dataList.sort(new Comparator<FamilyMemorabiliaBean>() { + @Override + public int compare(FamilyMemorabiliaBean o1, FamilyMemorabiliaBean o2) { + return (int) (o2.getId() - o1.getId()); + } + }); + } + getDataListLiveData().postValue(dataList); + } } -- Gitblit v1.9.1