Skip to content

Commit 32d2606

Browse files
committed
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`.
1 parent 1d8850d commit 32d2606

3 files changed

Lines changed: 27 additions & 58 deletions

File tree

app/src/main/java/org/schabi/newpipe/fragments/detail/BaseDescriptionFragment.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void onDestroy() {
6464

6565
/**
6666
* Get the description to display.
67-
* @return description object
67+
* @return description object, if available
6868
*/
6969
@Nullable
7070
protected abstract Description getDescription();
@@ -73,7 +73,7 @@ public void onDestroy() {
7373
* Get the streaming service. Used for generating description links.
7474
* @return streaming service
7575
*/
76-
@Nullable
76+
@NonNull
7777
protected abstract StreamingService getService();
7878

7979
/**
@@ -93,7 +93,7 @@ public void onDestroy() {
9393
* Get the list of tags to display below the description.
9494
* @return tag list
9595
*/
96-
@Nullable
96+
@NonNull
9797
public abstract List<String> getTags();
9898

9999
/**
@@ -158,7 +158,7 @@ protected void addMetadataItem(final LayoutInflater inflater,
158158
final LinearLayout layout,
159159
final boolean linkifyContent,
160160
@StringRes final int type,
161-
@Nullable final String content) {
161+
@NonNull final String content) {
162162
if (isBlank(content)) {
163163
return;
164164
}
@@ -221,16 +221,12 @@ protected void addImagesMetadataItem(final LayoutInflater inflater,
221221
urls.append(imageSizeToText(image.getWidth()));
222222
} else {
223223
switch (image.getEstimatedResolutionLevel()) {
224-
case LOW:
225-
urls.append(getString(R.string.image_quality_low));
226-
break;
227-
default: // unreachable, Image.ResolutionLevel.UNKNOWN is already filtered out
228-
case MEDIUM:
229-
urls.append(getString(R.string.image_quality_medium));
230-
break;
231-
case HIGH:
232-
urls.append(getString(R.string.image_quality_high));
233-
break;
224+
case LOW -> urls.append(getString(R.string.image_quality_low));
225+
case MEDIUM -> urls.append(getString(R.string.image_quality_medium));
226+
case HIGH -> urls.append(getString(R.string.image_quality_high));
227+
default -> {
228+
// unreachable, Image.ResolutionLevel.UNKNOWN is already filtered out
229+
}
234230
}
235231
}
236232

@@ -255,7 +251,7 @@ public void onClick(@NonNull final View widget) {
255251
private void addTagsMetadataItem(final LayoutInflater inflater, final LinearLayout layout) {
256252
final List<String> tags = getTags();
257253

258-
if (tags != null && !tags.isEmpty()) {
254+
if (!tags.isEmpty()) {
259255
final var itemBinding = ItemMetadataTagsBinding.inflate(inflater, layout, false);
260256

261257
tags.stream().sorted(String.CASE_INSENSITIVE_ORDER).forEach(tag -> {

app/src/main/java/org/schabi/newpipe/fragments/detail/DescriptionFragment.java

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.view.View;
88
import android.widget.LinearLayout;
99

10+
import androidx.annotation.NonNull;
1011
import androidx.annotation.Nullable;
1112
import androidx.annotation.StringRes;
1213

@@ -23,10 +24,8 @@
2324
public class DescriptionFragment extends BaseDescriptionFragment {
2425

2526
@State
26-
StreamInfo streamInfo = null;
27+
StreamInfo streamInfo;
2728

28-
public DescriptionFragment() {
29-
}
3029

3130
public DescriptionFragment(final StreamInfo streamInfo) {
3231
this.streamInfo = streamInfo;
@@ -35,44 +34,29 @@ public DescriptionFragment(final StreamInfo streamInfo) {
3534
@Nullable
3635
@Override
3736
protected Description getDescription() {
38-
if (streamInfo == null) {
39-
return null;
40-
}
4137
return streamInfo.getDescription();
4238
}
4339

44-
@Nullable
40+
@NonNull
4541
@Override
4642
protected StreamingService getService() {
47-
if (streamInfo == null) {
48-
return null;
49-
}
5043
return streamInfo.getService();
5144
}
5245

5346
@Override
5447
protected int getServiceId() {
55-
if (streamInfo == null) {
56-
return -1;
57-
}
5848
return streamInfo.getServiceId();
5949
}
6050

61-
@Nullable
51+
@NonNull
6252
@Override
6353
protected String getStreamUrl() {
64-
if (streamInfo == null) {
65-
return null;
66-
}
6754
return streamInfo.getUrl();
6855
}
6956

70-
@Nullable
57+
@NonNull
7158
@Override
7259
public List<String> getTags() {
73-
if (streamInfo == null) {
74-
return null;
75-
}
7660
return streamInfo.getTags();
7761
}
7862

app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelAboutFragment.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import static org.schabi.newpipe.extractor.stream.StreamExtractor.UNKNOWN_SUBSCRIBER_COUNT;
44

5-
import android.content.Context;
65
import android.os.Bundle;
76
import android.view.LayoutInflater;
87
import android.view.View;
98
import android.widget.LinearLayout;
109

10+
import androidx.annotation.NonNull;
1111
import androidx.annotation.Nullable;
1212

1313
import org.schabi.newpipe.R;
@@ -26,15 +26,12 @@ public class ChannelAboutFragment extends BaseDescriptionFragment {
2626
@State
2727
protected ChannelInfo channelInfo;
2828

29-
public static ChannelAboutFragment getInstance(final ChannelInfo channelInfo) {
29+
public static ChannelAboutFragment getInstance(final @NonNull ChannelInfo channelInfo) {
3030
final ChannelAboutFragment fragment = new ChannelAboutFragment();
3131
fragment.channelInfo = channelInfo;
3232
return fragment;
3333
}
3434

35-
public ChannelAboutFragment() {
36-
super();
37-
}
3835

3936
@Override
4037
protected void initViews(final View rootView, final Bundle savedInstanceState) {
@@ -45,26 +42,20 @@ protected void initViews(final View rootView, final Bundle savedInstanceState) {
4542
@Nullable
4643
@Override
4744
protected Description getDescription() {
48-
if (channelInfo == null) {
49-
return null;
50-
}
51-
return new Description(channelInfo.getDescription(), Description.PLAIN_TEXT);
45+
return new Description(
46+
channelInfo.getDescription(),
47+
Description.PLAIN_TEXT
48+
);
5249
}
5350

54-
@Nullable
51+
@NonNull
5552
@Override
5653
protected StreamingService getService() {
57-
if (channelInfo == null) {
58-
return null;
59-
}
6054
return channelInfo.getService();
6155
}
6256

6357
@Override
6458
protected int getServiceId() {
65-
if (channelInfo == null) {
66-
return -1;
67-
}
6859
return channelInfo.getServiceId();
6960
}
7061

@@ -74,12 +65,9 @@ protected String getStreamUrl() {
7465
return null;
7566
}
7667

77-
@Nullable
68+
@NonNull
7869
@Override
7970
public List<String> getTags() {
80-
if (channelInfo == null) {
81-
return null;
82-
}
8371
return channelInfo.getTags();
8472
}
8573

@@ -93,10 +81,11 @@ protected void setupMetadata(final LayoutInflater inflater,
9381
return;
9482
}
9583

96-
final Context context = getContext();
9784
if (channelInfo.getSubscriberCount() != UNKNOWN_SUBSCRIBER_COUNT) {
9885
addMetadataItem(inflater, layout, false, R.string.metadata_subscribers,
99-
Localization.localizeNumber(context, channelInfo.getSubscriberCount()));
86+
Localization.localizeNumber(
87+
requireContext(),
88+
channelInfo.getSubscriberCount()));
10089
}
10190

10291
addImagesMetadataItem(inflater, layout, R.string.metadata_avatars,

0 commit comments

Comments
 (0)