Skip to content

Commit

Permalink
ServiceHelper: Directly return service from Info/InfoItem
Browse files Browse the repository at this point in the history
Instead of going through the Service ID, we provide some more specific
wrappers to discourage passing service IDs around.
  • Loading branch information
Profpatsch committed Jan 20, 2024
1 parent e352b4c commit a3355cd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,6 +22,7 @@
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;
Expand Down Expand Up @@ -105,7 +105,7 @@ protected Supplier<View> getListHeaderSupplier() {

// 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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;
Expand Down Expand Up @@ -52,6 +51,7 @@
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;
Expand Down Expand Up @@ -329,7 +329,7 @@ public void handleResult(@NonNull final PlaylistInfo result) {
if (description != null && description != Description.EMPTY_DESCRIPTION
&& !isBlank(description.getContent())) {
final TextEllipsizer ellipsizer = new TextEllipsizer(
headerBinding.playlistDescription, 5, getServiceById(result.getServiceId()));
headerBinding.playlistDescription, 5, ServiceHelper.getServiceFromInfo(result));
ellipsizer.setStateChangeListener(isEllipsized ->
headerBinding.playlistDescriptionReadMore.setText(
Boolean.TRUE.equals(isEllipsized) ? R.string.show_more : R.string.show_less
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,6 +20,7 @@
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;
Expand Down Expand Up @@ -122,7 +121,7 @@ public void updateFromItem(final InfoItem infoItem,


// 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();
Expand Down
51 changes: 40 additions & 11 deletions app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
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;
Expand Down Expand Up @@ -110,7 +112,6 @@ public static Optional<StreamingService> getSelectedService(final Context contex
}
}


/** Get the name of the selected service.
*
* @param context
Expand Down Expand Up @@ -138,17 +139,45 @@ public static String getNameOfServiceByIdOrUnknown(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
/** Return the service for the given InfoItem.
* <p>
* 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.
* <p>
* NB: this function should exist as member on {@link InfoItem}.
*
* @param infoItem
* @param <Item>
* @return The service.
*/
@NonNull
public static StreamingService getServiceById(final int serviceId) {
return ServiceList.all().stream()
.filter(s -> s.getServiceId() == serviceId)
.findFirst()
.orElseThrow();
public static <Item extends InfoItem> 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.
* <p>
* 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.
* <p>
* NB: this function should exist as member on {@link Info}.
*
* @param info
* @param <Item>
* @return The service.
*/
public static <Item extends Info> 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) {
Expand Down

0 comments on commit a3355cd

Please sign in to comment.