From 32d2606a6544961ecd8766ea445c729c301ed14f Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sat, 20 Jan 2024 22:26:24 +0100 Subject: [PATCH 1/2] BaseDescriptionFragment: Assert member is initialized `streamInfo` and `channelInfo` have to be initialized, since the only way to construct the class it to pass them. So we can remove the null check boilerplate and make some of the accessors `NonNull`. --- .../detail/BaseDescriptionFragment.java | 26 +++++++-------- .../fragments/detail/DescriptionFragment.java | 26 +++------------ .../list/channel/ChannelAboutFragment.java | 33 +++++++------------ 3 files changed, 27 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/BaseDescriptionFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/BaseDescriptionFragment.java index e3bdc6592..4789b02e6 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/BaseDescriptionFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/BaseDescriptionFragment.java @@ -64,7 +64,7 @@ public void onDestroy() { /** * Get the description to display. - * @return description object + * @return description object, if available */ @Nullable protected abstract Description getDescription(); @@ -73,7 +73,7 @@ public void onDestroy() { * Get the streaming service. Used for generating description links. * @return streaming service */ - @Nullable + @NonNull protected abstract StreamingService getService(); /** @@ -93,7 +93,7 @@ public void onDestroy() { * Get the list of tags to display below the description. * @return tag list */ - @Nullable + @NonNull public abstract List getTags(); /** @@ -158,7 +158,7 @@ protected void addMetadataItem(final LayoutInflater inflater, final LinearLayout layout, final boolean linkifyContent, @StringRes final int type, - @Nullable final String content) { + @NonNull final String content) { if (isBlank(content)) { return; } @@ -221,16 +221,12 @@ protected void addImagesMetadataItem(final LayoutInflater inflater, urls.append(imageSizeToText(image.getWidth())); } else { switch (image.getEstimatedResolutionLevel()) { - case LOW: - urls.append(getString(R.string.image_quality_low)); - break; - default: // unreachable, Image.ResolutionLevel.UNKNOWN is already filtered out - case MEDIUM: - urls.append(getString(R.string.image_quality_medium)); - break; - case HIGH: - urls.append(getString(R.string.image_quality_high)); - break; + case LOW -> urls.append(getString(R.string.image_quality_low)); + case MEDIUM -> urls.append(getString(R.string.image_quality_medium)); + case HIGH -> urls.append(getString(R.string.image_quality_high)); + default -> { + // unreachable, Image.ResolutionLevel.UNKNOWN is already filtered out + } } } @@ -255,7 +251,7 @@ public void onClick(@NonNull final View widget) { private void addTagsMetadataItem(final LayoutInflater inflater, final LinearLayout layout) { final List tags = getTags(); - if (tags != null && !tags.isEmpty()) { + if (!tags.isEmpty()) { final var itemBinding = ItemMetadataTagsBinding.inflate(inflater, layout, false); tags.stream().sorted(String.CASE_INSENSITIVE_ORDER).forEach(tag -> { diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/DescriptionFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/DescriptionFragment.java index ada12fc8e..ba233559c 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/DescriptionFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/DescriptionFragment.java @@ -7,6 +7,7 @@ import android.view.View; import android.widget.LinearLayout; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.StringRes; @@ -23,10 +24,8 @@ public class DescriptionFragment extends BaseDescriptionFragment { @State - StreamInfo streamInfo = null; + StreamInfo streamInfo; - public DescriptionFragment() { - } public DescriptionFragment(final StreamInfo streamInfo) { this.streamInfo = streamInfo; @@ -35,44 +34,29 @@ public DescriptionFragment(final StreamInfo streamInfo) { @Nullable @Override protected Description getDescription() { - if (streamInfo == null) { - return null; - } return streamInfo.getDescription(); } - @Nullable + @NonNull @Override protected StreamingService getService() { - if (streamInfo == null) { - return null; - } return streamInfo.getService(); } @Override protected int getServiceId() { - if (streamInfo == null) { - return -1; - } return streamInfo.getServiceId(); } - @Nullable + @NonNull @Override protected String getStreamUrl() { - if (streamInfo == null) { - return null; - } return streamInfo.getUrl(); } - @Nullable + @NonNull @Override public List getTags() { - if (streamInfo == null) { - return null; - } return streamInfo.getTags(); } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelAboutFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelAboutFragment.java index 56418800d..30c3aac5a 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelAboutFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelAboutFragment.java @@ -2,12 +2,12 @@ import static org.schabi.newpipe.extractor.stream.StreamExtractor.UNKNOWN_SUBSCRIBER_COUNT; -import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.LinearLayout; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import org.schabi.newpipe.R; @@ -26,15 +26,12 @@ public class ChannelAboutFragment extends BaseDescriptionFragment { @State protected ChannelInfo channelInfo; - public static ChannelAboutFragment getInstance(final ChannelInfo channelInfo) { + public static ChannelAboutFragment getInstance(final @NonNull ChannelInfo channelInfo) { final ChannelAboutFragment fragment = new ChannelAboutFragment(); fragment.channelInfo = channelInfo; return fragment; } - public ChannelAboutFragment() { - super(); - } @Override protected void initViews(final View rootView, final Bundle savedInstanceState) { @@ -45,26 +42,20 @@ protected void initViews(final View rootView, final Bundle savedInstanceState) { @Nullable @Override protected Description getDescription() { - if (channelInfo == null) { - return null; - } - return new Description(channelInfo.getDescription(), Description.PLAIN_TEXT); + return new Description( + channelInfo.getDescription(), + Description.PLAIN_TEXT + ); } - @Nullable + @NonNull @Override protected StreamingService getService() { - if (channelInfo == null) { - return null; - } return channelInfo.getService(); } @Override protected int getServiceId() { - if (channelInfo == null) { - return -1; - } return channelInfo.getServiceId(); } @@ -74,12 +65,9 @@ protected String getStreamUrl() { return null; } - @Nullable + @NonNull @Override public List getTags() { - if (channelInfo == null) { - return null; - } return channelInfo.getTags(); } @@ -93,10 +81,11 @@ protected void setupMetadata(final LayoutInflater inflater, return; } - final Context context = getContext(); if (channelInfo.getSubscriberCount() != UNKNOWN_SUBSCRIBER_COUNT) { addMetadataItem(inflater, layout, false, R.string.metadata_subscribers, - Localization.localizeNumber(context, channelInfo.getSubscriberCount())); + Localization.localizeNumber( + requireContext(), + channelInfo.getSubscriberCount())); } addImagesMetadataItem(inflater, layout, R.string.metadata_avatars, From b85f7a6747eb61ea4b7b7893688ea65f7103de01 Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 28 Mar 2024 22:46:19 +0100 Subject: [PATCH 2/2] Some more slight improvements --- .../newpipe/fragments/detail/DescriptionFragment.java | 2 +- .../fragments/list/channel/ChannelAboutFragment.java | 11 +++-------- .../fragments/list/channel/ChannelFragment.java | 2 +- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/DescriptionFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/DescriptionFragment.java index ba233559c..7fc66b42f 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/DescriptionFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/DescriptionFragment.java @@ -26,11 +26,11 @@ public class DescriptionFragment extends BaseDescriptionFragment { @State StreamInfo streamInfo; - public DescriptionFragment(final StreamInfo streamInfo) { this.streamInfo = streamInfo; } + @Nullable @Override protected Description getDescription() { diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelAboutFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelAboutFragment.java index 30c3aac5a..674a13c5b 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelAboutFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelAboutFragment.java @@ -26,10 +26,8 @@ public class ChannelAboutFragment extends BaseDescriptionFragment { @State protected ChannelInfo channelInfo; - public static ChannelAboutFragment getInstance(final @NonNull ChannelInfo channelInfo) { - final ChannelAboutFragment fragment = new ChannelAboutFragment(); - fragment.channelInfo = channelInfo; - return fragment; + ChannelAboutFragment(@NonNull final ChannelInfo channelInfo) { + this.channelInfo = channelInfo; } @@ -42,10 +40,7 @@ protected void initViews(final View rootView, final Bundle savedInstanceState) { @Nullable @Override protected Description getDescription() { - return new Description( - channelInfo.getDescription(), - Description.PLAIN_TEXT - ); + return new Description(channelInfo.getDescription(), Description.PLAIN_TEXT); } @NonNull diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java index b16f40a4a..7e83d9958 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java @@ -474,7 +474,7 @@ private void updateTabs() { if (ChannelTabHelper.showChannelTab( context, preferences, R.string.show_channel_tabs_about)) { tabAdapter.addFragment( - ChannelAboutFragment.getInstance(currentInfo), + new ChannelAboutFragment(currentInfo), context.getString(R.string.channel_tab_about)); } }