CommentHelper.java 4.21 KB
package com.people.comment.utils;

import android.app.Activity;
import android.view.View;

import androidx.appcompat.widget.AppCompatTextView;

import com.people.comment.R;
import com.people.comment.bean.CommentClickShowType;
import com.people.comment.listener.CommentClickListener;
import com.people.comment.view.easy_pop.EasyPopup;
import com.people.comment.view.easy_pop.XGravity;
import com.people.comment.view.easy_pop.YGravity;
import com.wd.common.utils.PDUtils;
import com.wd.common.utils.ProcessUtils;
import com.wd.foundation.wdkitcore.tools.AppContext;

import java.util.Arrays;

/**
 * @Author LiuKun
 * @date:2023/5/15
 * @desc:弹出回复、分享、复制、删除、举报选择
 */
public class CommentHelper {

    private String[] cellStr = new String[]{"回复", "分享", "复制", "删除", "举报"};
    private AppCompatTextView[] textViews = new AppCompatTextView[4];
    private View[] lineViews = new View[3];
    private final int[] textIds = new int[]{R.id.comment_popup_cell_1, R.id.comment_popup_cell_2, R.id.comment_popup_cell_3, R.id.comment_popup_cell_4};
    private final int[] lineIds = new int[]{R.id.comment_popup_cell_line_1, R.id.comment_popup_cell_line_2, R.id.comment_popup_cell_line_3};
    private EasyPopup mCommentPop;

    private static class SingletonHolder {
        private final static CommentHelper instance = new CommentHelper();
    }

    public static CommentHelper getInstance() {
        return SingletonHolder.instance;
    }

    private CommentHelper() {
    }


    /**
     * 显示效果
     */
    public void showClickDialog(Activity activity, View anchor, final CommentClickListener listener, @CommentClickShowType final int... showTypes) {
        // 排序
        Arrays.sort(showTypes);
        // 在华为P30 Pro 上会跳一下(先在屏幕右边后面又居中),设置一下宽度,后面可以替换为BubbleManager用法
        int size = showTypes.length;
        final float cellWidth = AppContext.getContext().getResources().getDimension(R.dimen.rmrb_dp45);
        final float lintWidth = AppContext.getContext().getResources().getDimension(R.dimen.rmrb_dp30);

        int width = (int) (cellWidth * size + lintWidth * (size - 1));

        mCommentPop = EasyPopup.create()
                .setContentView(activity, R.layout.layout_popup_comment_click_show)
                .setAnimationStyle(R.style.RightPopAnim)
                .setWidth(width)
                .setOnViewListener((view, basePopup) -> {
                    int length = showTypes.length;
                    for (int i = 0; i < length; i++) {
                        final int showType = showTypes[i];
                        textViews[i] = view.findViewById(textIds[i]);
                        textViews[i].setText(cellStr[showType]);
                        textViews[i].setVisibility(View.VISIBLE);
                        textViews[i].setOnClickListener(v -> {
                            mCommentPop.dismiss();
                            if (listener != null) {
                                if (!PDUtils.isLogin()) {
                                    switch (showType) {
                                        case CommentClickShowType.reply:
                                        case CommentClickShowType.delete:
                                        case CommentClickShowType.report:
                                            ProcessUtils.toOneKeyLoginActivity();
                                            return;
                                        default:
                                            break;
                                    }
                                }
                                listener.click(showType);
                            }
                        });
                    }

                    for (int i = 0; i < length - 1; i++) {
                        lineViews[i] = view.findViewById(lineIds[i]);
                        lineViews[i].setVisibility(View.VISIBLE);
                    }

                })
                .setOnDismissListener((EasyPopup.OnDismissListener) () -> mCommentPop = null)
                .setFocusAndOutsideEnable(true)
                .apply();
        mCommentPop.showAtAnchorView(anchor, YGravity.CENTER, XGravity.LEFT, 0, 0);
    }

}