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.
This commit is contained in:
parent
e352b4ce03
commit
a3355cdf2c
|
@ -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();
|
||||
|
|
|
@ -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<StreamInfoItem, Playl
|
|||
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
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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("<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) {
|
||||
|
|
Loading…
Reference in New Issue