Skip to content

Commit b2d89a4

Browse files
authored
Merge pull request #12604 from Isira-Seneviratne/Refactor-EmptyState
2 parents 2ee7cc4 + 01a8c4e commit b2d89a4

9 files changed

Lines changed: 77 additions & 132 deletions

File tree

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.schabi.newpipe.ktx.TextViewUtils.animateTextColor;
44
import static org.schabi.newpipe.ktx.ViewUtils.animate;
55
import static org.schabi.newpipe.ktx.ViewUtils.animateBackgroundColor;
6+
import static org.schabi.newpipe.ui.emptystate.EmptyStateUtil.setEmptyStateComposable;
67

78
import android.content.Context;
89
import android.content.SharedPreferences;
@@ -45,7 +46,6 @@
4546
import org.schabi.newpipe.local.feed.notifications.NotificationHelper;
4647
import org.schabi.newpipe.local.subscription.SubscriptionManager;
4748
import org.schabi.newpipe.ui.emptystate.EmptyStateSpec;
48-
import org.schabi.newpipe.ui.emptystate.EmptyStateUtil;
4949
import org.schabi.newpipe.util.ChannelTabHelper;
5050
import org.schabi.newpipe.util.Constants;
5151
import org.schabi.newpipe.util.ExtractorHelper;
@@ -200,10 +200,7 @@ public boolean onMenuItemSelected(@NonNull final MenuItem item) {
200200
protected void initViews(final View rootView, final Bundle savedInstanceState) {
201201
super.initViews(rootView, savedInstanceState);
202202

203-
EmptyStateUtil.setEmptyStateComposable(
204-
binding.emptyStateView,
205-
EmptyStateSpec.Companion.getContentNotSupported()
206-
);
203+
setEmptyStateComposable(binding.emptyStateView, EmptyStateSpec.ContentNotSupported);
207204

208205
tabAdapter = new TabAdapter(getChildFragmentManager());
209206
binding.viewPager.setAdapter(tabAdapter);

app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static androidx.recyclerview.widget.ItemTouchHelper.Callback.makeMovementFlags;
44
import static org.schabi.newpipe.extractor.utils.Utils.isBlank;
55
import static org.schabi.newpipe.ktx.ViewUtils.animate;
6+
import static org.schabi.newpipe.ui.emptystate.EmptyStateUtil.setEmptyStateComposable;
67
import static org.schabi.newpipe.util.ExtractorHelper.showMetaInfoInTextView;
78
import static java.util.Arrays.asList;
89

@@ -66,7 +67,6 @@
6667
import org.schabi.newpipe.local.history.HistoryRecordManager;
6768
import org.schabi.newpipe.settings.NewPipeSettings;
6869
import org.schabi.newpipe.ui.emptystate.EmptyStateSpec;
69-
import org.schabi.newpipe.ui.emptystate.EmptyStateUtil;
7070
import org.schabi.newpipe.util.Constants;
7171
import org.schabi.newpipe.util.DeviceUtils;
7272
import org.schabi.newpipe.util.ExtractorHelper;
@@ -357,9 +357,7 @@ public void onActivityResult(final int requestCode, final int resultCode, final
357357
protected void initViews(final View rootView, final Bundle savedInstanceState) {
358358
super.initViews(rootView, savedInstanceState);
359359

360-
EmptyStateUtil.setEmptyStateComposable(
361-
searchBinding.emptyStateView,
362-
EmptyStateSpec.Companion.getNoSearchResult());
360+
setEmptyStateComposable(searchBinding.emptyStateView, EmptyStateSpec.NoSearchResult);
363361

364362
searchBinding.suggestionsList.setAdapter(suggestionListAdapter);
365363
// animations are just strange and useless, since the suggestions keep changing too much

app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import androidx.annotation.NonNull;
1616
import androidx.annotation.Nullable;
1717
import androidx.appcompat.app.AlertDialog;
18+
import androidx.compose.ui.platform.ComposeView;
1819
import androidx.fragment.app.FragmentManager;
1920
import androidx.recyclerview.widget.ItemTouchHelper;
2021
import androidx.recyclerview.widget.RecyclerView;
@@ -125,10 +126,8 @@ protected void initViews(final View rootView, final Bundle savedInstanceState) {
125126
super.initViews(rootView, savedInstanceState);
126127

127128
itemListAdapter.setUseItemHandle(true);
128-
EmptyStateUtil.setEmptyStateComposable(
129-
rootView.findViewById(R.id.empty_state_view),
130-
EmptyStateSpec.Companion.getNoBookmarkedPlaylist()
131-
);
129+
final ComposeView emptyView = rootView.findViewById(R.id.empty_state_view);
130+
EmptyStateUtil.setEmptyStateComposable(emptyView, EmptyStateSpec.NoBookmarkedPlaylist);
132131
}
133132

134133
@Override

app/src/main/java/org/schabi/newpipe/settings/SelectChannelFragment.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup
9595
progressBar = v.findViewById(R.id.progressBar);
9696
emptyView = v.findViewById(R.id.empty_state_view);
9797

98-
EmptyStateUtil.setEmptyStateComposable(emptyView,
99-
EmptyStateSpec.Companion.getNoSubscriptions());
98+
EmptyStateUtil.setEmptyStateComposable(emptyView, EmptyStateSpec.NoSubscriptions);
10099
progressBar.setVisibility(View.VISIBLE);
101100
recyclerView.setVisibility(View.GONE);
102101
emptyView.setVisibility(View.GONE);

app/src/main/java/org/schabi/newpipe/settings/SelectPlaylistFragment.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup
6565
recyclerView = v.findViewById(R.id.items_list);
6666
emptyView = v.findViewById(R.id.empty_state_view);
6767

68-
EmptyStateUtil.setEmptyStateComposable(emptyView,
69-
EmptyStateSpec.Companion.getNoBookmarkedPlaylist());
68+
EmptyStateUtil.setEmptyStateComposable(emptyView, EmptyStateSpec.NoBookmarkedPlaylist);
7069
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
7170
final SelectPlaylistAdapter playlistAdapter = new SelectPlaylistAdapter();
7271
recyclerView.setAdapter(playlistAdapter);

app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearchFragment.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.schabi.newpipe.settings.preferencesearch;
22

3+
import static org.schabi.newpipe.ui.emptystate.EmptyStateUtil.setEmptyStateComposable;
4+
35
import android.os.Bundle;
46
import android.view.LayoutInflater;
57
import android.view.View;
@@ -12,7 +14,6 @@
1214

1315
import org.schabi.newpipe.databinding.SettingsPreferencesearchFragmentBinding;
1416
import org.schabi.newpipe.ui.emptystate.EmptyStateSpec;
15-
import org.schabi.newpipe.ui.emptystate.EmptyStateUtil;
1617

1718
import java.util.List;
1819

@@ -41,9 +42,7 @@ public View onCreateView(
4142
binding = SettingsPreferencesearchFragmentBinding.inflate(inflater, container, false);
4243

4344
binding.searchResults.setLayoutManager(new LinearLayoutManager(getContext()));
44-
EmptyStateUtil.setEmptyStateComposable(
45-
binding.emptyStateView,
46-
EmptyStateSpec.Companion.getNoSearchMaxSizeResult());
45+
setEmptyStateComposable(binding.emptyStateView, EmptyStateSpec.NoSearchResult);
4746

4847
adapter = new PreferenceSearchAdapter();
4948
adapter.setOnItemClickListener(this::onItemClicked);

app/src/main/java/org/schabi/newpipe/ui/components/video/comment/CommentRepliesDialog.kt

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import androidx.compose.ui.Modifier
1919
import androidx.compose.ui.input.nestedscroll.nestedScroll
2020
import androidx.compose.ui.platform.rememberNestedScrollInteropConnection
2121
import androidx.compose.ui.res.pluralStringResource
22-
import androidx.compose.ui.res.stringResource
2322
import androidx.compose.ui.tooling.preview.Preview
2423
import androidx.compose.ui.tooling.preview.datasource.LoremIpsum
2524
import androidx.compose.ui.unit.dp
@@ -122,28 +121,23 @@ private fun CommentRepliesDialog(
122121

123122
if (comments.itemCount == 0) {
124123
item {
125-
val refresh = comments.loadState.refresh
126-
if (refresh is LoadState.Loading) {
127-
LoadingIndicator(modifier = Modifier.padding(top = 8.dp))
128-
} else if (refresh is LoadState.Error) {
129-
// TODO use error panel instead
130-
EmptyStateComposable(
131-
spec = EmptyStateSpec.DisabledComments.copy(
132-
descriptionText = {
133-
stringResource(R.string.error_unable_to_load_comments)
124+
when (val refresh = comments.loadState.refresh) {
125+
is LoadState.Loading -> {
126+
LoadingIndicator(modifier = Modifier.padding(top = 8.dp))
127+
}
128+
else -> {
129+
// TODO use error panel instead
130+
EmptyStateComposable(
131+
spec = if (refresh is LoadState.Error) {
132+
EmptyStateSpec.ErrorLoadingComments
133+
} else {
134+
EmptyStateSpec.NoComments
134135
},
135-
),
136-
modifier = Modifier
137-
.fillMaxWidth()
138-
.heightIn(min = 128.dp)
139-
)
140-
} else {
141-
EmptyStateComposable(
142-
spec = EmptyStateSpec.NoComments,
143-
modifier = Modifier
144-
.fillMaxWidth()
145-
.heightIn(min = 128.dp)
146-
)
136+
modifier = Modifier
137+
.fillMaxWidth()
138+
.heightIn(min = 128.dp)
139+
)
140+
}
147141
}
148142
}
149143
} else {

app/src/main/java/org/schabi/newpipe/ui/components/video/comment/CommentSection.kt

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import androidx.compose.ui.Modifier
1515
import androidx.compose.ui.input.nestedscroll.nestedScroll
1616
import androidx.compose.ui.platform.rememberNestedScrollInteropConnection
1717
import androidx.compose.ui.res.pluralStringResource
18-
import androidx.compose.ui.res.stringResource
1918
import androidx.compose.ui.tooling.preview.Preview
2019
import androidx.compose.ui.unit.dp
2120
import androidx.lifecycle.compose.collectAsStateWithLifecycle
@@ -75,7 +74,6 @@ private fun CommentSection(
7574
modifier = Modifier
7675
.fillMaxWidth()
7776
.heightIn(min = 128.dp)
78-
7977
)
8078
}
8179
} else if (count == 0) {
@@ -111,13 +109,7 @@ private fun CommentSection(
111109
is LoadState.Error -> {
112110
item {
113111
// TODO use error panel instead
114-
EmptyStateComposable(
115-
EmptyStateSpec.DisabledComments.copy(
116-
descriptionText = {
117-
stringResource(R.string.error_unable_to_load_comments)
118-
}
119-
)
120-
)
112+
EmptyStateComposable(EmptyStateSpec.ErrorLoadingComments)
121113
}
122114
}
123115

@@ -134,11 +126,7 @@ private fun CommentSection(
134126
item {
135127
// TODO use error panel instead
136128
EmptyStateComposable(
137-
spec = EmptyStateSpec.DisabledComments.copy(
138-
descriptionText = {
139-
stringResource(R.string.error_unable_to_load_comments)
140-
}
141-
),
129+
spec = EmptyStateSpec.ErrorLoadingComments,
142130
modifier = Modifier
143131
.fillMaxWidth()
144132
.heightIn(min = 128.dp)
Lines changed: 47 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.schabi.newpipe.ui.emptystate
22

33
import android.graphics.Color
4+
import androidx.annotation.StringRes
45
import androidx.compose.foundation.layout.Arrangement
56
import androidx.compose.foundation.layout.Column
67
import androidx.compose.foundation.layout.fillMaxWidth
@@ -22,25 +23,14 @@ import org.schabi.newpipe.ui.theme.AppTheme
2223
fun EmptyStateComposable(
2324
spec: EmptyStateSpec,
2425
modifier: Modifier = Modifier,
25-
) = EmptyStateComposable(
26-
emojiText = spec.emojiText(),
27-
descriptionText = spec.descriptionText(),
28-
modifier = modifier
29-
)
30-
31-
@Composable
32-
private fun EmptyStateComposable(
33-
emojiText: String,
34-
descriptionText: String,
35-
modifier: Modifier = Modifier,
3626
) {
3727
Column(
3828
modifier = modifier,
3929
horizontalAlignment = Alignment.CenterHorizontally,
40-
verticalArrangement = Arrangement.Center
30+
verticalArrangement = Arrangement.Center,
4131
) {
4232
Text(
43-
text = emojiText,
33+
text = spec.emojiText,
4434
style = MaterialTheme.typography.titleLarge,
4535
textAlign = TextAlign.Center,
4636
)
@@ -49,7 +39,7 @@ private fun EmptyStateComposable(
4939
modifier = Modifier
5040
.padding(top = 6.dp)
5141
.padding(horizontal = 16.dp),
52-
text = descriptionText,
42+
text = stringResource(spec.descriptionText),
5343
style = MaterialTheme.typography.bodyMedium,
5444
textAlign = TextAlign.Center,
5545
)
@@ -82,66 +72,48 @@ fun EmptyStateComposableNoCommentPreview() {
8272
}
8373
}
8474

85-
data class EmptyStateSpec(
86-
val emojiText: @Composable () -> String,
87-
val descriptionText: @Composable () -> String,
75+
enum class EmptyStateSpec(
76+
val emojiText: String,
77+
@field:StringRes val descriptionText: Int,
8878
) {
89-
companion object {
90-
91-
val GenericError =
92-
EmptyStateSpec(
93-
emojiText = { "¯\\_(ツ)_/¯" },
94-
descriptionText = { stringResource(id = R.string.empty_list_subtitle) },
95-
)
96-
97-
val NoVideos =
98-
EmptyStateSpec(
99-
emojiText = { "(╯°-°)╯" },
100-
descriptionText = { stringResource(id = R.string.no_videos) },
101-
)
102-
103-
val NoComments =
104-
EmptyStateSpec(
105-
106-
emojiText = { "¯\\_(╹x╹)_/¯" },
107-
descriptionText = { stringResource(id = R.string.no_comments) },
108-
)
109-
110-
val DisabledComments =
111-
NoComments.copy(
112-
descriptionText = { stringResource(id = R.string.comments_are_disabled) },
113-
)
114-
115-
val NoSearchResult =
116-
NoComments.copy(
117-
emojiText = { "╰(°●°╰)" },
118-
descriptionText = { stringResource(id = R.string.search_no_results) }
119-
)
120-
121-
val NoSearchMaxSizeResult =
122-
NoSearchResult
123-
124-
val ContentNotSupported =
125-
NoComments.copy(
126-
emojiText = { "(︶︹︺)" },
127-
descriptionText = { stringResource(id = R.string.content_not_supported) },
128-
)
129-
130-
val NoBookmarkedPlaylist =
131-
EmptyStateSpec(
132-
emojiText = { "(╥﹏╥)" },
133-
descriptionText = { stringResource(id = R.string.no_playlist_bookmarked_yet) },
134-
)
135-
136-
val NoSubscriptionsHint =
137-
EmptyStateSpec(
138-
emojiText = { "(꩜ᯅ꩜)" },
139-
descriptionText = { stringResource(id = R.string.import_subscriptions_hint) },
140-
)
141-
142-
val NoSubscriptions =
143-
NoSubscriptionsHint.copy(
144-
descriptionText = { stringResource(id = R.string.no_channel_subscribed_yet) },
145-
)
146-
}
79+
GenericError(
80+
emojiText = "¯\\_(ツ)_/¯",
81+
descriptionText = R.string.empty_list_subtitle,
82+
),
83+
NoVideos(
84+
emojiText = "(╯°-°)╯",
85+
descriptionText = R.string.no_videos,
86+
),
87+
NoComments(
88+
emojiText = "¯\\_(╹x╹)_/¯",
89+
descriptionText = R.string.no_comments,
90+
),
91+
DisabledComments(
92+
emojiText = "¯\\_(╹x╹)_/¯",
93+
descriptionText = R.string.comments_are_disabled,
94+
),
95+
ErrorLoadingComments(
96+
emojiText = "¯\\_(╹x╹)_/¯",
97+
descriptionText = R.string.error_unable_to_load_comments,
98+
),
99+
NoSearchResult(
100+
emojiText = "╰(°●°╰)",
101+
descriptionText = R.string.search_no_results,
102+
),
103+
ContentNotSupported(
104+
emojiText = "(︶︹︺)",
105+
descriptionText = R.string.content_not_supported,
106+
),
107+
NoBookmarkedPlaylist(
108+
emojiText = "(╥﹏╥)",
109+
descriptionText = R.string.no_playlist_bookmarked_yet,
110+
),
111+
NoSubscriptionsHint(
112+
emojiText = "(꩜ᯅ꩜)",
113+
descriptionText = R.string.import_subscriptions_hint,
114+
),
115+
NoSubscriptions(
116+
emojiText = "(꩜ᯅ꩜)",
117+
descriptionText = R.string.no_channel_subscribed_yet,
118+
),
147119
}

0 commit comments

Comments
 (0)