| | |
| | | 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.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.FamilyAssetsBean; |
| | | import com.application.zhangshi_app_android.bean.FamilyAssetsResponseBean; |
| | | import com.application.zhangshi_app_android.bean.FamilyAssetsRequestBean; |
| | | import com.application.zhangshi_app_android.bean.FamilyMemorabiliaBean; |
| | | import com.application.zhangshi_app_android.bean.HomeDevicesBean; |
| | | import com.application.zhangshi_app_android.bean.PageResponseBean; |
| | | import com.application.zhangshi_app_android.data.DataRepository; |
| | | |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Comparator; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | |
| | | import io.reactivex.Observer; |
| | | import io.reactivex.disposables.Disposable; |
| | | import io.reactivex.functions.Action; |
| | | |
| | | /** |
| | | * @author Ljj |
| | |
| | | * @desc |
| | | */ |
| | | public class FamilyAssetsActivityViewModel extends BaseViewModel<DataRepository> { |
| | | |
| | | private MutableLiveData<List<FamilyAssetsBean>> checkListLiveData;//选中的数据 |
| | | private MutableLiveData<List<FamilyAssetsBean>> dataListLiveData;//所有数据 |
| | | private MutableLiveData<List<FamilyAssetsBean>> moreListLiveData;//加载更多数据 |
| | | |
| | | private MutableLiveData<FamilyAssetsRequestBean> requestBeanLiveData;//请求参数 |
| | | |
| | | public FamilyAssetsActivityViewModel(@NonNull Application application) { |
| | | super(application); |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取家庭资产 |
| | | */ |
| | | public void getFamilyAssets(){ |
| | | model.getFamilyAssets(new HashMap<>()) |
| | | if (getRequestBeanLiveData().getValue() == null){ |
| | | getRequestBeanLiveData().setValue(new FamilyAssetsRequestBean()); |
| | | } |
| | | getRequestBeanLiveData().getValue().setPageNum(1); |
| | | model.getFamilyAssets(getRequestBeanLiveData().getValue().toMap()) |
| | | .compose(RxUtils.schedulersTransformer()) |
| | | .subscribe(new Observer<ResultData<FamilyAssetsResponseBean>>() { |
| | | .subscribe(new Observer<ResultData<PageResponseBean<FamilyAssetsBean>>>() { |
| | | @Override |
| | | public void onSubscribe(Disposable d) { |
| | | addSubscribe(d); |
| | | } |
| | | |
| | | @Override |
| | | public void onNext(ResultData<FamilyAssetsResponseBean> data) { |
| | | public void onNext(ResultData<PageResponseBean<FamilyAssetsBean>> data) { |
| | | if (data.getCode() == CODE_SUCCESS){ |
| | | if (data.getData().getData().isEmpty()){ |
| | | changeStateView(StateViewEnum.DATA_NULL); |
| | |
| | | }); |
| | | } |
| | | |
| | | public MutableLiveData<List<FamilyAssetsBean>> getCheckListLiveData() { |
| | | return checkListLiveData; |
| | | /** |
| | | * 加载更多 |
| | | */ |
| | | public void loadMoreFamilyAssets(){ |
| | | if (getRequestBeanLiveData().getValue() == null){ |
| | | getRequestBeanLiveData().setValue(new FamilyAssetsRequestBean()); |
| | | } |
| | | getRequestBeanLiveData().getValue().setPageNum(getRequestBeanLiveData().getValue().getPageNum() + 1); |
| | | model.getFamilyAssets(getRequestBeanLiveData().getValue().toMap()) |
| | | .compose(RxUtils.schedulersTransformer()) |
| | | .doFinally(new Action() { |
| | | @Override |
| | | public void run() throws Exception { |
| | | changeStateView(StateViewEnum.DATA_FINISH); |
| | | } |
| | | }) |
| | | .subscribe(new Observer<ResultData<PageResponseBean<FamilyAssetsBean>>>() { |
| | | @Override |
| | | public void onSubscribe(Disposable d) { |
| | | addSubscribe(d); |
| | | } |
| | | |
| | | @Override |
| | | public void onNext(ResultData<PageResponseBean<FamilyAssetsBean>> 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 setCheckListLiveData(MutableLiveData<List<FamilyAssetsBean>> checkListLiveData) { |
| | | this.checkListLiveData = checkListLiveData; |
| | | } |
| | | |
| | | public MutableLiveData<List<FamilyAssetsBean>> getDataListLiveData() { |
| | | if (dataListLiveData == null){ |
| | |
| | | return dataListLiveData; |
| | | } |
| | | |
| | | public MutableLiveData<FamilyAssetsRequestBean> getRequestBeanLiveData() { |
| | | if (requestBeanLiveData == null){ |
| | | requestBeanLiveData = new MutableLiveData<>(); |
| | | } |
| | | if (requestBeanLiveData.getValue() == null){ |
| | | requestBeanLiveData.setValue(new FamilyAssetsRequestBean()); |
| | | } |
| | | return requestBeanLiveData; |
| | | } |
| | | |
| | | public void setRequestBeanLiveData(MutableLiveData<FamilyAssetsRequestBean> requestBeanLiveData) { |
| | | this.requestBeanLiveData = requestBeanLiveData; |
| | | } |
| | | |
| | | public void setDataListLiveData(MutableLiveData<List<FamilyAssetsBean>> dataListLiveData) { |
| | | this.dataListLiveData = dataListLiveData; |
| | | } |
| | | |
| | | public MutableLiveData<List<FamilyAssetsBean>> getMoreListLiveData() { |
| | | if (moreListLiveData == null){ |
| | | moreListLiveData = new MutableLiveData<>(); |
| | | } |
| | | return moreListLiveData; |
| | | } |
| | | |
| | | public void setMoreListLiveData(MutableLiveData<List<FamilyAssetsBean>> moreListLiveData) { |
| | | this.moreListLiveData = moreListLiveData; |
| | | } |
| | | |
| | | public void sortDataList(int type){ |
| | |
| | | if (list == null || list.isEmpty()){ |
| | | return; |
| | | } |
| | | |
| | | list.sort(new Comparator<FamilyAssetsBean>() { |
| | | @Override |
| | | public int compare(FamilyAssetsBean o1, FamilyAssetsBean 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 (type == 0){ |
| | | if (type == 0) { |
| | | // 从新到旧,null排最后 |
| | | list.sort((o1, o2) -> { |
| | | String happenTime1 = o1.getHappenTime(); |
| | | String happenTime2 = o2.getHappenTime(); |
| | | 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); |
| | | }else { |
| | | } |
| | | return 0; |
| | | } |
| | | }); |
| | | } else if (type == 1) { |
| | | // 从旧到新,null排最前 |
| | | list.sort((o1, o2) -> { |
| | | String happenTime1 = o1.getHappenTime(); |
| | | String happenTime2 = o2.getHappenTime(); |
| | | 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; |
| | | } |
| | | return 0; |
| | | } |
| | | }); |
| | | }); |
| | | } |
| | | dataListLiveData.postValue(list); |
| | | } |
| | | |
| | | /** |
| | | * 本家数据 置顶 |
| | | */ |
| | | public void ownTop(){ |
| | | List<FamilyAssetsBean> list = getDataListLiveData().getValue(); |
| | | if (list == null || list.isEmpty()){ |
| | | return; |
| | | } |
| | | //根据ownData值为1的 放到前面 |
| | | list.sort((o1, o2) -> { |
| | | if (o1.getOwnData() == 1 && o2.getOwnData() == 0) { |
| | | return -1; |
| | | } else if (o1.getOwnData() == 0 && o2.getOwnData() == 1) { |
| | | return 1; |
| | | } |
| | | return 0; |
| | | }); |
| | | getDataListLiveData().setValue(list); |
| | | } |
| | | } |