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;
|
import io.reactivex.functions.Action;
|
|
/**
|
* @author Ljj
|
* @date 2023.03.18. 10:00
|
* @desc 家大事记 viewmodel
|
*/
|
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;//请求参数
|
private MutableLiveData<List<FamilyMemorabiliaBean>> moreListLiveData;//下拉加载数据
|
public FamilyMemorabiliaActivityViewModel(@NonNull Application application) {
|
super(application);
|
}
|
|
@Override
|
protected DataRepository initModel() {
|
return DataRepository.getInstance();
|
}
|
/**
|
* 获取家大事记
|
*/
|
public void getFamilyMemorabilia() {
|
System.out.println(getRequestBeanMutableLiveData().getValue());
|
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 getMoreFamilyMemorabilia() {
|
requestBeanMutableLiveData.getValue().setPageNum(requestBeanMutableLiveData.getValue().getPageNum() + 1);
|
model.getFamilyMemorabilia(getRequestBeanMutableLiveData().getValue().getMap())
|
.compose(RxUtils.schedulersTransformer())
|
.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<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(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<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<>();
|
}
|
return dataListLiveData;
|
}
|
|
public void setDataListLiveData(MutableLiveData<List<FamilyMemorabiliaBean>> dataListLiveData) {
|
this.dataListLiveData = dataListLiveData;
|
}
|
|
public MutableLiveData<List<FamilyMemorabiliaBean>> getMoreListLiveData() {
|
if (moreListLiveData == null){
|
moreListLiveData = new MutableLiveData<>();
|
}
|
return moreListLiveData;
|
}
|
|
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> 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);
|
}
|
}
|