| | |
| | | package com.application.zhangshi_app_android.ui.function; |
| | | |
| | | import static com.android.app_base.base.BaseConfig.CODE_SUCCESS; |
| | | |
| | | import android.app.Application; |
| | | import android.text.TextUtils; |
| | | |
| | | 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.android.app_base.utils.Utils; |
| | | import com.application.zhangshi_app_android.bean.CertificateOfHonorBean; |
| | | import com.application.zhangshi_app_android.bean.HundredWishBean; |
| | | import com.application.zhangshi_app_android.bean.CertificateOfHonorRequestBean; |
| | | import com.application.zhangshi_app_android.bean.PageResponseBean; |
| | | import com.application.zhangshi_app_android.data.DataRepository; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | import io.reactivex.Observer; |
| | | import io.reactivex.disposables.Disposable; |
| | | import io.reactivex.functions.Action; |
| | | |
| | | /** |
| | | * @author Ljj |
| | |
| | | */ |
| | | public class CertificateOfHonorActivityViewModel extends BaseViewModel<DataRepository> { |
| | | private MutableLiveData<List<CertificateOfHonorBean>> dataListLiveData; |
| | | private MutableLiveData<List<CertificateOfHonorBean>> moreListLiveData;//加载更多数据 |
| | | |
| | | private MutableLiveData<CertificateOfHonorRequestBean> requestBeanLiveData;//请求参数 |
| | | public CertificateOfHonorActivityViewModel(@NonNull Application application) { |
| | | super(application); |
| | | } |
| | |
| | | return DataRepository.getInstance(); |
| | | } |
| | | |
| | | /** |
| | | * 获取数据 |
| | | */ |
| | | public void getCertificateOfHonor(){ |
| | | if (getRequestBeanLiveData().getValue() == null){ |
| | | getRequestBeanLiveData().setValue(new CertificateOfHonorRequestBean()); |
| | | } |
| | | getRequestBeanLiveData().getValue().setPageNum(1); |
| | | model.getCertificateOfHonor(getRequestBeanLiveData().getValue().toMap()) |
| | | .compose(RxUtils.schedulersTransformer()) |
| | | .subscribe(new Observer<ResultData<PageResponseBean<CertificateOfHonorBean>>>() { |
| | | @Override |
| | | public void onSubscribe(Disposable d) { |
| | | addSubscribe(d); |
| | | } |
| | | |
| | | @Override |
| | | public void onNext(ResultData<PageResponseBean<CertificateOfHonorBean>> 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 loadMoreFamilyAssets(){ |
| | | if (getRequestBeanLiveData().getValue() == null){ |
| | | getRequestBeanLiveData().setValue(new CertificateOfHonorRequestBean()); |
| | | } |
| | | getRequestBeanLiveData().getValue().setPageNum(getRequestBeanLiveData().getValue().getPageNum() + 1); |
| | | model.getCertificateOfHonor(getRequestBeanLiveData().getValue().toMap()) |
| | | .compose(RxUtils.schedulersTransformer()) |
| | | .doFinally(new Action() { |
| | | @Override |
| | | public void run() throws Exception { |
| | | changeStateView(StateViewEnum.DATA_FINISH); |
| | | } |
| | | }) |
| | | .subscribe(new Observer<ResultData<PageResponseBean<CertificateOfHonorBean>>>() { |
| | | @Override |
| | | public void onSubscribe(Disposable d) { |
| | | addSubscribe(d); |
| | | } |
| | | |
| | | @Override |
| | | public void onNext(ResultData<PageResponseBean<CertificateOfHonorBean>> 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 MutableLiveData<List<CertificateOfHonorBean>> getDataListLiveData() { |
| | | if (dataListLiveData == null) { |
| | | if (dataListLiveData == null){ |
| | | dataListLiveData = new MutableLiveData<>(); |
| | | } |
| | | return dataListLiveData; |
| | | } |
| | | |
| | | public MutableLiveData<CertificateOfHonorRequestBean> getRequestBeanLiveData() { |
| | | if (requestBeanLiveData == null){ |
| | | requestBeanLiveData = new MutableLiveData<>(); |
| | | } |
| | | if (requestBeanLiveData.getValue() == null){ |
| | | requestBeanLiveData.setValue(new CertificateOfHonorRequestBean()); |
| | | } |
| | | return requestBeanLiveData; |
| | | } |
| | | |
| | | public void setRequestBeanLiveData(MutableLiveData<CertificateOfHonorRequestBean> requestBeanLiveData) { |
| | | this.requestBeanLiveData = requestBeanLiveData; |
| | | } |
| | | |
| | | public void setDataListLiveData(MutableLiveData<List<CertificateOfHonorBean>> dataListLiveData) { |
| | | this.dataListLiveData = dataListLiveData; |
| | | } |
| | | |
| | | public void getHundredWish() { |
| | | List<CertificateOfHonorBean> beans = new ArrayList<>(); |
| | | for (int i = 0; i < 10; i++) { |
| | | CertificateOfHonorBean bean = new CertificateOfHonorBean(); |
| | | bean.setName("荣誉证书" + i); |
| | | bean.setTime("2023-01-01"); |
| | | bean.setType("类别" + i); |
| | | bean.setCertificateNumber("证号" + i); |
| | | bean.setLevel("级别" + i); |
| | | bean.setEffectiveTime("2030-01-01"); |
| | | bean.setGetTime("2020-01-01"); |
| | | bean.setLocation("存放位置" + i); |
| | | bean.setRemark("备注"); |
| | | beans.add(bean); |
| | | public MutableLiveData<List<CertificateOfHonorBean>> getMoreListLiveData() { |
| | | if (moreListLiveData == null){ |
| | | moreListLiveData = new MutableLiveData<>(); |
| | | } |
| | | dataListLiveData.setValue(beans); |
| | | return moreListLiveData; |
| | | } |
| | | |
| | | public void setMoreListLiveData(MutableLiveData<List<CertificateOfHonorBean>> moreListLiveData) { |
| | | this.moreListLiveData = moreListLiveData; |
| | | } |
| | | |
| | | public void sortDataList(int type){ |
| | | List<CertificateOfHonorBean> list = dataListLiveData.getValue(); |
| | | if (list == null || list.isEmpty()){ |
| | | return; |
| | | } |
| | | if (type == 0) { |
| | | // 从新到旧,null排最后 |
| | | list.sort((o1, o2) -> { |
| | | String happenTime1 = o1.getCreateTime(); |
| | | String happenTime2 = o2.getCreateTime(); |
| | | if (TextUtils.isEmpty(happenTime1) && TextUtils.isEmpty(happenTime2)) { |
| | | return 0; |
| | | } else if (TextUtils.isEmpty(happenTime1)) { |
| | | return 1; |
| | | } else if (TextUtils.isEmpty(happenTime2)) { |
| | | return -1; |
| | | } else { |
| | | Date date1 = Utils.parseDate(happenTime1); |
| | | Date date2 = Utils.parseDate(happenTime2); |
| | | if (date1 != null && date2 != null) { |
| | | return date2.compareTo(date1); |
| | | } |
| | | return 0; |
| | | } |
| | | }); |
| | | } else if (type == 1) { |
| | | // 从旧到新,null排最前 |
| | | list.sort((o1, o2) -> { |
| | | String happenTime1 = o1.getCreateTime(); |
| | | String happenTime2 = o2.getCreateTime(); |
| | | if (TextUtils.isEmpty(happenTime1) && TextUtils.isEmpty(happenTime2)) { |
| | | return 0; |
| | | } else if (TextUtils.isEmpty(happenTime1)) { |
| | | return -1; |
| | | } else if (TextUtils.isEmpty(happenTime2)) { |
| | | return 1; |
| | | } else { |
| | | Date date1 = Utils.parseDate(happenTime1); |
| | | Date date2 = Utils.parseDate(happenTime2); |
| | | if (date1 != null && date2 != null) { |
| | | return date1.compareTo(date2); |
| | | } |
| | | return 0; |
| | | } |
| | | }); |
| | | } |
| | | dataListLiveData.postValue(list); |
| | | } |
| | | |
| | | } |