Skip to content

Commit

Permalink
Add some documentation and javadocs
Browse files Browse the repository at this point in the history
Also further simplify CommentRepliesInfo and RelatedItemsInfo
  • Loading branch information
Stypox committed Apr 12, 2023
1 parent 2fdb259 commit a3f6ae0
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected Supplier<View> getListHeaderSupplier() {
// 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));

Expand Down Expand Up @@ -139,7 +139,7 @@ public void readFrom(@NonNull final Queue<Object> savedObjects) throws Exception

@Override
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
Localization.replyCount(requireContext(), commentsInfoItem.getReplyCount())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@
import java.util.Collections;

public final class CommentRepliesInfo extends ListInfo<CommentsInfoItem> {
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
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@

import io.reactivex.rxjava3.core.Single;

public class RelatedItemsFragment extends BaseListInfoFragment<InfoItem, RelatedItemInfo>
public class RelatedItemsFragment extends BaseListInfoFragment<InfoItem, RelatedItemsInfo>
implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String INFO_KEY = "related_info_key";

private RelatedItemInfo relatedItemInfo;
private RelatedItemsInfo relatedItemsInfo;

/*//////////////////////////////////////////////////////////////////////////
// Views
Expand Down Expand Up @@ -68,7 +68,7 @@ public void onDestroyView() {

@Override
protected Supplier<View> getListHeaderSupplier() {
if (relatedItemInfo == null || relatedItemInfo.getRelatedItems() == null) {
if (relatedItemsInfo == null || relatedItemsInfo.getRelatedItems() == null) {
return null;
}

Expand Down Expand Up @@ -96,8 +96,8 @@ protected Single<ListExtractor.InfoItemsPage<InfoItem>> loadMoreItemsLogic() {
//////////////////////////////////////////////////////////////////////////*/

@Override
protected Single<RelatedItemInfo> loadResult(final boolean forceLoad) {
return Single.fromCallable(() -> relatedItemInfo);
protected Single<RelatedItemsInfo> loadResult(final boolean forceLoad) {
return Single.fromCallable(() -> relatedItemsInfo);
}

@Override
Expand All @@ -109,7 +109,7 @@ public void showLoading() {
}

@Override
public void handleResult(@NonNull final RelatedItemInfo result) {
public void handleResult(@NonNull final RelatedItemsInfo result) {
super.handleResult(result);

if (headerBinding != null) {
Expand All @@ -136,23 +136,23 @@ public void onCreateOptionsMenu(@NonNull final Menu menu,

private void setInitialData(final StreamInfo info) {
super.setInitialData(info.getServiceId(), info.getUrl(), info.getName());
if (this.relatedItemInfo == null) {
this.relatedItemInfo = RelatedItemInfo.getInfo(info);
if (this.relatedItemsInfo == null) {
this.relatedItemsInfo = new RelatedItemsInfo(info);
}
}

@Override
public void onSaveInstanceState(@NonNull final Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(INFO_KEY, relatedItemInfo);
outState.putSerializable(INFO_KEY, relatedItemsInfo);
}

@Override
protected void onRestoreInstanceState(@NonNull final Bundle savedState) {
super.onRestoreInstanceState(savedState);
final Serializable serializable = savedState.getSerializable(INFO_KEY);
if (serializable instanceof RelatedItemInfo) {
this.relatedItemInfo = (RelatedItemInfo) serializable;
if (serializable instanceof RelatedItemsInfo) {
this.relatedItemsInfo = (RelatedItemsInfo) serializable;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ public void updateFromItem(final InfoItem infoItem,
// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ private String getStreamInfoDetailLine(final StreamInfoItem infoItem) {
}
}

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;
Expand Down
23 changes: 20 additions & 3 deletions app/src/main/java/org/schabi/newpipe/util/Localization.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ public static String replyCount(final Context context, final int 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) {
if (likeCount < 0) {
return "-";
Expand Down Expand Up @@ -296,9 +302,20 @@ public static String relativeTime(final OffsetDateTime offsetDateTime) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,13 @@ public static void openChannelFragment(@NonNull final Fragment fragment,
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())) {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ public static String getNameOfServiceById(final int serviceId) {
.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
public static StreamingService getServiceById(final int serviceId) {
return ServiceList.all().stream()
Expand Down

0 comments on commit a3f6ae0

Please sign in to comment.