| | |
| | | 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.FamilyAssetsBean; |
| | | import com.application.zhangshi_app_android.bean.FamilyMemorabiliaBean; |
| | | import com.application.zhangshi_app_android.bean.FamilyMemorabiliaRequestBean; |
| | | import com.application.zhangshi_app_android.bean.FamilyMemorabiliaResponseBean; |
| | | import com.application.zhangshi_app_android.data.DataRepository; |
| | | |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.Comparator; |
| | | import java.util.Date; |
| | | 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<Integer> operateTypeLiveData;//操作类型 0 选择操作 1.完成 2.删除 |
| | | |
| | | private MutableLiveData<FamilyMemorabiliaRequestBean> requestBeanMutableLiveData; |
| | | public FamilyMemorabiliaActivityViewModel(@NonNull Application application) { |
| | | super(application); |
| | | } |
| | |
| | | protected DataRepository initModel() { |
| | | return DataRepository.getInstance(); |
| | | } |
| | | /** |
| | | * 获取家大事记 |
| | | */ |
| | | public void getFamilyMemorabilia() { |
| | | if (getRequestBeanMutableLiveData().getValue() == null){ |
| | | getRequestBeanMutableLiveData().setValue(new FamilyMemorabiliaRequestBean()); |
| | | } |
| | | model.getFamilyMemorabilia(getRequestBeanMutableLiveData().getValue().getMap()) |
| | | .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 { |
| | | 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 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; |
| | | } |
| | | |
| | | @NonNull |
| | | public MutableLiveData<FamilyMemorabiliaRequestBean> getRequestBeanMutableLiveData() { |
| | | if (requestBeanMutableLiveData == null){ |
| | | requestBeanMutableLiveData = new MutableLiveData<>(); |
| | | } |
| | | if (requestBeanMutableLiveData.getValue()==null){ |
| | | requestBeanMutableLiveData.postValue(new FamilyMemorabiliaRequestBean()); |
| | | } |
| | | return requestBeanMutableLiveData; |
| | | } |
| | | |
| | | public void setRequestBeanMutableLiveData(MutableLiveData<FamilyMemorabiliaRequestBean> requestBeanMutableLiveData) { |
| | | this.requestBeanMutableLiveData = requestBeanMutableLiveData; |
| | | } |
| | | |
| | | 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 void changeOrder(boolean b) { |
| | | List<FamilyMemorabiliaBean> list = getDataListLiveData().getValue(); |
| | | if (list == null || list.isEmpty()){ |
| | | return; |
| | | } |
| | | list.sort(new Comparator<FamilyMemorabiliaBean>() { |
| | | @Override |
| | | public int compare(FamilyMemorabiliaBean o1, FamilyMemorabiliaBean o2) { |
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", java.util.Locale.getDefault()); |
| | | Date date1 = null; |
| | | Date date2 = null; |
| | | try { |
| | | date1 = format.parse(o1.getCreateTime()); |
| | | date2 = format.parse(o2.getCreateTime()); |
| | | } catch (ParseException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | if (date1 != null && date2 != null) { |
| | | if (b){ |
| | | return date2.compareTo(date1); |
| | | }else { |
| | | return date1.compareTo(date2); |
| | | } |
| | | } |
| | | return 0; |
| | | } |
| | | }); |
| | | getDataListLiveData().postValue(list); |
| | | } |
| | | } |