package com.gh.gamecenter.adapter; import android.app.Activity; import android.content.Intent; import android.text.Html; import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; import androidx.core.content.ContextCompat; import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView.ViewHolder; import com.gh.common.constant.Config; import com.gh.common.util.CheckLoginUtils; import com.gh.common.util.CommentUtils; import com.gh.common.util.ConcernContentUtils; import com.gh.common.util.DataCollectionUtils; import com.gh.common.util.DialogUtils; import com.gh.common.util.DirectUtils; import com.gh.gamecenter.common.utils.ExtensionsKt; import com.gh.gamecenter.core.utils.DisplayUtils; import com.gh.gamecenter.common.constant.EntranceConsts; import com.gh.gamecenter.common.utils.ImageUtils; import com.gh.gamecenter.core.utils.MtaHelper; import com.gh.common.util.NewsUtils; import com.gh.gamecenter.core.utils.NumberUtils; import com.gh.gamecenter.core.utils.StringUtils; import com.gh.gamecenter.common.utils.TextHelper; import com.gh.gamecenter.common.utils.TimestampUtils; import com.gh.gamecenter.NewsDetailActivity; import com.gh.gamecenter.R; import com.gh.gamecenter.ShareCardActivity; import com.gh.gamecenter.ShareCardPicActivity; import com.gh.gamecenter.WebActivity; import com.gh.gamecenter.adapter.viewholder.CommentHeadViewHolder; import com.gh.gamecenter.adapter.viewholder.CommentViewHolder; import com.gh.gamecenter.common.viewholder.FooterViewHolder; import com.gh.gamecenter.adapter.viewholder.NewsDigestViewHolder; import com.gh.gamecenter.databinding.CommentHeadItemBinding; import com.gh.gamecenter.databinding.NewsDigestItemBinding; import com.gh.gamecenter.entity.ArticleCommentParent; import com.gh.gamecenter.entity.CommentEntity; import com.gh.gamecenter.entity.ConcernEntity; import com.gh.gamecenter.manager.VisitManager; import com.gh.gamecenter.common.retrofit.JSONObjectResponse; import com.gh.gamecenter.common.retrofit.OkHttpCache; import com.gh.gamecenter.common.retrofit.Response; import com.gh.gamecenter.retrofit.RetrofitManager; import com.lightgame.adapter.BaseRecyclerAdapter; import com.lightgame.utils.Utils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.atomic.AtomicBoolean; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import retrofit2.HttpException; /** * Created by khy on 2016/11/8. * 消息详情-数据适配器 */ public class MessageDetailAdapter extends BaseRecyclerAdapter { private static final int ITEM_TOP = 100; private static final int ITEM_TITLE = 101; private static final int ITEM_COMMENT = 102; private static final int ITEM_FOOTER = 103; private ConcernEntity mConcernEntity; private OnCommentCallBackListener mOnCommentCallBackListener; private RecyclerView mRecyclerView; private List mHotCommentList; private List mNormalCommentList; private String mEntrance; private boolean isOver; private boolean isLoading; private boolean isNetworkError; private boolean isRefreshPosition; private int mPage; public MessageDetailAdapter(Activity context, OnCommentCallBackListener listener, RecyclerView messageDetailRv, ConcernEntity concernEntity, String entrance) { super(context); mRecyclerView = messageDetailRv; mEntrance = entrance; mOnCommentCallBackListener = listener; isOver = false; isLoading = false; isNetworkError = false; isRefreshPosition = true; mPage = 1; mHotCommentList = new ArrayList<>(); mNormalCommentList = new ArrayList<>(); mConcernEntity = concernEntity; if (mConcernEntity != null && mConcernEntity.getCommentnum() != 0) { addHotComment(); } else if (mConcernEntity != null) { isOver = true; notifyItemChanged(getItemCount() - 1); if (mOnCommentCallBackListener != null) { mOnCommentCallBackListener.onCommentCallback(null); } } } public void addHotComment() { RetrofitManager.getInstance().getApi().getHotComment(mConcernEntity.getId(), 10, 1) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response>() { @Override public void onResponse(List response) { if (response.size() != 0) { mHotCommentList.clear(); mHotCommentList.addAll(response); notifyDataSetChanged(); } addNormalComment(true); // 考虑到断网刷新问题 } @Override public void onFailure(HttpException e) { addNormalComment(true); } }); } public void addNormalComment() { addNormalComment(false); } public void addNormalComment(boolean isRefresh) { if (isLoading) { return; } if (isRefresh) { mNormalCommentList.clear(); mPage = 1; } isLoading = true; RetrofitManager.getInstance().getApi().getComment(mConcernEntity.getId(), 10, mPage, Utils.getTime(mContext)) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response>() { @Override public void onResponse(List response) { if (response.size() < 10) { isOver = true; } if (response.size() != 0) { mNormalCommentList.addAll(response); } notifyDataSetChanged(); mPage++; isLoading = false; } @Override public void onFailure(HttpException e) { isLoading = false; isNetworkError = true; notifyDataSetChanged(); } }); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view; switch (viewType) { case ITEM_TOP: return new NewsDigestViewHolder(NewsDigestItemBinding.inflate(mLayoutInflater, parent, false)); case ITEM_TITLE: return new CommentHeadViewHolder(CommentHeadItemBinding.inflate(mLayoutInflater, parent, false)); case ITEM_COMMENT: view = mLayoutInflater.inflate(R.layout.comment_item, parent, false); return new CommentViewHolder(view); case ITEM_FOOTER: view = mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false); return new FooterViewHolder(view); default: return null; } } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof NewsDigestViewHolder) { initNewsDigestViewHolder((NewsDigestViewHolder) holder); } else if (holder instanceof CommentViewHolder) { initCommentViewHolder((CommentViewHolder) holder, position); } else if (holder instanceof FooterViewHolder) { initFooterViewHolder((FooterViewHolder) holder); } else if (holder instanceof CommentHeadViewHolder) { ((CommentHeadViewHolder) holder).binding.getRoot().setBackgroundColor(ContextCompat.getColor(mContext, R.color.background_white)); ((CommentHeadViewHolder) holder).binding.commentHeadTitle.setTextColor(ContextCompat.getColor(mContext, R.color.text_3a3a3a)); if (mHotCommentList.size() != 0 && position == 1) { ((CommentHeadViewHolder) holder).binding.commentHeadTitle.setText(R.string.comment_hot); } else { ((CommentHeadViewHolder) holder).binding.commentHeadTitle.setText(R.string.comment_new); } } } @Override public int getItemViewType(int position) { int index; if (mHotCommentList.size() == 0) { index = 1; } else { index = 2; } if (position == 0 && mConcernEntity != null) { return ITEM_TOP; } else if (mHotCommentList.size() != 0 && position == 1 || mNormalCommentList.size() != 0 && mHotCommentList.size() + index == position) { return ITEM_TITLE; } else if (getItemCount() == position + 1) { return ITEM_FOOTER; } return ITEM_COMMENT; } @Override public int getItemCount() { int itemCount = 0; if (mHotCommentList.size() != 0) { itemCount = itemCount + mHotCommentList.size() + 1; } if (mNormalCommentList.size() != 0) { itemCount = itemCount + mNormalCommentList.size() + 1; } if (mConcernEntity != null) { itemCount = itemCount + 1; } return itemCount + 1; } private void initNewsDigestViewHolder(final NewsDigestViewHolder viewHolder) { viewHolder.binding.getRoot().setBackground(ContextCompat.getDrawable(mContext, R.drawable.reuse_listview_item_style)); viewHolder.binding.newsDigestTitle.setTextColor(ContextCompat.getColor(mContext, R.color.text_black)); viewHolder.binding.newsDigestContent.setTextColor(ContextCompat.getColor(mContext, R.color.text_3a3a3a)); viewHolder.binding.newsDigestCommentnum.setTextColor(ContextCompat.getColor(mContext, R.color.text_subtitleDesc)); viewHolder.binding.newsDigestReadNum.setTextColor(ContextCompat.getColor(mContext, R.color.text_subtitleDesc)); if (mConcernEntity.getViews() != 0) { viewHolder.binding.newsDigestReadNum.setText(NumberUtils.transSimpleCount(mConcernEntity.getViews())); } viewHolder.binding.newsDigestCommentnum.setText(NumberUtils.transSimpleCount(mConcernEntity.getCommentnum())); if (mConcernEntity.getBrief() != null) { viewHolder.binding.newsDigestContent.setText(Html.fromHtml(mConcernEntity.getBrief())); viewHolder.binding.newsDigestContent.setMaxLines(100); } else { viewHolder.binding.newsDigestContent.setText(Html.fromHtml(mConcernEntity.getContent())); viewHolder.binding.newsDigestContent.setMaxLines(5); } if (mConcernEntity.getImg().size() == 0) { viewHolder.binding.newsDigestLlImg.setVisibility(View.GONE); viewHolder.binding.newsDigestLlImg.removeAllViews(); } else { viewHolder.binding.newsDigestLlImg.setVisibility(View.VISIBLE); viewHolder.binding.newsDigestLlImg.removeAllViews(); ConcernContentUtils.addContentPic(mContext, viewHolder.binding.newsDigestLlImg, mConcernEntity.getImg(), StringUtils.buildString(mEntrance, "+(消息详情)"), mContext.getResources().getDisplayMetrics().widthPixels - DisplayUtils.dip2px(mContext, 34)); } if (mConcernEntity.getGame() != null) { viewHolder.binding.newsDigestThumb.displayGameIcon(mConcernEntity.getGame().getIcon(), mConcernEntity.getGame().getIconSubscript(), mConcernEntity.getGame().getIconFloat()); } else { viewHolder.binding.newsDigestThumb.displayGameIcon(mConcernEntity.getGameIcon(), null, mConcernEntity.getGame().getIconFloat()); } viewHolder.binding.newsDigestTitle.setText(mConcernEntity.getGameName()); NewsUtils.setNewsPublishOn(viewHolder.binding.newsDigestTime, mConcernEntity.getTime()); viewHolder.binding.newsDigestShare.setOnClickListener(v -> { if (mConcernEntity.getImg() != null && mConcernEntity.getImg().size() > 0) { ShareCardPicActivity.startShareCardPicActivity(mContext, mConcernEntity, mEntrance); } else { String shareContent; if (mConcernEntity.getBrief() != null) { shareContent = mConcernEntity.getBrief(); } else { shareContent = mConcernEntity.getContent(); } mContext.startActivity(ShareCardActivity.getIntent(mContext, mConcernEntity, shareContent)); } }); viewHolder.binding.getRoot().setOnClickListener(v -> { DataCollectionUtils.uploadClick(mContext, "详情", "消息详情", mConcernEntity.getTitle()); // 统计阅读量 statNewsViews(mConcernEntity.getId()); if (mConcernEntity.getLink() != null) { mContext.startActivity(WebActivity.getIntentByNews(mContext, mConcernEntity , StringUtils.buildString(mEntrance, "+(消息详情)"))); } else { Intent intent = new Intent(mContext, NewsDetailActivity.class); intent.putExtra(EntranceConsts.KEY_NEWSID, mConcernEntity.getId()); intent.putExtra(EntranceConsts.KEY_ENTRANCE, StringUtils.buildString(mEntrance, "+(消息详情)")); mContext.startActivity(intent); } }); viewHolder.binding.newsDigestComment.setOnClickListener(v -> { if (mOnCommentCallBackListener != null) { mOnCommentCallBackListener.onCommentCallback(null); } }); viewHolder.binding.getRoot().post(() -> { if (isRefreshPosition) { Timer timer = new Timer(); // 延迟半秒,防止出现闪屏现象 timer.schedule(new TimerTask() { @Override public void run() { mRecyclerView.smoothScrollBy(0, viewHolder.binding.getRoot().getHeight()); //定位到评论顶部 } }, 300); isRefreshPosition = false; } }); } private void initCommentViewHolder(final CommentViewHolder holder, int position) { holder.itemView.setBackground(ContextCompat.getDrawable(mContext, R.drawable.reuse_listview_item_style)); holder.commentContentTv.setTextColor(ContextCompat.getColor(mContext, R.color.text_title)); holder.commentTimeTv.setTextColor(ContextCompat.getColor(mContext, R.color.text_subtitleDesc)); holder.commentReply.setTextColor(ContextCompat.getColor(mContext, R.color.text_title)); holder.commentUserNameTv.setTextColor(ContextCompat.getColor(mContext, R.color.text_subtitle)); holder.badgeNameTv.setTextColor(ContextCompat.getColor(mContext, R.color.text_subtitleDesc)); holder.quoteAuthorTv.setTextColor(ContextCompat.getColor(mContext, R.color.text_subtitle)); holder.commentLikeIv.setImageResource(R.drawable.comment_vote_unselect); holder.quoteContainer.setBackground(ContextCompat.getDrawable(mContext, R.drawable.community_comment_reply_background)); AtomicBoolean isChildLongClick = new AtomicBoolean(false); int index; if (mHotCommentList.size() == 0) { index = 2; } else { index = 3; } int commentPosition = 0;// boolean isHotComment = false; //区分热门评论和最新评论 - 修改缓存链接 CommentEntity commentEntity = null; // 初始化数据 if (mHotCommentList.size() != 0 && mHotCommentList.size() > position - 2) { commentEntity = mHotCommentList.get(position - 2); isHotComment = true; } else if (mNormalCommentList.size() != 0 && mNormalCommentList.size() > position - mHotCommentList.size() - index) { commentPosition = position - mHotCommentList.size() - index; commentEntity = mNormalCommentList.get(position - mHotCommentList.size() - index); isHotComment = false; } if (commentEntity == null) { return; } TextHelper.highlightTextThatIsWrappedInsideWrapperByDefault(holder.commentContentTv, commentEntity.getContent()); ArticleCommentParent parent = commentEntity.getParent(); if (parent != null && !TextUtils.isEmpty(parent.getUser().getName())) { holder.quoteContainer.setVisibility(View.VISIBLE); holder.quoteAuthorTv.setText(String.format("@%s", parent.getUser().getName())); if (parent.getUser().getBadge() != null) { holder.quoteAuthorBadgeSdv.setVisibility(View.VISIBLE); ImageUtils.display(holder.quoteAuthorBadgeSdv, parent.getUser().getBadge().getIcon()); } else { holder.quoteAuthorBadgeSdv.setVisibility(View.GONE); } String content; if (parent.getActive()) { content = parent.getComment(); holder.quoteContentTv.setTextColor(mContext.getResources().getColor(R.color.text_5d5d5d)); } else { content = mContext.getString(R.string.comment_hide_hint); holder.quoteContentTv.setTextColor(mContext.getResources().getColor(R.color.text_d5d5d5)); } TextHelper.highlightTextThatIsWrappedInsideWrapperByDefault(holder.quoteContentTv, content); } else { holder.quoteContainer.setVisibility(View.GONE); } CommentUtils.setCommentUserView(mContext, holder, commentEntity); CommentUtils.setCommentTime(holder.commentTimeTv, commentEntity.getTime()); if (commentEntity.getSource() != null && !commentEntity.getSource().getRegion().isEmpty()) { holder.commentTimeTv.setText(holder.commentTimeTv.getText() + " · " + commentEntity.getSource().getRegion()); } final CommentEntity finalCommentEntity = commentEntity; final boolean finalIsHotComment = isHotComment; final int finalCommentPosition = commentPosition; holder.commentLikeContainer.setOnClickListener(v -> CheckLoginUtils.checkLogin(mContext, "资讯文章详情-评论详情-点赞", () -> CommentUtils.postVote(mContext, finalCommentEntity, holder.commentLikeCountTv, holder.commentLikeIv, () -> { int index1 = (finalCommentPosition / 10); // todo index1 有可能不等于 page //获取需要修改缓存的链接 String cacheUrl; if (finalIsHotComment) { cacheUrl = StringUtils.buildString(Config.API_HOST, "article/", mConcernEntity.getId(), "/comments?filter=order:hot&page_size=10", "&page=1"); // 热门评论固定链接 } else { cacheUrl = StringUtils.buildString(Config.API_HOST, "article/", mConcernEntity.getId(), "/comments?page_size=10&page=", String.valueOf(index1)); } modifyVolleyCache(finalCommentEntity.getId(), cacheUrl); //修改缓存 }) )); holder.itemView.setOnClickListener(v -> { if (isChildLongClick.get()) { isChildLongClick.set(false); return; } if (holder.commentReply.getVisibility() == View.VISIBLE) { CheckLoginUtils.checkLogin(mContext, "资讯文章详情-评论详情-回复", () -> { mOnCommentCallBackListener.onCommentCallback(finalCommentEntity); }); } }); holder.commentContentTv.setOnLongClickListener(v -> { isChildLongClick.set(true); ExtensionsKt.copyTextAndToast(holder.commentContentTv.getText().toString(), "复制成功"); return true; }); holder.quoteContentTv.setOnLongClickListener(v -> { isChildLongClick.set(true); ExtensionsKt.copyTextAndToast(holder.quoteContentTv.getText().toString(), "复制成功"); return true; }); holder.commentMore.setOnClickListener(v -> CommentUtils.showReportDialog(finalCommentEntity, mContext, true, "资讯文章详情-评论详情")); holder.commentUserNameTv.setOnClickListener(v -> DirectUtils.directToHomeActivity(mContext, finalCommentEntity.getUser().getId(), mEntrance, "文章-评论详情")); holder.commentUserIconDv.setOnClickListener(v -> DirectUtils.directToHomeActivity(mContext, finalCommentEntity.getUser().getId(), mEntrance, "文章-评论详情")); holder.userBadgeSdv.setOnClickListener(v -> { DialogUtils.showViewBadgeDialog(mContext, finalCommentEntity.getUser().getBadge(), () -> { MtaHelper.onEvent("进入徽章墙_用户记录", "资讯文章-评论列表", finalCommentEntity.getUser().getName() + "(" + finalCommentEntity.getUser().getId() + ")"); MtaHelper.onEvent("徽章中心", "进入徽章中心", "资讯文章-评论列表"); DirectUtils.directToBadgeWall(mContext, finalCommentEntity.getUser().getId(), finalCommentEntity.getUser().getName(), finalCommentEntity.getUser().getIcon()); }); }); holder.badgeNameTv.setOnClickListener(v -> holder.userBadgeSdv.performClick()); holder.quoteAuthorBadgeSdv.setOnClickListener(v -> { DialogUtils.showViewBadgeDialog(mContext, parent.getUser().getBadge(), () -> { MtaHelper.onEvent("进入徽章墙_用户记录", "资讯文章-评论列表", parent.getUser().getName() + "(" + parent.getUser().getId() + ")"); MtaHelper.onEvent("徽章中心", "进入徽章中心", "资讯文章-评论列表"); DirectUtils.directToBadgeWall(mContext, parent.getUser().getId(), parent.getUser().getName(), parent.getUser().getIcon()); }); }); if (commentEntity.getPriority() != 0) { holder.commentBadge.setVisibility(View.VISIBLE); } else { holder.commentBadge.setVisibility(View.GONE); } } private void initFooterViewHolder(final FooterViewHolder viewHolder) { if (isNetworkError) { viewHolder.getLoading().setVisibility(View.GONE); viewHolder.getHint().setText(R.string.loading_error_network); } else if (!isOver) { viewHolder.getHint().setText(R.string.loading); viewHolder.getLoading().setVisibility(View.VISIBLE); } else if (mNormalCommentList.size() == 0 && mHotCommentList.size() == 0) { viewHolder.getLoading().setVisibility(View.GONE); viewHolder.getHint().setText(R.string.comment_empty); } else { viewHolder.getHint().setText(R.string.comment_nomore); viewHolder.getLoading().setVisibility(View.GONE); } viewHolder.itemView.setOnClickListener(v -> { if (isNetworkError) { isNetworkError = false; viewHolder.getLoading().setVisibility(View.VISIBLE); viewHolder.getHint().setText(R.string.loading); addHotComment(); notifyDataSetChanged(); } }); } private void statNewsViews(final String news_id) { RetrofitManager.getInstance().getApi().postArticleVisit(news_id) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new JSONObjectResponse() { @Override public void onResponse(JSONObject response) { if (response.length() != 0) { try { if ("success".equals(response.getString("status"))) { mConcernEntity.setViews(mConcernEntity.getViews() + 1); notifyItemChanged(0); // 更新okhttp缓存数据 VisitManager.updateOkhttpCache(mContext, mConcernEntity.getId()); } } catch (JSONException e) { e.printStackTrace(); } } } }); } private void modifyVolleyCache(String id, String url) { if (url == null) { return; } url = TimestampUtils.addTimestamp(url); byte[] data = OkHttpCache.getCache(mContext, url); if (data != null) { try { JSONArray jsonArray = new JSONArray(new String(data)); JSONObject jsonObject; for (int i = 0, size = jsonArray.length(); i < size; i++) { jsonObject = jsonArray.getJSONObject(i); if (jsonObject.getString("_id").equals(id)) { jsonObject.put("vote", jsonObject.getInt("vote") + 1); break; } } OkHttpCache.updateCache(mContext, url, jsonArray.toString().getBytes()); } catch (JSONException e) { e.printStackTrace(); } } } public ConcernEntity getConcernEntity() { return mConcernEntity; } public boolean isLoading() { return isLoading; } // 往位置0添加评论 public void addNormalComment(CommentEntity commentEntity) { mNormalCommentList.add(findTheLastPriorComment(), commentEntity); } public void addCommentCount() { mConcernEntity.setCommentnum(mConcernEntity.getCommentnum()); notifyDataSetChanged(); } public int findTheLastPriorComment() { int lastPriorityPosition = 0; for (int i = 0; i < mNormalCommentList.size(); i++) { if (mNormalCommentList.get(i).getPriority() != 0) { lastPriorityPosition = i + 1; } } return lastPriorityPosition; } public int getHotCommentListSize() { int index = 0; if (mHotCommentList.size() != 0) { index = mHotCommentList.size() + 1; } return index; } public void addConcernEntity(ConcernEntity concernEntity) { this.mConcernEntity = concernEntity; } public boolean isOver() { return isOver; } }