From 059db6fb31880b279ea30bc45c5048d7941cc30a Mon Sep 17 00:00:00 2001 From: Stypox Date: Tue, 11 Apr 2023 14:56:04 +0200 Subject: [PATCH] Add replies button to comments --- .../holder/CommentInfoItemHolder.java | 56 ++++++++++++++----- .../org/schabi/newpipe/util/Localization.java | 5 ++ app/src/main/res/layout/list_comment_item.xml | 38 ++++++++----- app/src/main/res/values/strings.xml | 4 ++ 4 files changed, 74 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentInfoItemHolder.java b/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentInfoItemHolder.java index afe641c18..f1ac72e89 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentInfoItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentInfoItemHolder.java @@ -9,6 +9,7 @@ import android.text.style.URLSpan; import android.util.Log; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; @@ -57,15 +58,20 @@ public class CommentInfoItemHolder extends InfoItemHolder { private final RelativeLayout itemRoot; private final ImageView itemThumbnailView; private final TextView itemContentView; + private final ImageView itemThumbsUpView; private final TextView itemLikesCountView; private final TextView itemTitleView; private final ImageView itemHeartView; private final ImageView itemPinnedView; + private final Button repliesButton; private final CompositeDisposable disposables = new CompositeDisposable(); - @Nullable private Description commentText; - @Nullable private StreamingService streamService; - @Nullable private String streamUrl; + @Nullable + private Description commentText; + @Nullable + private StreamingService streamService; + @Nullable + private String streamUrl; public CommentInfoItemHolder(final InfoItemBuilder infoItemBuilder, final ViewGroup parent) { @@ -74,10 +80,12 @@ public class CommentInfoItemHolder extends InfoItemHolder { itemRoot = itemView.findViewById(R.id.itemRoot); itemThumbnailView = itemView.findViewById(R.id.itemThumbnailView); itemContentView = itemView.findViewById(R.id.itemCommentContentView); + itemThumbsUpView = itemView.findViewById(R.id.detail_thumbs_up_img_view); itemLikesCountView = itemView.findViewById(R.id.detail_thumbs_up_count_view); itemTitleView = itemView.findViewById(R.id.itemTitleView); itemHeartView = itemView.findViewById(R.id.detail_heart_image_view); itemPinnedView = itemView.findViewById(R.id.detail_pinned_view); + repliesButton = itemView.findViewById(R.id.replies_button); commentHorizontalPadding = (int) infoItemBuilder.getContext() .getResources().getDimension(R.dimen.comments_horizontal_padding); @@ -97,6 +105,8 @@ public class CommentInfoItemHolder extends InfoItemHolder { } final CommentsInfoItem item = (CommentsInfoItem) infoItem; + + // load the author avatar PicassoHelper.loadAvatar(item.getUploaderAvatars()).into(itemThumbnailView); if (ImageStrategy.shouldLoadImages()) { itemThumbnailView.setVisibility(View.VISIBLE); @@ -109,6 +119,10 @@ public class CommentInfoItemHolder extends InfoItemHolder { } itemThumbnailView.setOnClickListener(view -> openCommentAuthor(item)); + + // setup the top row, with pinned icon, author name and comment date + itemPinnedView.setVisibility(item.isPinned() ? View.VISIBLE : View.GONE); + final String uploadDate; if (item.getUploadDate() != null) { uploadDate = Localization.relativeTime(item.getUploadDate().offsetDateTime()); @@ -117,9 +131,29 @@ public class CommentInfoItemHolder extends InfoItemHolder { } itemTitleView.setText(Localization.concatenateStrings(item.getUploaderName(), uploadDate)); - itemPinnedView.setVisibility(item.isPinned() ? View.VISIBLE : View.GONE); + + // setup bottom row, with likes, heart and replies button + if (item.getLikeCount() >= 0) { + itemLikesCountView.setText( + Localization.shortCount( + itemBuilder.getContext(), + item.getLikeCount())); + } else { + itemLikesCountView.setText("-"); + } + itemHeartView.setVisibility(item.isHeartedByUploader() ? View.VISIBLE : View.GONE); + final boolean hasReplies = item.getReplies() != null; + repliesButton.setOnClickListener(hasReplies ? (v) -> openRepliesFragment() : null); + repliesButton.setVisibility(hasReplies ? View.VISIBLE : View.GONE); + repliesButton.setText(hasReplies + ? Localization.replyCount(itemBuilder.getContext(), item.getReplyCount()) : ""); + ((RelativeLayout.LayoutParams) itemThumbsUpView.getLayoutParams()).topMargin = + hasReplies ? 0 : DeviceUtils.dpToPx(6, itemBuilder.getContext()); + + + // setup comment content and click listeners to expand/ellipsize it try { streamService = NewPipe.getService(item.getServiceId()); } catch (final ExtractionException e) { @@ -135,16 +169,6 @@ public class CommentInfoItemHolder extends InfoItemHolder { //noinspection ClickableViewAccessibility itemContentView.setOnTouchListener(CommentTextOnTouchListener.INSTANCE); - if (item.getLikeCount() >= 0) { - itemLikesCountView.setText( - Localization.shortCount( - itemBuilder.getContext(), - item.getLikeCount())); - } else { - itemLikesCountView.setText("-"); - } - - itemView.setOnClickListener(view -> { toggleEllipsize(); if (itemBuilder.getOnCommentsSelectedListener() != null) { @@ -278,4 +302,8 @@ public class CommentInfoItemHolder extends InfoItemHolder { onCompletion); } } + + private void openRepliesFragment() { + // TODO + } } diff --git a/app/src/main/java/org/schabi/newpipe/util/Localization.java b/app/src/main/java/org/schabi/newpipe/util/Localization.java index c4034252d..20d21d76b 100644 --- a/app/src/main/java/org/schabi/newpipe/util/Localization.java +++ b/app/src/main/java/org/schabi/newpipe/util/Localization.java @@ -209,6 +209,11 @@ public final class Localization { deletedCount, shortCount(context, deletedCount)); } + public static String replyCount(final Context context, final int replyCount) { + return getQuantity(context, R.plurals.replies, 0, replyCount, + String.valueOf(replyCount)); + } + public static String getDurationString(final long duration) { final String output; diff --git a/app/src/main/res/layout/list_comment_item.xml b/app/src/main/res/layout/list_comment_item.xml index 3e0cce2e7..631ab204b 100644 --- a/app/src/main/res/layout/list_comment_item.xml +++ b/app/src/main/res/layout/list_comment_item.xml @@ -16,7 +16,6 @@ android:layout_height="42dp" android:layout_alignParentStart="true" android:layout_alignParentTop="true" - android:layout_marginStart="3dp" android:layout_marginEnd="@dimen/comment_item_avatar_right_margin" android:focusable="false" android:src="@drawable/placeholder_person" @@ -31,39 +30,37 @@ android:layout_marginEnd="@dimen/video_item_detail_pinned_right_margin" android:layout_toEndOf="@+id/itemThumbnailView" android:contentDescription="@string/detail_pinned_comment_view_description" - android:src="@drawable/ic_pin" - android:visibility="gone" - tools:visibility="visible" /> + android:src="@drawable/ic_pin" /> + tools:text="Author Name, Lorem ipsum • 5 months ago" /> + tools:text="@tools:sample/lorem/random[1]" /> @@ -71,26 +68,37 @@ + android:src="@drawable/ic_heart" /> + +