<commit_before>package com.leavjenn.hews.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.leavjenn.hews.Constants;
import com.leavjenn.hews.R;
import com.leavjenn.hews.SharedPrefsManager;
import com.leavjenn.hews.Utils;
import com.leavjenn.hews.model.Comment;
import com.leavjenn.hews.model.HNItem;
import com.leavjenn.hews.model.Post;
import com.leavjenn.hews.ui.CommentsActivity;
import com.leavjenn.hews.ui.widget.CommentDialogFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int VIEW_TYPE_HEADER = 0;
    private static final int VIEW_TYPE_COMMENT = 1;
    private static final int VIEW_TYPE_FOOTER = 2;

    Context mContext;
    RecyclerView mRecyclerView;
    ArrayList<HNItem> mItemList;
    Map<Long, List<Comment>> mCollapsedChildrenCommentsIndex;
    Map<Long, List<Comment>> mCollapsedOlderCommentsIndex;
    static SharedPreferences prefs;
    Typeface mFont;
    float mTextSize, mLineHeight;
    int mLoadingState = 0;
    int mCommentIndentColorOrange, mCommentIndentColorBg, mCommentIndentWidth;

    public CommentAdapter(Context context, RecyclerView recyclerView) {
        mContext = context;
        mRecyclerView = recyclerView;
        mItemList = new ArrayList<>();
        mCollapsedChildrenCommentsIndex = new HashMap<>();
        mCollapsedOlderCommentsIndex = new HashMap<>();
        prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        updateCommentPrefs();
        setCommentIndentStyle();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View v;
        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case VIEW_TYPE_COMMENT:
                v = LayoutInflater.from(viewGroup.getContext()).inflate(
                        R.layout.list_item_comment,
                        viewGroup, false);
                CommentViewHolder vh = new CommentViewHolder(v,
                        new CommentViewHolder.ViewHolderClickListener() {
                            @Override
                            public void onClick(int position) {
                                if (isInBetweenCommentsCollapsed(position)) {
                                    expandInBetweenComments(position);
                                } else {
                                    collapseInBetweenComments(position);
                                }
                            }

                            @Override
                            public void onClickComment(int position) {
                                if (isChildrenCommentsCollapsed(position)) {
                                    expandChildrenComments(position);
                                } else {
                                    collapseChildrenComment(position);
                                }
                            }

                            @Override
                            public void onLongClick(int position) {
                                showDialog(position);
                            }
                        });

                vh.tvComment.setTypeface(mFont);
                vh.tvComment.setPaintFlags(vh.tvComment.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
                vh.tvComment.setTextSize(mTextSize);
                vh.tvComment.setLineSpacing(0, mLineHeight);
                vh.setCommentIndentStripeStyle(mCommentIndentColorOrange,
                        mCommentIndentColorBg, mCommentIndentWidth);
                viewHolder = vh;
                break;
            case VIEW_TYPE_HEADER:
                v = LayoutInflater.from(viewGroup.getContext())
                        .inflate(R.layout.list_item_comment_header, viewGroup, false);
                viewHolder = new CommentHeaderViewHolder(v);
                break;
            case VIEW_TYPE_FOOTER:
                v = LayoutInflater.from(viewGroup.getContext())
                        .inflate(R.layout.list_item_comment_footer, viewGroup, false);
                viewHolder = new CommentFooterViewHolder(v);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof CommentViewHolder) {
            // which one is better?
//        if (getItemViewType(position) == VIEW_TYPE_COMMENT) {
            CommentViewHolder commentViewHolder = (CommentViewHolder) viewHolder;
            Comment comment = (Comment) mItemList.get(position);
            commentViewHolder.setCommentIndent(comment.getLevel());
            String text = comment.getDeleted() ? "[deleted]"
                    : Html.fromHtml(comment.getText()
                    .replace("<p>", "<br /><br />").replace("\n", "<br />")).toString();
            commentViewHolder.tvComment.setText(text);
            commentViewHolder.tvTime.setText(Utils.formatTime(comment.getTime()));
            if (!comment.getDeleted()) {
                commentViewHolder.tvAuthor.setText(comment.getBy());
            }

            if (mCollapsedChildrenCommentsIndex.containsKey(comment.getId())) {
                commentViewHolder.tvComment.setText(
                        (mCollapsedChildrenCommentsIndex.get(comment.getId()).size() + 1)
                                + " comments collapsed");
                commentViewHolder.tvComment.setMinLines(2);
                commentViewHolder.tvComment.setGravity(Gravity.CENTER);
            } else {
                commentViewHolder.tvComment.setText(text);
                commentViewHolder.tvComment.setMinLines(Integer.MIN_VALUE);
                commentViewHolder.tvComment.setGravity(Gravity.LEFT);
            }

            if (mCollapsedOlderCommentsIndex.containsKey(comment.getId())) {
                commentViewHolder.tvCollapseOlderComments.setVisibility(View.VISIBLE);
                commentViewHolder.tvCollapseOlderComments.setText(
                        " +" + mCollapsedOlderCommentsIndex.get(comment.getId()).size() + " ");
            } else {<commit_msg>fixed (links in comment unclickable): change comment type from String to CharSequence<commit_after>package com.leavjenn.hews.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.leavjenn.hews.Constants;
import com.leavjenn.hews.R;
import com.leavjenn.hews.SharedPrefsManager;
import com.leavjenn.hews.Utils;
import com.leavjenn.hews.model.Comment;
import com.leavjenn.hews.model.HNItem;
import com.leavjenn.hews.model.Post;
import com.leavjenn.hews.ui.CommentsActivity;
import com.leavjenn.hews.ui.widget.CommentDialogFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int VIEW_TYPE_HEADER = 0;
    private static final int VIEW_TYPE_COMMENT = 1;
    private static final int VIEW_TYPE_FOOTER = 2;

    Context mContext;
    RecyclerView mRecyclerView;
    ArrayList<HNItem> mItemList;
    Map<Long, List<Comment>> mCollapsedChildrenCommentsIndex;
    Map<Long, List<Comment>> mCollapsedOlderCommentsIndex;
    static SharedPreferences prefs;
    Typeface mFont;
    float mTextSize, mLineHeight;
    int mLoadingState = 0;
    int mCommentIndentColorOrange, mCommentIndentColorBg, mCommentIndentWidth;

    public CommentAdapter(Context context, RecyclerView recyclerView) {
        mContext = context;
        mRecyclerView = recyclerView;
        mItemList = new ArrayList<>();
        mCollapsedChildrenCommentsIndex = new HashMap<>();
        mCollapsedOlderCommentsIndex = new HashMap<>();
        prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        updateCommentPrefs();
        setCommentIndentStyle();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View v;
        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case VIEW_TYPE_COMMENT:
                v = LayoutInflater.from(viewGroup.getContext()).inflate(
                        R.layout.list_item_comment,
                        viewGroup, false);
                CommentViewHolder vh = new CommentViewHolder(v,
                        new CommentViewHolder.ViewHolderClickListener() {
                            @Override
                            public void onClick(int position) {
                                if (isInBetweenCommentsCollapsed(position)) {
                                    expandInBetweenComments(position);
                                } else {
                                    collapseInBetweenComments(position);
                                }
                            }

                            @Override
                            public void onClickComment(int position) {
                                if (isChildrenCommentsCollapsed(position)) {
                                    expandChildrenComments(position);
                                } else {
                                    collapseChildrenComment(position);
                                }
                            }

                            @Override
                            public void onLongClick(int position) {
                                showDialog(position);
                            }
                        });

                vh.tvComment.setTypeface(mFont);
                vh.tvComment.setPaintFlags(vh.tvComment.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
                vh.tvComment.setTextSize(mTextSize);
                vh.tvComment.setLineSpacing(0, mLineHeight);
                vh.setCommentIndentStripeStyle(mCommentIndentColorOrange,
                        mCommentIndentColorBg, mCommentIndentWidth);
                viewHolder = vh;
                break;
            case VIEW_TYPE_HEADER:
                v = LayoutInflater.from(viewGroup.getContext())
                        .inflate(R.layout.list_item_comment_header, viewGroup, false);
                viewHolder = new CommentHeaderViewHolder(v);
                break;
            case VIEW_TYPE_FOOTER:
                v = LayoutInflater.from(viewGroup.getContext())
                        .inflate(R.layout.list_item_comment_footer, viewGroup, false);
                viewHolder = new CommentFooterViewHolder(v);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof CommentViewHolder) {
            // which one is better?
//        if (getItemViewType(position) == VIEW_TYPE_COMMENT) {
            CommentViewHolder commentViewHolder = (CommentViewHolder) viewHolder;
            Comment comment = (Comment) mItemList.get(position);
            commentViewHolder.setCommentIndent(comment.getLevel());
            CharSequence text = comment.getDeleted() ? "[deleted]"
                    : Html.fromHtml(comment.getText()
                    .replace("<p>", "<br /><br />").replace("\n", "<br />"));
            commentViewHolder.tvComment.setText(text);
            commentViewHolder.tvTime.setText(Utils.formatTime(comment.getTime()));
            if (!comment.getDeleted()) {
                commentViewHolder.tvAuthor.setText(comment.getBy());
            }

            if (mCollapsedChildrenCommentsIndex.containsKey(comment.getId())) {
                commentViewHolder.tvComment.setText(
                        (mCollapsedChildrenCommentsIndex.get(comment.getId()).size() + 1)
                                + " comments collapsed");
                commentViewHolder.tvComment.setMinLines(2);
                commentViewHolder.tvComment.setGravity(Gravity.CENTER);
            } else {
                commentViewHolder.tvComment.setText(text);
                commentViewHolder.tvComment.setMinLines(Integer.MIN_VALUE);
                commentViewHolder.tvComment.setGravity(Gravity.LEFT);
            }

            if (mCollapsedOlderCommentsIndex.containsKey(comment.getId())) {
                commentViewHolder.tvCollapseOlderComments.setVisibility(View.VISIBLE);
                commentViewHolder.tvCollapseOlderComments.setText(
                        " +" + mCollapsedOlderCommentsIndex.get(comment.getId()).size() + " ");
            } else {
{'fraction_non_alphanumeric': 0.08428835489833642, 'fraction_numerical': 0.0014787430683918669, 'mean_word_length': 2.047082676278441, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1890234', 'n_tokens_mistral': 3569, 'n_tokens_neox': 3366, 'n_words': 570}