Add some documentation and javadocs

Also further simplify CommentRepliesInfo and RelatedItemsInfo
This commit is contained in:
Stypox 2023-04-12 15:18:26 +02:00
parent f41ab8b086
commit 3f37e27852
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
10 changed files with 83 additions and 62 deletions

View File

@ -89,7 +89,7 @@ public final class CommentRepliesFragment
// setup author name and comment date // setup author name and comment date
binding.authorName.setText(item.getUploaderName()); binding.authorName.setText(item.getUploaderName());
binding.uploadDate.setText(Localization.relativeTimeOrTextual( binding.uploadDate.setText(Localization.relativeTimeOrTextual(
item.getUploadDate(), item.getTextualUploadDate(), getContext())); getContext(), item.getUploadDate(), item.getTextualUploadDate()));
binding.authorTouchArea.setOnClickListener( binding.authorTouchArea.setOnClickListener(
v -> NavigationHelper.openCommentAuthorIfPresent(requireActivity(), item)); v -> NavigationHelper.openCommentAuthorIfPresent(requireActivity(), item));
@ -140,7 +140,7 @@ public final class CommentRepliesFragment
@Override @Override
protected Single<CommentRepliesInfo> loadResult(final boolean forceLoad) { protected Single<CommentRepliesInfo> 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 // the reply count string will be shown as the activity title
Localization.replyCount(requireContext(), commentsInfoItem.getReplyCount()))); Localization.replyCount(requireContext(), commentsInfoItem.getReplyCount())));
} }

View File

@ -7,19 +7,16 @@ import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import java.util.Collections; import java.util.Collections;
public final class CommentRepliesInfo extends ListInfo<CommentsInfoItem> { public final class CommentRepliesInfo extends ListInfo<CommentsInfoItem> {
private CommentRepliesInfo(final int serviceId, /**
final ListLinkHandler listUrlIdHandler, * This class is used to wrap the comment replies page into a ListInfo object.
final String name) { *
super(serviceId, listUrlIdHandler, name); * @param comment the comment from which to get replies
} * @param name will be shown as the fragment title
*/
public static CommentRepliesInfo getInfo(final CommentsInfoItem comment, final String name) { public CommentRepliesInfo(final CommentsInfoItem comment, final String name) {
final ListLinkHandler handler = super(comment.getServiceId(),
new ListLinkHandler("", "", "", Collections.emptyList(), null); new ListLinkHandler("", "", "", Collections.emptyList(), null), name);
final CommentRepliesInfo relatedItemInfo = new CommentRepliesInfo( setNextPage(comment.getReplies());
comment.getServiceId(), handler, name); // the name will be shown as fragment title setRelatedItems(Collections.emptyList()); // since it must be non-null
relatedItemInfo.setNextPage(comment.getReplies());
relatedItemInfo.setRelatedItems(Collections.emptyList()); // since it must be non-null
return relatedItemInfo;
} }
} }

View File

@ -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<InfoItem> {
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<InfoItem> relatedItems = new ArrayList<>(info.getRelatedItems());
relatedItemInfo.setRelatedItems(relatedItems);
return relatedItemInfo;
}
}

View File

@ -27,11 +27,11 @@ import java.util.function.Supplier;
import io.reactivex.rxjava3.core.Single; import io.reactivex.rxjava3.core.Single;
public class RelatedItemsFragment extends BaseListInfoFragment<InfoItem, RelatedItemInfo> public class RelatedItemsFragment extends BaseListInfoFragment<InfoItem, RelatedItemsInfo>
implements SharedPreferences.OnSharedPreferenceChangeListener { implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String INFO_KEY = "related_info_key"; private static final String INFO_KEY = "related_info_key";
private RelatedItemInfo relatedItemInfo; private RelatedItemsInfo relatedItemsInfo;
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Views // Views
@ -68,7 +68,7 @@ public class RelatedItemsFragment extends BaseListInfoFragment<InfoItem, Related
@Override @Override
protected Supplier<View> getListHeaderSupplier() { protected Supplier<View> getListHeaderSupplier() {
if (relatedItemInfo == null || relatedItemInfo.getRelatedItems() == null) { if (relatedItemsInfo == null || relatedItemsInfo.getRelatedItems() == null) {
return null; return null;
} }
@ -96,8 +96,8 @@ public class RelatedItemsFragment extends BaseListInfoFragment<InfoItem, Related
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
@Override @Override
protected Single<RelatedItemInfo> loadResult(final boolean forceLoad) { protected Single<RelatedItemsInfo> loadResult(final boolean forceLoad) {
return Single.fromCallable(() -> relatedItemInfo); return Single.fromCallable(() -> relatedItemsInfo);
} }
@Override @Override
@ -109,7 +109,7 @@ public class RelatedItemsFragment extends BaseListInfoFragment<InfoItem, Related
} }
@Override @Override
public void handleResult(@NonNull final RelatedItemInfo result) { public void handleResult(@NonNull final RelatedItemsInfo result) {
super.handleResult(result); super.handleResult(result);
if (headerBinding != null) { if (headerBinding != null) {
@ -136,23 +136,23 @@ public class RelatedItemsFragment extends BaseListInfoFragment<InfoItem, Related
private void setInitialData(final StreamInfo info) { private void setInitialData(final StreamInfo info) {
super.setInitialData(info.getServiceId(), info.getUrl(), info.getName()); super.setInitialData(info.getServiceId(), info.getUrl(), info.getName());
if (this.relatedItemInfo == null) { if (this.relatedItemsInfo == null) {
this.relatedItemInfo = RelatedItemInfo.getInfo(info); this.relatedItemsInfo = new RelatedItemsInfo(info);
} }
} }
@Override @Override
public void onSaveInstanceState(@NonNull final Bundle outState) { public void onSaveInstanceState(@NonNull final Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putSerializable(INFO_KEY, relatedItemInfo); outState.putSerializable(INFO_KEY, relatedItemsInfo);
} }
@Override @Override
protected void onRestoreInstanceState(@NonNull final Bundle savedState) { protected void onRestoreInstanceState(@NonNull final Bundle savedState) {
super.onRestoreInstanceState(savedState); super.onRestoreInstanceState(savedState);
final Serializable serializable = savedState.getSerializable(INFO_KEY); final Serializable serializable = savedState.getSerializable(INFO_KEY);
if (serializable instanceof RelatedItemInfo) { if (serializable instanceof RelatedItemsInfo) {
this.relatedItemInfo = (RelatedItemInfo) serializable; this.relatedItemsInfo = (RelatedItemsInfo) serializable;
} }
} }

View File

@ -0,0 +1,22 @@
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;
public final class RelatedItemsInfo extends ListInfo<InfoItem> {
/**
* 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()));
}
}

View File

@ -119,8 +119,8 @@ public class CommentInfoItemHolder extends InfoItemHolder {
// setup the top row, with pinned icon, author name and comment date // setup the top row, with pinned icon, author name and comment date
itemPinnedView.setVisibility(item.isPinned() ? View.VISIBLE : View.GONE); itemPinnedView.setVisibility(item.isPinned() ? View.VISIBLE : View.GONE);
itemTitleView.setText(Localization.concatenateStrings(item.getUploaderName(), itemTitleView.setText(Localization.concatenateStrings(item.getUploaderName(),
Localization.relativeTimeOrTextual(item.getUploadDate(), Localization.relativeTimeOrTextual(itemBuilder.getContext(), item.getUploadDate(),
item.getTextualUploadDate(), itemBuilder.getContext()))); item.getTextualUploadDate())));
// setup bottom row, with likes, heart and replies button // setup bottom row, with likes, heart and replies button

View File

@ -77,8 +77,9 @@ public class StreamInfoItemHolder extends StreamMiniInfoItemHolder {
} }
} }
final String uploadDate = Localization.relativeTimeOrTextual(infoItem.getUploadDate(), final String uploadDate = Localization.relativeTimeOrTextual(itemBuilder.getContext(),
infoItem.getTextualUploadDate(), itemBuilder.getContext()); infoItem.getUploadDate(),
infoItem.getTextualUploadDate());
if (!TextUtils.isEmpty(uploadDate)) { if (!TextUtils.isEmpty(uploadDate)) {
if (viewsAndDate.isEmpty()) { if (viewsAndDate.isEmpty()) {
return uploadDate; return uploadDate;

View File

@ -217,6 +217,12 @@ public final class Localization {
String.valueOf(replyCount)); 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) { public static String likeCount(final Context context, final int likeCount) {
if (likeCount < 0) { if (likeCount < 0) {
return "-"; return "-";
@ -344,9 +350,20 @@ public final class Localization {
return prettyTime.formatUnrounded(offsetDateTime); return prettyTime.formatUnrounded(offsetDateTime);
} }
public static String relativeTimeOrTextual(final DateWrapper parsed, /**
final String textual, * @param context the Android context; if {@code null} then even if in debug mode and the
@Nullable final Context context) { * 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) { if (parsed == null) {
return textual; return textual;
} else if (DEBUG && context != null && PreferenceManager } else if (DEBUG && context != null && PreferenceManager

View File

@ -482,6 +482,13 @@ public final class NavigationHelper {
item.getServiceId(), uploaderUrl, item.getUploaderName()); 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, public static void openCommentAuthorIfPresent(@NonNull final FragmentActivity activity,
final CommentsInfoItem comment) { final CommentsInfoItem comment) {
if (isEmpty(comment.getUploaderUrl())) { if (isEmpty(comment.getUploaderUrl())) {

View File

@ -144,6 +144,11 @@ public final class ServiceHelper {
.orElse("<unknown>"); .orElse("<unknown>");
} }
/**
* @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 @NonNull
public static StreamingService getServiceById(final int serviceId) { public static StreamingService getServiceById(final int serviceId) {
return ServiceList.all().stream() return ServiceList.all().stream()