| | |
| | | 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; |
| | | import io.reactivex.functions.Action; |
| | | |
| | | /** |
| | | * @author Ljj |
| | |
| | | */ |
| | | public class FamilyMemorabiliaActivityViewModel extends BaseViewModel<DataRepository> { |
| | | |
| | | private MutableLiveData<List<FamilyMemorabiliaBean>> checkListLiveData; |
| | | private MutableLiveData<List<FamilyMemorabiliaBean>> dataListLiveData; |
| | | private MutableLiveData<String> stringMutableLiveData; |
| | | private MutableLiveData<List<FamilyMemorabiliaBean>> checkListLiveData;//选中的数据 |
| | | private MutableLiveData<List<FamilyMemorabiliaBean>> dataListLiveData;//所有数据 |
| | | private MutableLiveData<Integer> operateTypeLiveData;//操作类型 0 选择操作 1.完成 2.删除 |
| | | |
| | | private MutableLiveData<FamilyMemorabiliaRequestBean> requestBeanMutableLiveData;//请求参数 |
| | | private MutableLiveData<List<FamilyMemorabiliaBean>> moreListLiveData;//下拉加载数据 |
| | | public FamilyMemorabiliaActivityViewModel(@NonNull Application application) { |
| | | super(application); |
| | | } |
| | |
| | | /** |
| | | * 获取家大事记 |
| | | */ |
| | | 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) |
| | | public void getFamilyMemorabilia() { |
| | | System.out.println(getRequestBeanMutableLiveData().getValue()); |
| | | model.getFamilyMemorabilia(getRequestBeanMutableLiveData().getValue().getMap()) |
| | | .compose(RxUtils.schedulersTransformer()) |
| | | .subscribe(new Observer<ResultData<FamilyMemorabiliaResponseBean>>() { |
| | | @Override |
| | |
| | | if (data.getData().getData().isEmpty()){ |
| | | changeStateView(StateViewEnum.DATA_NULL); |
| | | }else { |
| | | changeStateView(StateViewEnum.HIDE); |
| | | dataListLiveData.postValue(data.getData().getData()); |
| | | } |
| | | }else { |
| | |
| | | }); |
| | | } |
| | | /** |
| | | * 增加家大事记 |
| | | * 获取家大事记 加载更多 |
| | | */ |
| | | public void add(FamilyMemorabiliaBean bean) { |
| | | model.addFamilyMemorabilia(bean) |
| | | public void getMoreFamilyMemorabilia() { |
| | | requestBeanMutableLiveData.getValue().setPageNum(requestBeanMutableLiveData.getValue().getPageNum() + 1); |
| | | model.getFamilyMemorabilia(getRequestBeanMutableLiveData().getValue().getMap()) |
| | | .compose(RxUtils.schedulersTransformer()) |
| | | .subscribe(new Observer<ResultData<String>>() { |
| | | .doFinally(new Action() { |
| | | @Override |
| | | public void run() throws Exception { |
| | | changeStateView(StateViewEnum.DATA_FINISH); |
| | | } |
| | | }) |
| | | .subscribe(new Observer<ResultData<FamilyMemorabiliaResponseBean>>() { |
| | | @Override |
| | | public void onSubscribe(Disposable d) { |
| | | addSubscribe(d); |
| | | } |
| | | |
| | | @Override |
| | | public void onNext(ResultData<String> stringResultData) { |
| | | if (stringResultData.getCode() == CODE_SUCCESS){ |
| | | messageLiveData.postValue("添加成功"); |
| | | public void onNext(ResultData<FamilyMemorabiliaResponseBean> 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(stringResultData.getMsg()); |
| | | messageLiveData.postValue(data.getMsg()); |
| | | } |
| | | } |
| | | |
| | |
| | | this.checkListLiveData = checkListLiveData; |
| | | } |
| | | |
| | | public MutableLiveData<FamilyMemorabiliaRequestBean> getRequestBeanMutableLiveData() { |
| | | if (requestBeanMutableLiveData == null){ |
| | | requestBeanMutableLiveData = new MutableLiveData<>(); |
| | | } |
| | | if (requestBeanMutableLiveData.getValue() == null){ |
| | | requestBeanMutableLiveData.setValue(new FamilyMemorabiliaRequestBean()); |
| | | } |
| | | return requestBeanMutableLiveData; |
| | | } |
| | | |
| | | public void setRequestBeanMutableLiveData(MutableLiveData<FamilyMemorabiliaRequestBean> requestBeanMutableLiveData) { |
| | | this.requestBeanMutableLiveData = requestBeanMutableLiveData; |
| | | } |
| | | |
| | | public MutableLiveData<List<FamilyMemorabiliaBean>> getDataListLiveData() { |
| | | if (dataListLiveData == null){ |
| | | dataListLiveData = new MutableLiveData<>(); |
| | |
| | | this.dataListLiveData = dataListLiveData; |
| | | } |
| | | |
| | | public MutableLiveData<String> getStringMutableLiveData() { |
| | | if (stringMutableLiveData == null){ |
| | | stringMutableLiveData = new MutableLiveData<>(); |
| | | public MutableLiveData<List<FamilyMemorabiliaBean>> getMoreListLiveData() { |
| | | if (moreListLiveData == null){ |
| | | moreListLiveData = new MutableLiveData<>(); |
| | | } |
| | | return stringMutableLiveData; |
| | | return moreListLiveData; |
| | | } |
| | | |
| | | public void setStringMutableLiveData(MutableLiveData<String> stringMutableLiveData) { |
| | | this.stringMutableLiveData = stringMutableLiveData; |
| | | public void setMoreListLiveData(MutableLiveData<List<FamilyMemorabiliaBean>> moreListLiveData) { |
| | | this.moreListLiveData = moreListLiveData; |
| | | } |
| | | |
| | | public MutableLiveData<Integer> getOperateTypeLiveData() { |
| | | if (operateTypeLiveData == null){ |
| | | operateTypeLiveData = new MutableLiveData<>(); |
| | | operateTypeLiveData.setValue(0); |
| | | } |
| | | return operateTypeLiveData; |
| | | } |
| | | |
| | | public void setOperateTypeLiveData(MutableLiveData<Integer> operateTypeLiveData) { |
| | | this.operateTypeLiveData = operateTypeLiveData; |
| | | } |
| | | |
| | | /** |
| | | * 改变排序 |
| | | * @param b true 降序 false 升序 |
| | | */ |
| | | 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()); |
| | | } |
| | | }); |
| | | List<FamilyMemorabiliaBean> list = getDataListLiveData().getValue(); |
| | | if (list == null || list.isEmpty()){ |
| | | return; |
| | | } |
| | | getDataListLiveData().postValue(dataList); |
| | | 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); |
| | | } |
| | | } |