From ad68f784aebf44323f2c1234f5634ab10b95c517 Mon Sep 17 00:00:00 2001 From: Stypox Date: Wed, 12 Apr 2023 10:54:03 +0200 Subject: [PATCH] Extract some utility methods from CommentInfoItemHolder --- .../holder/CommentInfoItemHolder.java | 76 ++++--------------- .../holder/StreamInfoItemHolder.java | 23 +----- .../org/schabi/newpipe/util/Localization.java | 25 ++++++ .../schabi/newpipe/util/NavigationHelper.java | 29 +++++++ .../schabi/newpipe/util/ServiceHelper.java | 8 ++ 5 files changed, 80 insertions(+), 81 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 74c80c73d..03f796f59 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 @@ -1,12 +1,12 @@ package org.schabi.newpipe.info_list.holder; import static android.text.TextUtils.isEmpty; +import static org.schabi.newpipe.util.ServiceHelper.getServiceById; import android.graphics.Paint; import android.text.Layout; import android.text.method.LinkMovementMethod; import android.text.style.URLSpan; -import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.Button; @@ -15,21 +15,15 @@ import android.widget.RelativeLayout; import android.widget.TextView; import androidx.annotation.Nullable; -import androidx.appcompat.app.AppCompatActivity; import androidx.core.text.HtmlCompat; +import androidx.fragment.app.FragmentActivity; -import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.R; -import org.schabi.newpipe.error.ErrorUtil; import org.schabi.newpipe.extractor.InfoItem; -import org.schabi.newpipe.extractor.NewPipe; -import org.schabi.newpipe.extractor.ServiceList; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.comments.CommentsInfo; import org.schabi.newpipe.extractor.comments.CommentsInfoItem; -import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.stream.Description; -import org.schabi.newpipe.fragments.list.comments.CommentRepliesFragment; import org.schabi.newpipe.info_list.InfoItemBuilder; import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.util.DeviceUtils; @@ -46,7 +40,6 @@ import java.util.function.Consumer; import io.reactivex.rxjava3.disposables.CompositeDisposable; public class CommentInfoItemHolder extends InfoItemHolder { - private static final String TAG = "CommentIIHolder"; private static final String ELLIPSIS = "…"; private static final int COMMENT_DEFAULT_LINES = 2; @@ -125,30 +118,19 @@ public class CommentInfoItemHolder extends InfoItemHolder { // 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()); - } else { - uploadDate = item.getTextualUploadDate(); - } - itemTitleView.setText(Localization.concatenateStrings(item.getUploaderName(), uploadDate)); + itemTitleView.setText(Localization.concatenateStrings(item.getUploaderName(), + Localization.relativeTimeOrTextual(item.getUploadDate(), + item.getTextualUploadDate(), itemBuilder.getContext()))); // setup bottom row, with likes, heart and replies button - if (item.getLikeCount() >= 0) { - itemLikesCountView.setText( - Localization.shortCount( - itemBuilder.getContext(), - item.getLikeCount())); - } else { - itemLikesCountView.setText("-"); - } + itemLikesCountView.setText( + Localization.likeCount(itemBuilder.getContext(), item.getLikeCount())); itemHeartView.setVisibility(item.isHeartedByUploader() ? View.VISIBLE : View.GONE); final boolean hasReplies = item.getReplies() != null; - repliesButton.setOnClickListener(hasReplies ? v -> openRepliesFragment(item) : null); + repliesButton.setOnClickListener(hasReplies ? v -> openCommentReplies(item) : null); repliesButton.setVisibility(hasReplies ? View.VISIBLE : View.GONE); repliesButton.setText(hasReplies ? Localization.replyCount(itemBuilder.getContext(), item.getReplyCount()) : ""); @@ -157,14 +139,7 @@ public class CommentInfoItemHolder extends InfoItemHolder { // setup comment content and click listeners to expand/ellipsize it - try { - streamService = NewPipe.getService(item.getServiceId()); - } catch (final ExtractionException e) { - // should never happen - ErrorUtil.showUiErrorSnackbar(itemBuilder.getContext(), "Getting StreamingService", e); - Log.w(TAG, "Cannot obtain service from comment service id, defaulting to YouTube", e); - streamService = ServiceList.YouTube; - } + streamService = getServiceById(item.getServiceId()); streamUrl = item.getUrl(); commentText = item.getCommentText(); ellipsize(); @@ -193,19 +168,13 @@ public class CommentInfoItemHolder extends InfoItemHolder { } private void openCommentAuthor(final CommentsInfoItem item) { - if (isEmpty(item.getUploaderUrl())) { - return; - } - final AppCompatActivity activity = (AppCompatActivity) itemBuilder.getContext(); - try { - NavigationHelper.openChannelFragment( - activity.getSupportFragmentManager(), - item.getServiceId(), - item.getUploaderUrl(), - item.getUploaderName()); - } catch (final Exception e) { - ErrorUtil.showUiErrorSnackbar(activity, "Opening channel fragment", e); - } + NavigationHelper.openCommentAuthorIfPresent((FragmentActivity) itemBuilder.getContext(), + item); + } + + private void openCommentReplies(final CommentsInfoItem item) { + NavigationHelper.openCommentRepliesFragment((FragmentActivity) itemBuilder.getContext(), + (CommentsInfo) itemBuilder.getSourceListInfo(), item); } private void allowLinkFocus() { @@ -305,17 +274,4 @@ public class CommentInfoItemHolder extends InfoItemHolder { onCompletion); } } - - private void openRepliesFragment(final CommentsInfoItem commentsInfoItem) { - ((MainActivity) itemBuilder.getContext()) - .getSupportFragmentManager() - .beginTransaction() - .setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out, - R.animator.custom_fade_in, R.animator.custom_fade_out) - .replace(R.id.fragment_holder, - new CommentRepliesFragment((CommentsInfo) itemBuilder.getSourceListInfo(), - commentsInfoItem)) - .addToBackStack(null) - .commit(); - } } diff --git a/app/src/main/java/org/schabi/newpipe/info_list/holder/StreamInfoItemHolder.java b/app/src/main/java/org/schabi/newpipe/info_list/holder/StreamInfoItemHolder.java index a84c98404..7f80707bc 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/holder/StreamInfoItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/holder/StreamInfoItemHolder.java @@ -12,10 +12,6 @@ import org.schabi.newpipe.info_list.InfoItemBuilder; import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.util.Localization; -import androidx.preference.PreferenceManager; - -import static org.schabi.newpipe.MainActivity.DEBUG; - /* * Created by Christian Schabesberger on 01.08.16. *

@@ -81,7 +77,8 @@ public class StreamInfoItemHolder extends StreamMiniInfoItemHolder { } } - final String uploadDate = getFormattedRelativeUploadDate(infoItem); + final String uploadDate = Localization.relativeTimeOrTextual(infoItem.getUploadDate(), + infoItem.getTextualUploadDate(), itemBuilder.getContext()); if (!TextUtils.isEmpty(uploadDate)) { if (viewsAndDate.isEmpty()) { return uploadDate; @@ -92,20 +89,4 @@ public class StreamInfoItemHolder extends StreamMiniInfoItemHolder { return viewsAndDate; } - - private String getFormattedRelativeUploadDate(final StreamInfoItem infoItem) { - if (infoItem.getUploadDate() != null) { - String formattedRelativeTime = Localization - .relativeTime(infoItem.getUploadDate().offsetDateTime()); - - if (DEBUG && PreferenceManager.getDefaultSharedPreferences(itemBuilder.getContext()) - .getBoolean(itemBuilder.getContext() - .getString(R.string.show_original_time_ago_key), false)) { - formattedRelativeTime += " (" + infoItem.getTextualUploadDate() + ")"; - } - return formattedRelativeTime; - } else { - return infoItem.getTextualUploadDate(); - } - } } 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 20d21d76b..dea48651e 100644 --- a/app/src/main/java/org/schabi/newpipe/util/Localization.java +++ b/app/src/main/java/org/schabi/newpipe/util/Localization.java @@ -1,5 +1,7 @@ package org.schabi.newpipe.util; +import static org.schabi.newpipe.MainActivity.DEBUG; + import android.annotation.SuppressLint; import android.content.Context; import android.content.SharedPreferences; @@ -22,6 +24,7 @@ import org.ocpsoft.prettytime.units.Decade; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.ListExtractor; import org.schabi.newpipe.extractor.localization.ContentCountry; +import org.schabi.newpipe.extractor.localization.DateWrapper; import org.schabi.newpipe.extractor.stream.AudioStream; import org.schabi.newpipe.extractor.stream.AudioTrackType; @@ -214,6 +217,14 @@ public final class Localization { String.valueOf(replyCount)); } + public static String likeCount(final Context context, final int likeCount) { + if (likeCount < 0) { + return "-"; + } else { + return shortCount(context, likeCount); + } + } + public static String getDurationString(final long duration) { final String output; @@ -333,6 +344,20 @@ public final class Localization { return prettyTime.formatUnrounded(offsetDateTime); } + public static String relativeTimeOrTextual(final DateWrapper parsed, + final String textual, + @Nullable final Context context) { + if (parsed == null) { + return textual; + } else if (DEBUG && context != null && PreferenceManager + .getDefaultSharedPreferences(context) + .getBoolean(context.getString(R.string.show_original_time_ago_key), false)) { + return relativeTime(parsed.offsetDateTime()) + " (" + textual + ")"; + } else { + return relativeTime(parsed.offsetDateTime()); + } + } + public static void assureCorrectAppLanguage(final Context c) { final Resources res = c.getResources(); final DisplayMetrics dm = res.getDisplayMetrics(); diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java index b0d7dcf73..e127a8b55 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -1,5 +1,6 @@ package org.schabi.newpipe.util; +import static android.text.TextUtils.isEmpty; import static org.schabi.newpipe.util.ListHelper.getUrlAndNonTorrentStreams; import android.annotation.SuppressLint; @@ -17,6 +18,7 @@ import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; @@ -29,8 +31,11 @@ import org.schabi.newpipe.RouterActivity; import org.schabi.newpipe.about.AboutActivity; import org.schabi.newpipe.database.feed.model.FeedGroupEntity; import org.schabi.newpipe.download.DownloadActivity; +import org.schabi.newpipe.error.ErrorUtil; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.StreamingService; +import org.schabi.newpipe.extractor.comments.CommentsInfo; +import org.schabi.newpipe.extractor.comments.CommentsInfoItem; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.stream.AudioStream; import org.schabi.newpipe.extractor.stream.DeliveryMethod; @@ -41,6 +46,7 @@ import org.schabi.newpipe.extractor.stream.VideoStream; import org.schabi.newpipe.fragments.MainFragment; import org.schabi.newpipe.fragments.detail.VideoDetailFragment; import org.schabi.newpipe.fragments.list.channel.ChannelFragment; +import org.schabi.newpipe.fragments.list.comments.CommentRepliesFragment; import org.schabi.newpipe.fragments.list.kiosk.KioskFragment; import org.schabi.newpipe.fragments.list.playlist.PlaylistFragment; import org.schabi.newpipe.fragments.list.search.SearchFragment; @@ -476,6 +482,29 @@ public final class NavigationHelper { item.getServiceId(), uploaderUrl, item.getUploaderName()); } + public static void openCommentAuthorIfPresent(@NonNull final FragmentActivity activity, + final CommentsInfoItem comment) { + if (isEmpty(comment.getUploaderUrl())) { + return; + } + try { + openChannelFragment(activity.getSupportFragmentManager(), comment.getServiceId(), + comment.getUploaderUrl(), comment.getUploaderName()); + } catch (final Exception e) { + ErrorUtil.showUiErrorSnackbar(activity, "Opening channel fragment", e); + } + } + + public static void openCommentRepliesFragment(@NonNull final FragmentActivity activity, + final CommentsInfo commentsInfo, + final CommentsInfoItem commentsInfoItem) { + defaultTransaction(activity.getSupportFragmentManager()) + .replace(R.id.fragment_holder, + new CommentRepliesFragment(commentsInfo, commentsInfoItem)) + .addToBackStack(null) + .commit(); + } + public static void openPlaylistFragment(final FragmentManager fragmentManager, final int serviceId, final String url, @NonNull final String name) { diff --git a/app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java b/app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java index acd019ba0..68905e6c9 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java @@ -144,6 +144,14 @@ public final class ServiceHelper { .orElse(""); } + @NonNull + public static StreamingService getServiceById(final int serviceId) { + return ServiceList.all().stream() + .filter(s -> s.getServiceId() == serviceId) + .findFirst() + .orElseThrow(); + } + public static void setSelectedServiceId(final Context context, final int serviceId) { String serviceName; try {