CommentCommitDialog.java 18 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
package com.wd.comment.dialog;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.wd.comment.R;
import com.wd.comment.listener.CommitDialogListener;
import com.wd.comment.manager.ExpressionManager;
import com.wd.foundation.wdkit.constant.PageNameConstants;
import com.wd.common.dialog.LayerDialog;
import com.wd.foundation.wdkit.imageglide.ImageUtils;
import com.wd.common.permissions.IPmsCallBack;
import com.wd.common.permissions.PermissionsUtils;
import com.wd.common.widget.edittext.AutoAdaptTextSize;
import com.wd.foundation.bean.analytics.TraceBean;
import com.wd.foundation.bean.analytics.TrackContentBean;
import com.wd.foundation.bean.custom.video.VodDetailIntentBean;
import com.wd.foundation.wdkit.utils.SpUtils;
import com.wd.foundation.wdkit.utils.ToastNightUtil;
import com.wd.foundation.wdkitcore.constant.BaseConstants;
import com.wd.foundation.wdkitcore.storage.MemStoreUtils;
import com.wd.foundation.wdkitcore.tools.ResUtils;
import com.wd.foundation.wdkitcore.tools.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

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

/**
 * @Author LiuKun
 * @date:2023/5/2
 * @desc:评论弹出框
 */
public class CommentCommitDialog extends LayerDialog {

    /**
     * 上个页面传递过来的数据
     */
    public VodDetailIntentBean intentBean;
    private TrackContentBean mTrackContentBean;
    /**
     * 输入框
     */
    private AutoAdaptTextSize inputEt;
    /**
     * 语音
     */
    private ImageView voiceIv;
    /**
     * 表情
     */
    private ImageView expressionIv,expressionIvGif;
    /**
     * 发送按钮
     */
    private TextView publishBtn;
    private View uploadImgView;
    private View showImgView;
    private ImageView showImageIv;
    /**
     * 删除图片
     */
    private View delImageView;
    private Activity activity;
    private ExpressionManager expressionManager;
    /**
     * 发送文字、图片监听
     */
    private CommitDialogListener commitDialogListener;
    private String hintStr = "";
    private int maxCountLength;
    /**
     * 选择的gif图
     */
    private String gifUrl ;

    /**
     * 最近历史表情
     */
    List<String> gifRecentListStr;

    public CommentCommitDialog(Activity activity) {
        this(activity, false);
    }

    public CommentCommitDialog(Activity activity, boolean isLive) {
        super(activity, R.style.NoAnimBottom);
        this.activity = activity;
        this.maxCountLength = isLive?100:900;
    }

    /**
     * 初始化
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        this.setContentView(R.layout.dialog_commit_comment);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initDialog();
        initView();
    }

    /**
     * 初始化弹出框
     */
    private void initDialog() {
        this.setCanceledOnTouchOutside(true);
        WindowManager manager = this.getWindow().getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.width = width;
        lp.gravity = Gravity.BOTTOM;
        this.getWindow().setAttributes(lp);
    }

    /**
     * 初始化布局控件
     */
    private void initView() {
        showImgView = findViewById(R.id.cd_fl_img);
        showImageIv = findViewById(R.id.cd_iv_img);
        delImageView = findViewById(R.id.cd_iv_del_img);
        delImageView.setOnClickListener(v -> {
            showImgView.setVisibility(View.GONE);
            publishStatus();
            ImageUtils.getInstance().loadImage(showImageIv, "");
        });
        uploadImgView = findViewById(R.id.cd_iv_img_btn);
        //跳转到选择图片界面
        uploadImgView.setOnClickListener(v -> {
            if (null != expressionManager) {
                expressionManager.voicePressUpLift();
            }
            //存入临时变量,只有横屏直播间用到,showWithAddBtn
            //画中画模式时,修改选择选图片键盘弹框消失问题
            MemStoreUtils.put("selectPic", "1");
            goSelectPic();

        });
        inputEt = findViewById(R.id.cd_et_input);
        inputEt.setHint(hintStr);
        inputEt.setBackListener(textView -> dismiss());
        //语音
        voiceIv = findViewById(R.id.cd_iv_voice_btn);
        //表情
        expressionIv = findViewById(R.id.cd_iv_expression_btn);
        //定制表情
        expressionIvGif = findViewById(R.id.cd_iv_gif_btn);

        //全部表情
        List<String> gifListStr =  new ArrayList<>();
        try {
            Object obj = MemStoreUtils.getObj("default_map_group","commentgif");
            if (obj instanceof List<?>) {
                gifListStr = (List<String>) obj;
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        //没有gif图数据隐藏定制表情
        if (gifListStr == null || gifListStr.isEmpty()) {
            expressionIvGif.setVisibility(View.GONE);
        }

        //定制最近表情数据
        gifRecentListStr =  new ArrayList<>();
        try {
            String str = SpUtils.getGifIcons();
            if(!TextUtils.isEmpty(str)) {
                String[] strList = str.split(";");
                for (int i = 0; i < strList.length; i++) {
                    gifRecentListStr.add(strList[i]);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        //发表按钮
        publishBtn = findViewById(R.id.cd_tv_publish_btn);
        publishBtn.setOnClickListener(v -> {
            if (null != expressionManager) {
                expressionManager.voicePressUpLift();
            }
            String text = getText();

            if (commitDialogListener != null) {

                if (showImgView.getVisibility() == View.VISIBLE || !TextUtils.isEmpty(text)) {

                    if(showImgView.getVisibility() == View.VISIBLE ){
                        commitDialogListener.publish(text,gifUrl);

                    }else{
                        commitDialogListener.publish(text,null);
                    }

                    // 清空内容
                    inputEt.setText("");
                    showImgView.setVisibility(View.GONE);
                    //清除动图
                    gifUrl = "";

                    //评论发布点击埋点
                    if(mTrackContentBean != null){
                        if (mTrackContentBean.getTraceBean() == null) {
                            TraceBean traceBean = new TraceBean();
                            mTrackContentBean.setTraceBean(traceBean);
                        }
                        // 浏览时长
                        TraceBean traceBean = mTrackContentBean.getTraceBean();
                        traceBean.duration = 0;
                        traceBean.shareChannel = "";
                        if (intentBean != null) {
                            traceBean.expIds = intentBean.getExpIds();
                            traceBean.itemId = intentBean.getItemId();
                            traceBean.traceId = intentBean.getTraceId();
                            // pageName
                            String pageName = intentBean.getPageName();
                            if (StringUtils.isBlank(pageName)) {
                                mTrackContentBean.setPage_name(PageNameConstants.VIDEO_DETAIL_PAGE);
                            }else {
                                mTrackContentBean.setPage_name(pageName);
                            }
                            // pageId
                            String pageId = intentBean.getPageId();
                            if (StringUtils.isBlank(pageId)) {
                                mTrackContentBean.setPage_id(PageNameConstants.VIDEO_DETAIL_PAGE);
                            }else {
                                mTrackContentBean.setPage_id(pageId);
                            }
                        }
                        mTrackContentBean.setDuration(0);
                        mTrackContentBean.commentAction();
//                        CommonTrack.getInstance().contentCommentTrack(mTrackContentBean);
                    }
                }
            }
        });
        //语音录入
        View voiceLayout = findViewById(R.id.layout_voice);
        //使用旧版表情包
        View expressionLayout = findViewById(R.id.layout_expression);
        //全部表情
        View expressionGifLayout = findViewById(R.id.layout_expression_gif);
        //最近使用表情
        View expressionGifLayoutRecent = findViewById(R.id.layout_expression_gif_recent);

        //表情管理类
        expressionManager = new ExpressionManager(activity, voiceLayout, expressionLayout, expressionGifLayout,
                expressionGifLayoutRecent, inputEt, voiceIv, expressionIv, expressionIvGif,gifListStr,gifRecentListStr);
        if (maxCountLength > 0) {
            expressionManager.setMaxCountLength(maxCountLength);
        }
        expressionManager.setInputTextListener(() -> {
            publishStatus();
        });

        //更新输入框gif图
        expressionManager.setGifInputTextListener(this::uploadImgGif);
        //更新最近表情输入框gif图
        expressionManager.setRecentGifInputTextListener(this::uploadImgGif);
    }

    public void setIntentBean(VodDetailIntentBean intentBean) {
        this.intentBean = intentBean;
    }

    /**
     * 去选择图片
     */
    private void goSelectPic() {
        PermissionsUtils.getCameraAndSdPermission(activity, new IPmsCallBack() {
            @Override
            public void granted() {
//                Matisse.from(activity)
//                        .choose(EnumSet.of(MimeType.JPEG, MimeType.PNG, MimeType.GIF, MimeType.BMP), true)
//                        .countable(true)
//                        .maxSelectable(1)
//                        .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
//                        .thumbnailScale(0.85f)
//                        .imageEngine(new GlideEngine())
//                        //这两行要连用 是否在选择图片中展示照相 和适配安卓7.0 FileProvider
//                        .capture(true)
//                        .captureStrategy(new CaptureStrategy(true, BaseConstants.FILE_PROVIDER))
//                        //评论框跳转
//                        .setSkipType(0)
//                        //Glide加载方式
//                        .imageEngine(new GlideEngine())
//                        //Picasso加载方式
//                        // .imageEngine(new PicassoEngine())
//                        // 单个图片限制大小15M
//                        .addFilter(new PicSizeFilter(15* Filter.K*Filter.K))
//                        //请求码
//                        .forResult(10001);
            }

            @Override
            public void notGranted() {
//                ToastNightUtil.showShort("没有权限或拒绝权限");
            }
        });
    }

    /**
     * 发送按钮状态
     */
    private void publishStatus() {
        String text = getText().trim();
        if (showImgView.getVisibility() == View.VISIBLE || !TextUtils.isEmpty(text)) {
            publishBtn.setBackgroundResource(R.drawable.shape_comment_input_publish);
            publishBtn.setTextColor(ContextCompat.getColor(publishBtn.getContext(),
                    R.color.res_color_common_C8_keep));
        } else {
            publishBtn.setBackgroundResource(R.drawable.shape_comment_input_publish_default);
            publishBtn.setTextColor(ContextCompat.getColor(publishBtn.getContext(),
                    R.color.res_color_general_FFFFFF_50_keep));
        }
    }


    @Override
    public void show() {
        if ("1".equals(BaseConstants.cnCommentAllowed)){
            ToastNightUtil.showShort(ResUtils.getString(R.string.res_unable_to_comment_temporarilyk));
            return;
        }
        super.show();
    }

    /**
     * 带有软键盘
     */
    public void showWithSoftBoard() {
        show();
        showSoftBoard();
    }

    /**
     * hint为回复userName:
     */
    public void showUserName(String userName) {
        setHint(activity.getResources().getString(R.string.str_reply_other_user, userName));
        showWithSoftBoard();
    }

    /**
     * hint为说两句
     */
    public void showDefaultHint(int bestNoticer) {
        if (1 == bestNoticer){
            //开启最佳评论框
            setHint(activity.getResources().getString(R.string.str_best_reviewer));
        }else {
            setHint(activity.getResources().getString(R.string.res_say_two_sentences));
        }
        showWithSoftBoard();
    }

    /**
     * 显示图片选择
     */
    public void showWithAddBtn() {
        showWithSoftBoard();
        uploadImgView.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏gif定制表情入口
     */
    public void hideWithGifBtn() {
        showWithSoftBoard();
        expressionIvGif.setVisibility(View.GONE);
    }

    /**
     * 设置提示语
     */
    public void setHint(String hint) {
        this.hintStr = hint;
        if (inputEt != null) {
            inputEt.setHint(hint);
        }
    }

    public String getText() {
        return inputEt.getText().toString();
    }

    public void setCommitDialogListener(CommitDialogListener commitDialogListener) {
        this.commitDialogListener = commitDialogListener;
    }


    public void closeDialog() {
        dismiss();
        clear();
    }

    /**
     * 列表监听
     */
    public void recycler() {

        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }

        if (expressionManager != null) {
            expressionManager.recycler();
        }
    }

    /**
     * 横屏直播上传选择图片
     * @param selPaths
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void uploadImgCallback(ArrayList<String> selPaths) {
        ArrayList<String> images = new ArrayList<>();
        images.addAll(selPaths);
        if (images.size() > 0) {
            //接口上传图片
            if (commitDialogListener != null) {
                commitDialogListener.getUnloadImg(images);
            }
            if (images.size() > 0) {
                showImgView.setVisibility(View.VISIBLE);
                publishStatus();
                ImageUtils.getInstance().loadImage(showImageIv, images.get(0), R.drawable.default_placeholder_img);
            }
        }
    }

    /**
     * 选择gif图片到输入框
     * @param url
     */
    public void uploadImgGif(String url) {
        if(!TextUtils.isEmpty(url)){
            showImgView.setVisibility(View.VISIBLE);
            publishStatus();
            ImageUtils.getInstance().loadImage(showImageIv, url, R.drawable.default_placeholder_img);
            gifUrl = url;
            //最近表情存储
            if(gifRecentListStr != null) {
                if(gifRecentListStr.contains(gifUrl)){
                    gifRecentListStr.remove(gifUrl);
                }else if (gifRecentListStr.size() > 4){
                    gifRecentListStr.remove(4);
                }
                gifRecentListStr.add(0, gifUrl);
            }

            if(gifRecentListStr != null &&  gifRecentListStr.size() >0 ){
                StringBuilder stringBuilder = new StringBuilder();
                for(int i=0; i<gifRecentListStr.size(); i++){
                    stringBuilder.append(gifRecentListStr.get(i));
                    if(i < gifRecentListStr.size()-1){
                        stringBuilder.append(";");
                    }
                    if(!TextUtils.isEmpty(stringBuilder)){
                        SpUtils.saveGifIcon(stringBuilder.toString());
                    }
                }

            }
        }else{
            gifUrl = "";
        }

    }



    /**
     * 消失
     */
    @Override
    public void dismiss() {
        super.dismiss();
        expressionManager.resetStatus();
//        resetImageStatus();
    }

    /**
     * 显示软键盘
     */
    private void showSoftBoard() {
//        HandlerHelper.main().postDelayed(() -> {
//            if (activity == null || activity.isDestroyed()) {
//                return;
//            }
//            if (inputEt != null) {
//                KeyboardHelper.showSoftInput(inputEt);
//                inputEt.requestFocus();
//            }
//        }, 200);
    }

    /**
     * 清除所有输入
     */
    public void clear() {
        if (inputEt != null && inputEt.getEditableText() != null) {
            inputEt.getEditableText().clear();
        }

        resetImageStatus();
    }

    /**
     * 重置图片状态
     */
    private void resetImageStatus() {
        if (showImgView != null && showImgView.getVisibility() == View.VISIBLE) {
            showImgView.setVisibility(View.GONE);
            ImageUtils.getInstance().loadImage(showImageIv, "");
        }
    }

    /**
     * 清除输入框里的内容
     */
    public void clearEditText() {
        if (inputEt != null) {
            inputEt.setText("");
        }
        resetImageStatus();
    }
    /**
     *评论发布埋点数据
    */
    public void setTrackContentBean(TrackContentBean trackContentBean){
        this.mTrackContentBean = trackContentBean;
    }

    public TrackContentBean getmTrackContentBean(){
        return mTrackContentBean;
    }
}