From 3f37e27852a2d7559722bc69e7233ded68730ab1 Mon Sep 17 00:00:00 2001 From: Stypox Date: Wed, 12 Apr 2023 15:18:26 +0200 Subject: [PATCH] Add some documentation and javadocs Also further simplify CommentRepliesInfo and RelatedItemsInfo --- .../list/comments/CommentRepliesFragment.java | 4 +-- .../list/comments/CommentRepliesInfo.java | 25 ++++++++--------- .../list/videos/RelatedItemInfo.java | 28 ------------------- .../list/videos/RelatedItemsFragment.java | 22 +++++++-------- .../list/videos/RelatedItemsInfo.java | 22 +++++++++++++++ .../holder/CommentInfoItemHolder.java | 4 +-- .../holder/StreamInfoItemHolder.java | 5 ++-- .../org/schabi/newpipe/util/Localization.java | 23 +++++++++++++-- .../schabi/newpipe/util/NavigationHelper.java | 7 +++++ .../schabi/newpipe/util/ServiceHelper.java | 5 ++++ 10 files changed, 83 insertions(+), 62 deletions(-) delete mode 100644 app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedItemInfo.java create mode 100644 app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedItemsInfo.java diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesFragment.java index 163e7d25f..2e0f0d7c6 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesFragment.java @@ -89,7 +89,7 @@ public final class CommentRepliesFragment // setup author name and comment date binding.authorName.setText(item.getUploaderName()); binding.uploadDate.setText(Localization.relativeTimeOrTextual( - item.getUploadDate(), item.getTextualUploadDate(), getContext())); + getContext(), item.getUploadDate(), item.getTextualUploadDate())); binding.authorTouchArea.setOnClickListener( v -> NavigationHelper.openCommentAuthorIfPresent(requireActivity(), item)); @@ -140,7 +140,7 @@ public final class CommentRepliesFragment @Override protected Single loadResult(final boolean forceLoad) { - return Single.fromCallable(() -> CommentRepliesInfo.getInfo(commentsInfoItem, + return Single.fromCallable(() -> new CommentRepliesInfo(commentsInfoItem, // the reply count string will be shown as the activity title Localization.replyCount(requireContext(), commentsInfoItem.getReplyCount()))); } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesInfo.java b/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesInfo.java index 7f85dc6bf..cc160c395 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesInfo.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesInfo.java @@ -7,19 +7,16 @@ import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; import java.util.Collections; public final class CommentRepliesInfo extends ListInfo { - private CommentRepliesInfo(final int serviceId, - final ListLinkHandler listUrlIdHandler, - final String name) { - super(serviceId, listUrlIdHandler, name); - } - - public static CommentRepliesInfo getInfo(final CommentsInfoItem comment, final String name) { - final ListLinkHandler handler = - new ListLinkHandler("", "", "", Collections.emptyList(), null); - final CommentRepliesInfo relatedItemInfo = new CommentRepliesInfo( - comment.getServiceId(), handler, name); // the name will be shown as fragment title - relatedItemInfo.setNextPage(comment.getReplies()); - relatedItemInfo.setRelatedItems(Collections.emptyList()); // since it must be non-null - return relatedItemInfo; + /** + * This class is used to wrap the comment replies page into a ListInfo object. + * + * @param comment the comment from which to get replies + * @param name will be shown as the fragment title + */ + public CommentRepliesInfo(final CommentsInfoItem comment, final String name) { + super(comment.getServiceId(), + new ListLinkHandler("", "", "", Collections.emptyList(), null), name); + setNextPage(comment.getReplies()); + setRelatedItems(Collections.emptyList()); // since it must be non-null } } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedItemInfo.java b/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedItemInfo.java deleted file mode 100644 index 9d2985082..000000000 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedItemInfo.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.schabi.newpipe.fragments.list.videos; - -import org.schabi.newpipe.extractor.InfoItem; -import org.schabi.newpipe.extractor.ListInfo; -import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; -import org.schabi.newpipe.extractor.stream.StreamInfo; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public final class RelatedItemInfo extends ListInfo { - private RelatedItemInfo(final int serviceId, - final ListLinkHandler listUrlIdHandler, - final String name) { - super(serviceId, listUrlIdHandler, name); - } - - public static RelatedItemInfo getInfo(final StreamInfo info) { - final ListLinkHandler handler = new ListLinkHandler( - info.getOriginalUrl(), info.getUrl(), info.getId(), Collections.emptyList(), null); - final RelatedItemInfo relatedItemInfo = new RelatedItemInfo( - info.getServiceId(), handler, info.getName()); - final List relatedItems = new ArrayList<>(info.getRelatedItems()); - relatedItemInfo.setRelatedItems(relatedItems); - return relatedItemInfo; - } -} diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedItemsFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedItemsFragment.java index 5ec316420..e46937ede 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedItemsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedItemsFragment.java @@ -27,11 +27,11 @@ import java.util.function.Supplier; import io.reactivex.rxjava3.core.Single; -public class RelatedItemsFragment extends BaseListInfoFragment +public class RelatedItemsFragment extends BaseListInfoFragment implements SharedPreferences.OnSharedPreferenceChangeListener { private static final String INFO_KEY = "related_info_key"; - private RelatedItemInfo relatedItemInfo; + private RelatedItemsInfo relatedItemsInfo; /*////////////////////////////////////////////////////////////////////////// // Views @@ -68,7 +68,7 @@ public class RelatedItemsFragment extends BaseListInfoFragment getListHeaderSupplier() { - if (relatedItemInfo == null || relatedItemInfo.getRelatedItems() == null) { + if (relatedItemsInfo == null || relatedItemsInfo.getRelatedItems() == null) { return null; } @@ -96,8 +96,8 @@ public class RelatedItemsFragment extends BaseListInfoFragment loadResult(final boolean forceLoad) { - return Single.fromCallable(() -> relatedItemInfo); + protected Single loadResult(final boolean forceLoad) { + return Single.fromCallable(() -> relatedItemsInfo); } @Override @@ -109,7 +109,7 @@ public class RelatedItemsFragment extends BaseListInfoFragment { + /** + * This class is used to wrap the related items of a StreamInfo into a ListInfo object. + * + * @param info the stream info from which to get related items + */ + public RelatedItemsInfo(final StreamInfo info) { + super(info.getServiceId(), new ListLinkHandler(info.getOriginalUrl(), info.getUrl(), + info.getId(), Collections.emptyList(), null), info.getName()); + setRelatedItems(new ArrayList<>(info.getRelatedItems())); + } +} 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 03f796f59..ef949a745 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 @@ -119,8 +119,8 @@ 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); itemTitleView.setText(Localization.concatenateStrings(item.getUploaderName(), - Localization.relativeTimeOrTextual(item.getUploadDate(), - item.getTextualUploadDate(), itemBuilder.getContext()))); + Localization.relativeTimeOrTextual(itemBuilder.getContext(), item.getUploadDate(), + item.getTextualUploadDate()))); // setup bottom row, with likes, heart and replies button 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 7f80707bc..80f62eed3 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 @@ -77,8 +77,9 @@ public class StreamInfoItemHolder extends StreamMiniInfoItemHolder { } } - final String uploadDate = Localization.relativeTimeOrTextual(infoItem.getUploadDate(), - infoItem.getTextualUploadDate(), itemBuilder.getContext()); + final String uploadDate = Localization.relativeTimeOrTextual(itemBuilder.getContext(), + infoItem.getUploadDate(), + infoItem.getTextualUploadDate()); if (!TextUtils.isEmpty(uploadDate)) { if (viewsAndDate.isEmpty()) { return uploadDate; 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 dea48651e..ef30e1761 100644 --- a/app/src/main/java/org/schabi/newpipe/util/Localization.java +++ b/app/src/main/java/org/schabi/newpipe/util/Localization.java @@ -217,6 +217,12 @@ public final class Localization { String.valueOf(replyCount)); } + /** + * @param context the Android context + * @param likeCount the like count, possibly negative if unknown + * @return if {@code likeCount} is smaller than {@code 0}, the string {@code "-"}, otherwise + * the result of calling {@link #shortCount(Context, long)} on the like count + */ public static String likeCount(final Context context, final int likeCount) { if (likeCount < 0) { return "-"; @@ -344,9 +350,20 @@ public final class Localization { return prettyTime.formatUnrounded(offsetDateTime); } - public static String relativeTimeOrTextual(final DateWrapper parsed, - final String textual, - @Nullable final Context context) { + /** + * @param context the Android context; if {@code null} then even if in debug mode and the + * setting is enabled, {@code textual} will not be shown next to {@code parsed} + * @param parsed the textual date or time ago parsed by NewPipeExtractor, or {@code null} if + * the extractor could not parse it + * @param textual the original textual date or time ago string as provided by services + * @return {@link #relativeTime(OffsetDateTime)} is used if {@code parsed != null}, otherwise + * {@code textual} is returned. If in debug mode, {@code context != null}, + * {@code parsed != null} and the relevant setting is enabled, {@code textual} will + * be appended to the returned string for debugging purposes. + */ + public static String relativeTimeOrTextual(@Nullable final Context context, + @Nullable final DateWrapper parsed, + final String textual) { if (parsed == null) { return textual; } else if (DEBUG && context != null && PreferenceManager 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 e127a8b55..e105e551b 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -482,6 +482,13 @@ public final class NavigationHelper { item.getServiceId(), uploaderUrl, item.getUploaderName()); } + /** + * Opens the comment author channel fragment, if the {@link CommentsInfoItem#getUploaderUrl()} + * of {@code comment} is non-null. Shows a UI-error snackbar if something goes wrong. + * + * @param activity the activity with the fragment manager and in which to show the snackbar + * @param comment the comment whose uploader/author will be opened + */ public static void openCommentAuthorIfPresent(@NonNull final FragmentActivity activity, final CommentsInfoItem comment) { if (isEmpty(comment.getUploaderUrl())) { 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 68905e6c9..c712157b3 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,11 @@ public final class ServiceHelper { .orElse(""); } + /** + * @param serviceId the id of the service + * @return the service corresponding to the provided id + * @throws java.util.NoSuchElementException if there is no service with the provided id + */ @NonNull public static StreamingService getServiceById(final int serviceId) { return ServiceList.all().stream()