| | |
| | | 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.LiveData; |
| | | 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.FamilyMemorabiliaBean; |
| | | import com.application.zhangshi_app_android.bean.FamilyMemorabiliaResponseBean; |
| | | import com.application.zhangshi_app_android.bean.FamilyMemorabiliaRequestBean; |
| | | import com.application.zhangshi_app_android.bean.PageResponseBean; |
| | | import com.application.zhangshi_app_android.data.DataRepository; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.Comparator; |
| | | import java.util.HashMap; |
| | | import java.util.Date; |
| | | 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> requestBeanLiveData;//请求参数 |
| | | private MutableLiveData<List<FamilyMemorabiliaBean>> moreListLiveData;//下拉加载数据 |
| | | private MutableLiveData<List<FamilyMemorabiliaBean>> finishDeleteListLiveData;//已经完成删除的数据,用于adapter的remove操作 |
| | | |
| | | //创建并初始化ViewModel |
| | | public FamilyMemorabiliaActivityViewModel(@NonNull Application application) { |
| | | super(application); |
| | | } |
| | | |
| | | |
| | | //DataRepository 是一个数据仓库类,负责管理数据的获取和存储, |
| | | // 该方法通过调用 DataRepository.getInstance() 来获取 DataRepository 的单例实例,并将其返回model。 |
| | | @Override |
| | | protected DataRepository initModel() { |
| | | return DataRepository.getInstance(); |
| | |
| | | /** |
| | | * 获取家大事记 |
| | | */ |
| | | 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()); |
| | | |
| | | |
| | | |
| | | //getRequestBeanLiveData().getValue() 检查请求数据的 LiveData 是否为空。 |
| | | // 如果为空,则创建一个新的 FamilyMemorabiliaRequestBean 对象,并将其设置为 LiveData 的值,以确保请求数据不为 null。 |
| | | public void getFamilyMemorabilia() { |
| | | if (getRequestBeanLiveData().getValue() == null){ |
| | | getRequestBeanLiveData().setValue(new FamilyMemorabiliaRequestBean()); |
| | | } |
| | | else if (searchType == 4) { |
| | | queryMap.put("createTime",stringMutableLiveData.getValue()); |
| | | } |
| | | else if (searchType == 5) { |
| | | queryMap.put("address",stringMutableLiveData.getValue()); |
| | | } |
| | | model.getFamilyMemorabilia(queryMap) |
| | | //设置请求数据对象的页码为 1,表示要获取第一页的数据。 |
| | | getRequestBeanLiveData().getValue().setPageNum(1); |
| | | |
| | | //通过model(ViewModel 中的数据仓库对象)发起网络请求,并将请求数据转换为 Map 类型,以便发送给服务器。 |
| | | model.getFamilyMemorabilia(getRequestBeanLiveData().getValue().toMap()) |
| | | |
| | | //应用线程调度器,确保观察者在正确的线程上运行。 |
| | | .compose(RxUtils.schedulersTransformer()) |
| | | .subscribe(new Observer<ResultData<FamilyMemorabiliaResponseBean>>() { |
| | | |
| | | //订阅这个网络请求的结果 |
| | | .subscribe(new Observer<ResultData<PageResponseBean<FamilyMemorabiliaBean>>>() { |
| | | @Override |
| | | |
| | | //将订阅对象添加到 addSubscribe() 中,以便在不再需要时取消订阅。 |
| | | //创建 Disposable 对象来管理订阅 |
| | | // 使用 Disposable,可以有效地管理异步操作的生命周期,确保资源在不再需要时被正确释放。 |
| | | |
| | | public void onSubscribe(Disposable d) { |
| | | addSubscribe(d); |
| | | } |
| | | |
| | | |
| | | |
| | | @Override |
| | | //onNext()处理成功获取数据的情况 |
| | | public void onNext(ResultData<PageResponseBean<FamilyMemorabiliaBean>> data) { |
| | | if (data.getCode() == CODE_SUCCESS){ |
| | | if (data.getData().getData().isEmpty()){ |
| | | |
| | | //更新界面状态为 StateViewEnum.DATA_NULL |
| | | changeStateView(StateViewEnum.DATA_NULL); |
| | | }else { |
| | | //隐藏状态视图并将数据列表发布到 dataListLiveData |
| | | changeStateView(StateViewEnum.HIDE); |
| | | dataListLiveData.postValue(data.getData().getData()); |
| | | } |
| | | |
| | | //如果返回的数据不成功,则将服务器返回的消息发布到 messageLiveData,以便通知用户。 |
| | | }else { |
| | | messageLiveData.postValue(data.getMsg()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | //onError()处理发生错误的情况,将错误消息发布到 messageLiveData。 |
| | | public void onError(Throwable e) { |
| | | messageLiveData.postValue(e.getMessage()); |
| | | } |
| | | |
| | | @Override |
| | | public void onComplete() { |
| | | |
| | | } |
| | | }); |
| | | } |
| | | /** |
| | | * 加载更多 |
| | | */ |
| | | public void getMoreFamilyMemorabilia() { |
| | | if (getRequestBeanLiveData().getValue() == null){ |
| | | getRequestBeanLiveData().setValue(new FamilyMemorabiliaRequestBean()); |
| | | } |
| | | getRequestBeanLiveData().getValue().setPageNum(getRequestBeanLiveData().getValue().getPageNum() + 1); |
| | | model.getFamilyMemorabilia(getRequestBeanLiveData().getValue().toMap()) |
| | | .compose(RxUtils.schedulersTransformer()) |
| | | .doFinally(new Action() { |
| | | @Override |
| | | public void run() throws Exception { |
| | | changeStateView(StateViewEnum.DATA_FINISH); |
| | | } |
| | | }) |
| | | .subscribe(new Observer<ResultData<PageResponseBean<FamilyMemorabiliaBean>>>() { |
| | | @Override |
| | | public void onSubscribe(Disposable d) { |
| | | addSubscribe(d); |
| | | } |
| | | |
| | | @Override |
| | | public void onNext(ResultData<FamilyMemorabiliaResponseBean> data) { |
| | | public void onNext(ResultData<PageResponseBean<FamilyMemorabiliaBean>> data) { |
| | | if (data.getCode() == CODE_SUCCESS){ |
| | | if (data.getData().getData().isEmpty()){ |
| | | changeStateView(StateViewEnum.DATA_NULL); |
| | | messageLiveData.postValue("没有更多数据了"); |
| | | }else { |
| | | dataListLiveData.postValue(data.getData().getData()); |
| | | changeStateView(StateViewEnum.HIDE); |
| | | moreListLiveData.postValue(data.getData().getData()); |
| | | } |
| | | }else { |
| | | messageLiveData.postValue(data.getMsg()); |
| | |
| | | }); |
| | | } |
| | | /** |
| | | * 增加家大事记 |
| | | * 删除家大事记 |
| | | */ |
| | | public void add(FamilyMemorabiliaBean bean) { |
| | | model.addFamilyMemorabilia(bean) |
| | | public void deleteCheckList() { |
| | | 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); |
| | | model.deleteFamilyMemorabilia(sb.toString()) |
| | | .compose(RxUtils.schedulersTransformer()) |
| | | .subscribe(new Observer<ResultData<String>>() { |
| | | @Override |
| | |
| | | @Override |
| | | public void onNext(ResultData<String> stringResultData) { |
| | | if (stringResultData.getCode() == CODE_SUCCESS){ |
| | | messageLiveData.postValue("添加成功"); |
| | | messageLiveData.postValue("删除成功"); |
| | | finishDeleteListLiveData.postValue(checkListLiveData.getValue()); |
| | | getCheckListLiveData().postValue(new ArrayList<>()); |
| | | }else { |
| | | messageLiveData.postValue(stringResultData.getMsg()); |
| | | } |
| | |
| | | }); |
| | | } |
| | | /** |
| | | * 删除家大事记 |
| | | * 删除单个数据 |
| | | */ |
| | | 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()) |
| | | public void deleteItem(FamilyMemorabiliaBean bean) { |
| | | model.deleteFamilyMemorabilia(String.valueOf(bean.getId())) |
| | | .compose(RxUtils.schedulersTransformer()) |
| | | .subscribe(new Observer<ResultData<String>>() { |
| | | @Override |
| | |
| | | @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); |
| | | List<FamilyMemorabiliaBean> list = new ArrayList<>(); |
| | | list.add(bean); |
| | | finishDeleteListLiveData.postValue(list); |
| | | messageLiveData.postValue("删除成功"); |
| | | getCheckListLiveData().postValue(new ArrayList<>()); |
| | | }else { |
| | | messageLiveData.postValue(stringResultData.getMsg()); |
| | | } |
| | |
| | | this.checkListLiveData = checkListLiveData; |
| | | } |
| | | |
| | | @NonNull //NonNull 用于标记方法参数、返回值、字段或方法的返回类型不允许为 null |
| | | |
| | | //getRequestBeanLiveData() 获取一个包含 FamilyMemorabiliaRequestBean 数据的 MutableLiveData 对象。 |
| | | public MutableLiveData<FamilyMemorabiliaRequestBean> getRequestBeanLiveData() { |
| | | if (requestBeanLiveData == null){ |
| | | requestBeanLiveData = new MutableLiveData<>(); |
| | | } |
| | | if (requestBeanLiveData.getValue() == null){ |
| | | requestBeanLiveData.setValue(new FamilyMemorabiliaRequestBean()); |
| | | } |
| | | return requestBeanLiveData; |
| | | } |
| | | |
| | | public void setRequestBeanLiveData(MutableLiveData<FamilyMemorabiliaRequestBean> requestBeanLiveData) { |
| | | this.requestBeanLiveData = requestBeanLiveData; |
| | | } |
| | | |
| | | public MutableLiveData<List<FamilyMemorabiliaBean>> getDataListLiveData() { |
| | | if (dataListLiveData == null){ |
| | | dataListLiveData = new MutableLiveData<>(); |
| | |
| | | this.dataListLiveData = dataListLiveData; |
| | | } |
| | | |
| | | public MutableLiveData<String> getStringMutableLiveData() { |
| | | if (stringMutableLiveData == null){ |
| | | stringMutableLiveData = new MutableLiveData<>(); |
| | | @NonNull |
| | | 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; |
| | | } |
| | | |
| | | @NonNull |
| | | 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 LiveData<List<FamilyMemorabiliaBean>> getFinishDeleteListLiveData() { |
| | | if (finishDeleteListLiveData == null){ |
| | | finishDeleteListLiveData = new MutableLiveData<>(); |
| | | } |
| | | return finishDeleteListLiveData; |
| | | } |
| | | |
| | | public void setFinishDeleteListLiveData(MutableLiveData<List<FamilyMemorabiliaBean>> finishDeleteListLiveData) { |
| | | this.finishDeleteListLiveData = finishDeleteListLiveData; |
| | | } |
| | | |
| | | /** |
| | | * 改变排序 |
| | | * @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()); |
| | | List<FamilyMemorabiliaBean> list = getDataListLiveData().getValue(); |
| | | if (list == null || list.isEmpty()){ |
| | | return; |
| | | } |
| | | if (b) { |
| | | // 从新到旧,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); |
| | | } |
| | | return 0; |
| | | } |
| | | }); |
| | | }else { |
| | | dataList.sort(new Comparator<FamilyMemorabiliaBean>() { |
| | | @Override |
| | | public int compare(FamilyMemorabiliaBean o1, FamilyMemorabiliaBean o2) { |
| | | return (int) (o2.getId() - o1.getId()); |
| | | } else { |
| | | // 从旧到新,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; |
| | | } |
| | | }); |
| | | } |
| | | getDataListLiveData().postValue(dataList); |
| | | getDataListLiveData().setValue(list); |
| | | } |
| | | |
| | | /** |
| | | * 本家数据 置顶 |
| | | */ |
| | | public void ownTop(){ |
| | | List<FamilyMemorabiliaBean> 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); |
| | | } |
| | | } |