Files
assistant-android/app/src/main/java/com/gh/gamecenter/adapter/CommentDetailAdapter.java

221 lines
8.3 KiB
Java

package com.gh.gamecenter.adapter;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import com.gh.common.constant.ItemViewType;
import com.gh.common.util.CheckLoginUtils;
import com.gh.common.util.CommentUtils;
import com.gh.common.util.DirectUtils;
import com.gh.common.util.TextHelper;
import com.gh.gamecenter.R;
import com.gh.gamecenter.adapter.viewholder.CommentViewHolder;
import com.gh.gamecenter.adapter.viewholder.FooterViewHolder;
import com.gh.gamecenter.entity.ArticleCommentParent;
import com.gh.gamecenter.entity.CommentEntity;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import com.lightgame.adapter.BaseRecyclerAdapter;
import com.lightgame.utils.Utils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import retrofit2.HttpException;
/**
* Created by khy on 2017/3/22.
*/
public class CommentDetailAdapter extends BaseRecyclerAdapter<ViewHolder> {
private OnCommentCallBackListener mOnCommentCallBackListener;
private List<CommentEntity> mCommentList;
private RecyclerView mRecyclerView;
private View mDoData;
private String mCommentId;
private boolean mIsOver;
private boolean mIsLoading;
private boolean mIsNetworkError;
private int mPage;
public CommentDetailAdapter(Context context, String commentId,
OnCommentCallBackListener commentCallBackListener,
View noData, RecyclerView recyclerView) {
super(context);
mCommentId = commentId;
mOnCommentCallBackListener = commentCallBackListener;
mCommentList = new ArrayList<>();
mPage = 1;
mDoData = noData;
mRecyclerView = recyclerView;
loadData();
}
public void loadData() {
if (mIsLoading) return;
mIsLoading = true;
RetrofitManager.getInstance(mContext).getApi()
.getCommentTrace(mCommentId, mPage)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<List<CommentEntity>>() {
@Override
public void onResponse(List<CommentEntity> response) {
super.onResponse(response);
mCommentList.addAll(response);
if (response.size() < 20) {
mIsOver = true;
}
mDoData.setVisibility(View.GONE);
mRecyclerView.setVisibility(View.VISIBLE);
notifyItemRangeChanged(0, getItemCount() - 1);
mPage++;
mIsLoading = false;
}
@Override
public void onFailure(HttpException e) {
super.onFailure(e);
try {
if (e != null && e.code() == 404 && e.response().errorBody().string().length() > 0) {
mDoData.setVisibility(View.VISIBLE);
mRecyclerView.setVisibility(View.GONE);
Utils.toast(mContext, R.string.content_delete_toast);
} else {
Utils.toast(mContext, R.string.comment_failure_hint);
mIsNetworkError = true;
mIsLoading = false;
notifyItemChanged(getItemCount() - 1);
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == ItemViewType.LOADING) {
View view = mLayoutInflater.inflate(R.layout.refresh_footerview, parent, false);
return new FooterViewHolder(view);
} else {
View view = mLayoutInflater.inflate(R.layout.comment_item, parent, false);
return new CommentViewHolder(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof CommentViewHolder) {
initCommentViewHolder((CommentViewHolder) holder, position);
} else if (holder instanceof FooterViewHolder) {
initFooterViewHolder((FooterViewHolder) holder);
}
}
@Override
public int getItemViewType(int position) {
if (position == getItemCount() - 1) {
return ItemViewType.LOADING;
}
return 100;
}
@Override
public int getItemCount() {
return mCommentList.size() + 1;
}
private void initCommentViewHolder(final CommentViewHolder holder, int position) {
final CommentEntity commentEntity = mCommentList.get(position);
holder.commentLine.setVisibility(View.VISIBLE);
holder.commentLineBottom.setVisibility(View.GONE);
CommentUtils.setCommentUserView(mContext, holder, commentEntity);
CommentUtils.setCommentTime(holder.commentTimeTv, commentEntity.getTime());
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()));
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);
}
holder.commentLikeIv.setOnClickListener(v -> CheckLoginUtils.checkLogin(mContext,
"资讯文章-评论-点赞", () ->
CommentUtils.postVote(mContext,
commentEntity, holder.commentLikeCountTv,
holder.commentLikeIv, null)));
holder.itemView.setOnClickListener(v ->
CommentUtils.showReportDialog(commentEntity,
mContext, false,
mOnCommentCallBackListener, null, "资讯文章-评论"));
holder.commentUserIconDv.setOnClickListener(v -> DirectUtils.directToHomeActivity(mContext, commentEntity.getUser().getId(), "", "文章-评论详情"));
holder.commentUserNameTv.setOnClickListener(v -> DirectUtils.directToHomeActivity(mContext, commentEntity.getUser().getId(), "", "文章-评论详情"));
if (commentEntity.getPriority() != 0) {
holder.commentBadge.setVisibility(View.VISIBLE);
} else {
holder.commentBadge.setVisibility(View.GONE);
}
}
private void initFooterViewHolder(FooterViewHolder holder) {
if (mIsNetworkError) {
holder.loading.setVisibility(View.GONE);
holder.hint.setText(R.string.loading_error_network);
} else if (!mIsOver) {
holder.hint.setText(R.string.loading);
holder.loading.setVisibility(View.VISIBLE);
} else if (mCommentList.size() == 0) {
holder.loading.setVisibility(View.GONE);
holder.hint.setText(R.string.comment_empty);
} else {
holder.hint.setText(R.string.comment_nomore);
holder.loading.setVisibility(View.GONE);
}
}
public boolean isOver() {
return mIsOver;
}
public boolean isLoading() {
return mIsLoading;
}
}