Skip to content

Commit 10c57b1

Browse files
authored
Merge pull request #10781 from Profpatsch/BaseDescriptionFragment-assert-member-is-initialized
BaseDescriptionFragment: Assert member is initialized
2 parents 3f94e7b + b85f7a6 commit 10c57b1

4 files changed

Lines changed: 26 additions & 62 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: 6 additions & 22 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,56 +24,39 @@
2324
public class DescriptionFragment extends BaseDescriptionFragment {
2425

2526
@State
26-
StreamInfo streamInfo = null;
27-
28-
public DescriptionFragment() {
29-
}
27+
StreamInfo streamInfo;
3028

3129
public DescriptionFragment(final StreamInfo streamInfo) {
3230
this.streamInfo = streamInfo;
3331
}
3432

33+
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: 8 additions & 24 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,10 @@ public class ChannelAboutFragment extends BaseDescriptionFragment {
2626
@State
2727
protected ChannelInfo channelInfo;
2828

29-
public static ChannelAboutFragment getInstance(final ChannelInfo channelInfo) {
30-
final ChannelAboutFragment fragment = new ChannelAboutFragment();
31-
fragment.channelInfo = channelInfo;
32-
return fragment;
29+
ChannelAboutFragment(@NonNull final ChannelInfo channelInfo) {
30+
this.channelInfo = channelInfo;
3331
}
3432

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

3934
@Override
4035
protected void initViews(final View rootView, final Bundle savedInstanceState) {
@@ -45,26 +40,17 @@ protected void initViews(final View rootView, final Bundle savedInstanceState) {
4540
@Nullable
4641
@Override
4742
protected Description getDescription() {
48-
if (channelInfo == null) {
49-
return null;
50-
}
5143
return new Description(channelInfo.getDescription(), Description.PLAIN_TEXT);
5244
}
5345

54-
@Nullable
46+
@NonNull
5547
@Override
5648
protected StreamingService getService() {
57-
if (channelInfo == null) {
58-
return null;
59-
}
6049
return channelInfo.getService();
6150
}
6251

6352
@Override
6453
protected int getServiceId() {
65-
if (channelInfo == null) {
66-
return -1;
67-
}
6854
return channelInfo.getServiceId();
6955
}
7056

@@ -74,12 +60,9 @@ protected String getStreamUrl() {
7460
return null;
7561
}
7662

77-
@Nullable
63+
@NonNull
7864
@Override
7965
public List<String> getTags() {
80-
if (channelInfo == null) {
81-
return null;
82-
}
8366
return channelInfo.getTags();
8467
}
8568

@@ -93,10 +76,11 @@ protected void setupMetadata(final LayoutInflater inflater,
9376
return;
9477
}
9578

96-
final Context context = getContext();
9779
if (channelInfo.getSubscriberCount() != UNKNOWN_SUBSCRIBER_COUNT) {
9880
addMetadataItem(inflater, layout, false, R.string.metadata_subscribers,
99-
Localization.localizeNumber(context, channelInfo.getSubscriberCount()));
81+
Localization.localizeNumber(
82+
requireContext(),
83+
channelInfo.getSubscriberCount()));
10084
}
10185

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ private void updateTabs() {
474474
if (ChannelTabHelper.showChannelTab(
475475
context, preferences, R.string.show_channel_tabs_about)) {
476476
tabAdapter.addFragment(
477-
ChannelAboutFragment.getInstance(currentInfo),
477+
new ChannelAboutFragment(currentInfo),
478478
context.getString(R.string.channel_tab_about));
479479
}
480480
}

0 commit comments

Comments
 (0)