Add some documentation and javadocs
Also further simplify CommentRepliesInfo and RelatedItemsInfo
This commit is contained in:
parent
f41ab8b086
commit
3f37e27852
10 changed files with 83 additions and 62 deletions
|
@ -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<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())));
|
||||
}
|
||||
|
|
|
@ -7,19 +7,16 @@ import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
|
|||
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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -27,11 +27,11 @@ import java.util.function.Supplier;
|
|||
|
||||
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
|
||||
|
@ -68,7 +68,7 @@ public class RelatedItemsFragment extends BaseListInfoFragment<InfoItem, Related
|
|||
|
||||
@Override
|
||||
protected Supplier<View> getListHeaderSupplier() {
|
||||
if (relatedItemInfo == null || relatedItemInfo.getRelatedItems() == null) {
|
||||
if (relatedItemsInfo == null || relatedItemsInfo.getRelatedItems() == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -96,8 +96,8 @@ public class RelatedItemsFragment extends BaseListInfoFragment<InfoItem, Related
|
|||
//////////////////////////////////////////////////////////////////////////*/
|
||||
|
||||
@Override
|
||||
protected Single<RelatedItemInfo> loadResult(final boolean forceLoad) {
|
||||
return Single.fromCallable(() -> relatedItemInfo);
|
||||
protected Single<RelatedItemsInfo> loadResult(final boolean forceLoad) {
|
||||
return Single.fromCallable(() -> relatedItemsInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -109,7 +109,7 @@ public class RelatedItemsFragment extends BaseListInfoFragment<InfoItem, Related
|
|||
}
|
||||
|
||||
@Override
|
||||
public void handleResult(@NonNull final RelatedItemInfo result) {
|
||||
public void handleResult(@NonNull final RelatedItemsInfo result) {
|
||||
super.handleResult(result);
|
||||
|
||||
if (headerBinding != null) {
|
||||
|
@ -136,23 +136,23 @@ public class RelatedItemsFragment extends BaseListInfoFragment<InfoItem, Related
|
|||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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()));
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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())) {
|
||||
|
|
|
@ -144,6 +144,11 @@ public final class ServiceHelper {
|
|||
.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()
|
||||
|
|
Loading…
Reference in a new issue