| | |
| | | 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 |
| | |
| | | */ |
| | | 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); |
| | | } |
| | |
| | | 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 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); |
| | | } |
| | | } |