From a3355cdf2c3a034d8ca05d9da000a741731df1aa Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sat, 20 Jan 2024 19:24:13 +0100 Subject: [PATCH] ServiceHelper: Directly return service from Info/InfoItem Instead of going through the Service ID, we provide some more specific wrappers to discourage passing service IDs around. --- .../list/comments/CommentRepliesFragment.java | 4 +- .../list/playlist/PlaylistFragment.java | 4 +- .../holder/CommentInfoItemHolder.java | 5 +- .../schabi/newpipe/util/ServiceHelper.java | 51 +++++++++++++++---- 4 files changed, 46 insertions(+), 18 deletions(-) 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 a816b149f..7622e1950 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 @@ -1,6 +1,5 @@ package org.schabi.newpipe.fragments.list.comments; -import static org.schabi.newpipe.util.ServiceHelper.getServiceById; import android.os.Bundle; import android.view.LayoutInflater; @@ -23,6 +22,7 @@ import org.schabi.newpipe.util.DeviceUtils; import org.schabi.newpipe.util.ExtractorHelper; import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.util.NavigationHelper; +import org.schabi.newpipe.util.ServiceHelper; import org.schabi.newpipe.util.image.ImageStrategy; import org.schabi.newpipe.util.image.PicassoHelper; import org.schabi.newpipe.util.text.TextLinkifier; @@ -105,7 +105,7 @@ public final class CommentRepliesFragment // setup comment content TextLinkifier.fromDescription(binding.commentContent, item.getCommentText(), - HtmlCompat.FROM_HTML_MODE_LEGACY, getServiceById(item.getServiceId()), + HtmlCompat.FROM_HTML_MODE_LEGACY, ServiceHelper.getServiceFromInfoItem(item), item.getUrl(), disposables, null); return binding.getRoot(); diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/playlist/PlaylistFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/playlist/PlaylistFragment.java index ab3963d61..167f25e27 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/playlist/PlaylistFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/playlist/PlaylistFragment.java @@ -3,7 +3,6 @@ package org.schabi.newpipe.fragments.list.playlist; import static org.schabi.newpipe.extractor.utils.Utils.isBlank; import static org.schabi.newpipe.ktx.ViewUtils.animate; import static org.schabi.newpipe.ktx.ViewUtils.animateHideRecyclerViewAllowingScrolling; -import static org.schabi.newpipe.util.ServiceHelper.getServiceById; import android.content.Context; import android.os.Bundle; @@ -52,6 +51,7 @@ import org.schabi.newpipe.util.ExtractorHelper; import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.PlayButtonHelper; +import org.schabi.newpipe.util.ServiceHelper; import org.schabi.newpipe.util.external_communication.ShareUtils; import org.schabi.newpipe.util.image.PicassoHelper; import org.schabi.newpipe.util.text.TextEllipsizer; @@ -329,7 +329,7 @@ public class PlaylistFragment extends BaseListInfoFragment headerBinding.playlistDescriptionReadMore.setText( Boolean.TRUE.equals(isEllipsized) ? R.string.show_more : R.string.show_less 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 a3f0384ad..47720517b 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,7 +1,5 @@ package org.schabi.newpipe.info_list.holder; -import static org.schabi.newpipe.util.ServiceHelper.getServiceById; - import android.text.method.LinkMovementMethod; import android.text.style.URLSpan; import android.view.View; @@ -22,6 +20,7 @@ import org.schabi.newpipe.local.history.HistoryRecordManager; import org.schabi.newpipe.util.DeviceUtils; import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.util.NavigationHelper; +import org.schabi.newpipe.util.ServiceHelper; import org.schabi.newpipe.util.external_communication.ShareUtils; import org.schabi.newpipe.util.image.ImageStrategy; import org.schabi.newpipe.util.image.PicassoHelper; @@ -122,7 +121,7 @@ public class CommentInfoItemHolder extends InfoItemHolder { // setup comment content and click listeners to expand/ellipsize it - textEllipsizer.setStreamingService(getServiceById(item.getServiceId())); + textEllipsizer.setStreamingService(ServiceHelper.getServiceFromInfoItem(item)); textEllipsizer.setStreamUrl(item.getUrl()); textEllipsizer.setContent(item.getCommentText()); textEllipsizer.ellipsize(); 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 06ec9eae8..535eafe91 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java @@ -15,6 +15,8 @@ import com.grack.nanojson.JsonParser; import com.grack.nanojson.JsonParserException; import org.schabi.newpipe.R; +import org.schabi.newpipe.extractor.Info; +import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.ServiceList; import org.schabi.newpipe.extractor.StreamingService; @@ -110,7 +112,6 @@ public final class ServiceHelper { } } - /** Get the name of the selected service. * * @param context @@ -138,17 +139,45 @@ 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 + /** Return the service for the given InfoItem. + *

+ * This should always succeed, except in the (very unlikely) case + * that we remove a service from NewPipeExtractor and the `InfoItem` is deserialized + * from an old version where the service still existed. + *

+ * NB: this function should exist as member on {@link InfoItem}. + * + * @param infoItem + * @param + * @return The service. */ - @NonNull - public static StreamingService getServiceById(final int serviceId) { - return ServiceList.all().stream() - .filter(s -> s.getServiceId() == serviceId) - .findFirst() - .orElseThrow(); + public static StreamingService getServiceFromInfoItem( + final Item infoItem) { + try { + return NewPipe.getService(infoItem.getServiceId()); + } catch (final ExtractionException e) { + throw new AssertionError("InfoItem should never have a bad service id"); + } + } + + /** Return the service for the given Info. + *

+ * This should always succeed, except in the (very unlikely) case + * that we remove a service from NewPipeExtractor and the `Info` is deserialized + * from an old version where the service still existed. + *

+ * NB: this function should exist as member on {@link Info}. + * + * @param info + * @param + * @return The service. + */ + public static StreamingService getServiceFromInfo(final Item info) { + try { + return NewPipe.getService(info.getServiceId()); + } catch (final ExtractionException e) { + throw new AssertionError("InfoItem should never have a bad service id"); + } } public static void setSelectedServiceId(final Context context, final int serviceId) {