From 0bef61a45f211cb9cc525f8c9760d5d0560ed157 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 9 Apr 2021 23:20:46 +0800 Subject: Tweak the design of comments. --- .../CommentAndPostRecyclerViewAdapter.java | 156 ++++----------------- .../CommentsListingRecyclerViewAdapter.java | 18 +-- .../customviews/CommentIndentationView.java | 68 +++++++++ 3 files changed, 100 insertions(+), 142 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/customviews/CommentIndentationView.java (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java index 63c7f230..91f819f9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java @@ -111,6 +111,7 @@ import ml.docilealligator.infinityforreddit.comment.Comment; import ml.docilealligator.infinityforreddit.comment.FetchComment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.AspectRatioGifImageView; +import ml.docilealligator.infinityforreddit.customviews.CommentIndentationView; import ml.docilealligator.infinityforreddit.customviews.MarkwonLinearLayoutManager; import ml.docilealligator.infinityforreddit.fragments.ViewPostDetailFragment; import ml.docilealligator.infinityforreddit.post.Post; @@ -237,6 +238,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter 0) { - switch (comment.getDepth() % 7) { - case 0: - ((CommentViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor7); - break; - case 1: - ((CommentViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor1); - break; - case 2: - ((CommentViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor2); - break; - case 3: - ((CommentViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor3); - break; - case 4: - ((CommentViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor4); - break; - case 5: - ((CommentViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor5); - break; - case 6: - ((CommentViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor6); - break; - } - ViewGroup.LayoutParams params = ((CommentViewHolder) holder).verticalBlock.getLayoutParams(); - params.width = 12; - ((CommentViewHolder) holder).verticalBlock.setLayoutParams(params); - } + ((CommentViewHolder) holder).commentIndentationView.setLevelAndColors(comment.getDepth(), verticalBlockColors); if (comment.hasReply()) { if (comment.isExpanded()) { @@ -1205,85 +1182,13 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter 0) { - switch (comment.getDepth() % 7) { - case 0: - ((CommentFullyCollapsedViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor7); - break; - case 1: - ((CommentFullyCollapsedViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor1); - break; - case 2: - ((CommentFullyCollapsedViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor2); - break; - case 3: - ((CommentFullyCollapsedViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor3); - break; - case 4: - ((CommentFullyCollapsedViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor4); - break; - case 5: - ((CommentFullyCollapsedViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor5); - break; - case 6: - ((CommentFullyCollapsedViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor6); - break; - } - ViewGroup.LayoutParams params = ((CommentFullyCollapsedViewHolder) holder).verticalBlock.getLayoutParams(); - params.width = 12; - ((CommentFullyCollapsedViewHolder) holder).verticalBlock.setLayoutParams(params); - } + ((CommentFullyCollapsedViewHolder) holder).commentIndentationView.setLevelAndColors(comment.getDepth(), verticalBlockColors); } else if (holder instanceof LoadMoreChildCommentsViewHolder) { Comment placeholder; placeholder = mIsSingleCommentThreadMode ? mVisibleComments.get(holder.getBindingAdapterPosition() - 2) : mVisibleComments.get(holder.getBindingAdapterPosition() - 1); - ((LoadMoreChildCommentsViewHolder) holder).itemView.setPadding(placeholder.getDepth() * 24, 0, 0, 0); - if (placeholder.getDepth() > 0) { - switch (placeholder.getDepth() % 7) { - case 0: - ((LoadMoreChildCommentsViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor7); - break; - case 1: - ((LoadMoreChildCommentsViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor1); - break; - case 2: - ((LoadMoreChildCommentsViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor2); - break; - case 3: - ((LoadMoreChildCommentsViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor3); - break; - case 4: - ((LoadMoreChildCommentsViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor4); - break; - case 5: - ((LoadMoreChildCommentsViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor5); - break; - case 6: - ((LoadMoreChildCommentsViewHolder) holder).verticalBlock - .setBackgroundColor(mCommentVerticalBarColor6); - break; - } - - ViewGroup.LayoutParams params = ((LoadMoreChildCommentsViewHolder) holder).verticalBlock.getLayoutParams(); - params.width = 12; - ((LoadMoreChildCommentsViewHolder) holder).verticalBlock.setLayoutParams(params); - } + ((LoadMoreChildCommentsViewHolder) holder).commentIndentationView.setLevelAndColors(placeholder.getDepth(), verticalBlockColors); if (placeholder.isLoadingMoreChildren()) { ((LoadMoreChildCommentsViewHolder) holder).placeholderTextView.setText(R.string.loading); @@ -1989,16 +1894,12 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { + itemView.setOnClickListener(view -> { int position = getAdapterPosition(); if (position < 0) { return; @@ -536,7 +532,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter { if (commentMarkdownView.getSelectionStart() == -1 && commentMarkdownView.getSelectionEnd() == -1) { - linearLayout.callOnClick(); + itemView.callOnClick(); } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/CommentIndentationView.java b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/CommentIndentationView.java new file mode 100644 index 00000000..1887702b --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/CommentIndentationView.java @@ -0,0 +1,68 @@ +package ml.docilealligator.infinityforreddit.customviews; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.DashPathEffect; +import android.graphics.Paint; +import android.graphics.Path; +import android.util.AttributeSet; +import android.widget.LinearLayout; + +import androidx.annotation.Nullable; + +import java.util.ArrayList; + +import ml.docilealligator.infinityforreddit.utils.Utils; + +public class CommentIndentationView extends LinearLayout { + + private final Paint paint; + private int level; + private int[] colors; + private ArrayList paths; + private final int spacing; + private final int pathWidth; + + public CommentIndentationView(Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + setWillNotDraw(false); + paint = new Paint(Paint.ANTI_ALIAS_FLAG); + pathWidth = (int) Utils.convertDpToPixel(2, context); + spacing = pathWidth * 6; + paint.setStrokeWidth(pathWidth); + paint.setStyle(Paint.Style.FILL_AND_STROKE); + paint.setPathEffect(new DashPathEffect(new float[] { pathWidth * 2, pathWidth * 2 }, 0)); + paths = new ArrayList<>(); + } + + @Override + protected void onLayout(boolean changed, int left, int top, int right, int bottom) { + super.onLayout(changed, left, top, right, bottom); + + for (int i = 0; i < level; i++) { + float startX = spacing * (i + 1) + pathWidth; + Path path = new Path(); + path.moveTo(startX, 0); + path.lineTo(startX, getHeight()); + paths.add(path); + } + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + for (int i = 0; i < paths.size(); i++) { + paint.setColor(colors[i % 7]); + canvas.drawPath(paths.get(i), paint); + } + } + + public void setLevelAndColors(int level, int[] colors) { + paths.clear(); + this.colors = colors; + this.level = level; + int indentationSpacing = (int) (level * spacing + pathWidth); + setPaddingRelative(indentationSpacing, 0, pathWidth, 0); + } +} -- cgit v1.2.3