CommentLikeHelper.java 3.61 KB

package com.wd.comment.helper;

import android.text.TextUtils;

import com.wd.comment.adapter.MyCommentListAdapter;
import com.wd.comment.comment.vm.CommentViewModel;
import com.wd.comment.listener.BatchMyCommentListLikeStatusListener;
import com.wd.foundation.bean.comment.CommentStatusBean;
import com.wd.foundation.bean.custom.content.CommentItem;
import com.wd.foundation.wdkit.utils.CommonUtil;
import com.wd.foundation.wdkit.utils.SpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论点赞帮助类
 *
 * @author ouyang
 * @version [V1.0.0, 2022/08/18]
 * @since V1.0.0
 */
public class CommentLikeHelper {

    private static final String TAG = "CommentLikeHelper";

    private CommentViewModel mLikeViewModel;

    /**
     * 请求点赞状态(我的评论列表)
     * @param startIndex 刷新的起始位置
     * @param dataBeanList 请求数据源
     * @param mAdapter 列表adapter
     */
    public void getLike(int startIndex, List<CommentItem> dataBeanList, MyCommentListAdapter mAdapter) {
        if (dataBeanList == null || dataBeanList.isEmpty()) {
            if (0 == startIndex) {
                mAdapter.replaceData(dataBeanList);
            }
            return;
        }
        if (TextUtils.isEmpty(SpUtils.getUserToken())) {
            if (0 == startIndex) {
                mAdapter.replaceData(dataBeanList);
            }
            return;
        }
        if (mLikeViewModel == null) {
            mLikeViewModel = new CommentViewModel();
        }

        List<Integer> requestParams = new ArrayList<>();
        for (CommentItem commentBean:dataBeanList) {
            /*if (commentBean.getCheckStatus()==2) {
                requestParams.add(commentBean.getId());
            }*/
            //评论点赞状态不判断审核状态
            requestParams.add(commentBean.getId());
        }

        if (CommonUtil.isEmpty(requestParams)) {
            if (startIndex == 0) {
                mAdapter.replaceData(dataBeanList);
            } else {
                mAdapter.notifyItemRangeChanged(startIndex, dataBeanList.size());
            }
            return;
        }

        if (!requestParams.isEmpty()) {
            mLikeViewModel.getMyCommentListStatusList(requestParams, new BatchMyCommentListLikeStatusListener() {

                @Override
                public void onGetCommentStatusListSuccess(List<CommentStatusBean> statusList) {
                    for (CommentItem tempCommentBean : dataBeanList) {
                        //点赞不判断审核状态
//                        if (tempCommentBean.getCheckStatus() == 2) {
                            for (CommentStatusBean tempCommentStatusBean : statusList) {
                                if (tempCommentBean.getId() == tempCommentStatusBean.getCommentId()) {
                                    tempCommentBean.setLikeStatus(tempCommentStatusBean.getStatus());
                                }
                            }
//                        }
                    }

                    if (startIndex == 0) {
                        mAdapter.replaceData(dataBeanList);
                    } else {
                        mAdapter.notifyItemRangeChanged(startIndex, dataBeanList.size());
                    }
                }

                @Override
                public void onGetCommentStatusListFailure(String errorInfo) {
                    if (startIndex == 0) {
                        mAdapter.replaceData(dataBeanList);
                    } else {
                        mAdapter.notifyItemRangeChanged(startIndex, dataBeanList.size());
                    }
                }
            });
        }
    }
}